spring-context、spring-webmvc、spring-jdbc、spring-tx、servlet-api、jsp-api、jstl、jackson-core、jackson-databind、jackson-annotations等,视需求而定
<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<!--需要加载配置文件的对象,配置时需要考虑文件名抽取的问题,便于解耦和维护等,故配置此变量--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
命名空间:
xmlns:mvc="http://www.springframework.org/schema/mvc"
约束路径:
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
<!--mvc的注解驱动--> <mvc:annotation-driven/>
<context:component-scan back-package="包全限定名(controller层)"/>
<mvc:default-servlet-handler/>
视图解析器的默认设置
REDIRECT_URL_PREFIX = "redirect:" --重定向前缀
FORWARD_URL_PREFIX = "forward:" --转发前缀(默认值)
prefix = ""; --视图名称前缀
suffix = ""; --视图名称后缀
实际配置,以下代码仅配置访问jsp页面时的返回数据
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean>
<mvc:interceptors><!--该标签内配置多个拦截器--> <mvc:interceptor> <!--对哪些路径进行拦截,本例中拦截所有--> <mvc:mapping path="/**"/> <mvc:exclude-mapping path=""/> <bean class="MyInterceptor全类名"/> </mvc:interceptor> </mvc:interceptor>
<bean class="SimpleMappingExceptionResolver全类名"> <property name="defaultErroeView" value="error"/><!--默认错误视图--> <property name="exceptionMappings"> <map> <entry key="MyException全类名" value="error"/> <entry key="其他异常全类名" value="error"/> </map> </property> </bean>
告知SpringMVC框架,方法返回的字符串不是跳转是直接在http响应体中返回。
当使用ajax提交时,可以指定contentType为json形式,那么在方法参数位置使用@RequestBody可以直接接收集合数据而无需使用POJO进行包装。
public void quickMethod13(@RequestBody List<User> userList)
当请求的参数名称与Controller的业务方法参数名称不一致时,就需要通过@RequestParam注解显示的绑定。有以下参数可以使用:
<!--form表单代码--> <form action="${pageContext.request.contextPath}/quick14" method="post"> <input type="text" name="name"><br> <input type="submit" value="提交"><br> </form>
//获取请求数据时的注解配置
public void quickMethod14(@RequestParam("name") String username)
原文:https://www.cnblogs.com/dwyer/p/11182738.html