SpringMvc技术与Servlet技术功能等同,均属于web层开发技术
SpringMVC是一种基于java实现MVC模型的轻量级web框架
SpringMVC主要用在控制层开发的
@RequestMapping("/save") 设置web访问路径
写在类上表示表示类这个路径的前缀
写在方法上表示访问该方法的访问路径
@RequestMapping("/books") //设置web访问路径
@ResponseBody //设置当前方法的返回值为响应类容
public class BookController {}
@ResponseBody 设置当前方法的返回值为响应内容
写在类上表示该类所有方法的返回值为响应内容
ResponseBody
public String save(){
return "我是响应内容";
}
方法里面的的形参其实就是网页传过来的请求内容
ResponseBody
public String save(String name){
System.out.println(name);
return "我是响应内容";
}
配置SpringMvc的类
因为我们要让Mvc拦截 Tomcat 的资源然后上SpringMvc来处理,
然后加载SpringMvc的容器
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};//表示加载SpringConfig容器
}
protected Class>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class}; //表示加载SpringMvc容器
}
protected String[] getServletMappings() {
return new String[]{"/"};//"/" 表示拦截所有与资源
}
}
定义配置类 继承 AbstractAnnotationConfigDispatcherServletInitializer
重写里面的方法
然后加载容器,然后把资源拦截下来
接下来就就是加载配置类
我们SpringMvc记载配置只用加载SpringMvc的配置
@Configuration
@ComponentScan({"com.xxxxx.controller","com.xxxx.config"})
public class SpringMvcConfig {
}



