依赖架包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
web项目自动扫描web.xml,继而加载servlet-context.xml和root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 系统环境变量 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- 监听web启动,加载spring对象加载到web环境 (tomcat ,jetty) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
servlet-context.xml文件扫描com.testfan.spring包下面的文件,通过@controller支持servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- 支持 @Controller 开启 -->
<annotation-driven content-negotiation-manager="contentNegotiationManager" />
<!-- 静态资源位置 -->
<resources mapping="/resources/**" location="/resources/" />
<!-- jsp支持 -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- json 支持 -->
<beans:bean id="messageAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<!-- Support JSON -->
<beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- 默认支持json -->
<beans:bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<beans:property name="defaultContentType" value="application/json" />
</beans:bean>
<!-- 文件上传支持 -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<beans:property name="maxUploadSize" value="20000000"/>
<!-- 最大内存大小 (10240)-->
<beans:property name="maxInMemorySize" value="40960" />
</beans:bean>
<!-- 异常支持 -->
<beans:bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<beans:property name="exceptionMappings">
<beans:props>
<!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到配置页面 -->
<beans:prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">upload/error_fileupload</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<context:component-scan base-package="com.testfan.spring" />
</beans:beans>
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
HttpServletRequest request;
// 页面跳转和传参数 ModelAndView 方式
@RequestMapping(value = "/test1", method = RequestMethod.GET)
public ModelAndView test() {
ModelAndView modelAndView = new ModelAndView("home");
modelAndView.addObject("name", "test12222");
return modelAndView;
}
// GetMapping == @RequestMapping(value = "/test2", method = RequestMethod.GET)
// 也可以不采用ModelAndView 用request 对象
@GetMapping("/test2")
public String test2(HttpServletRequest request) {
request.setAttribute("name", "test2");
return "home";
}
}
spring mvc如何和web工程集成?
1.包扫描@controller 支持servlet
2.servlet 协议和路径
@RequestMapping 访问路径
@GetMapping/@PostMapping 协议
3.ModelAndView 返回参数
4.jsp路径通过spring提供对象控制
5.对象自动注入,最大亮点