pom.xml添加依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>



servlet配置:web.xml文件

<!-- 
SpringMVC思想是有一个前端控制器能拦截所有请求,并智能派发。
这个前端控制器就是一个servlet
-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- contextConfigLocation:指定SpringMVC配置文件位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<!--
servlet启动加载
servlet原本是第一次访问创建对象
load-on-startup:服务器启动的时候创建对象,值越小,优先级越高,越先创造对象
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--
/ 和 /* 都是拦截所有氢气
/:不会拦截 *.jsp 页面
/*:会拦截 *.jsp 页面
-->
<url-pattern>/</url-pattern>
</servlet-mapping>



视图解析器

视图解析器能帮我们拼接页面地址

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>



乱码

GET请求乱码

修改tomcat下的 conf/server.xml 文件,找到如下代码:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>


可以在这里添加一个属性:URIEncoding,将该属性值设置为UTF-8,即可让Tomcat(默认ISO-8859-1编码)以UTF-8的编码处理get请求

<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>


POST请求乱码

web.xml 文件中添加以下代码

<!-- 字符过滤,防止post请求乱码 -->
<filter>
<filter-name>SpringCharacterEncodingFilter</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-mapping>
<filter-name>SpringCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



静态资源

<!-- 启用注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 处理静态资源 -->
<mvc:default-servlet-handler/>



拦截器

拦截器配置

<!--拦截器(多个拦截器,顺序执行) -->
<mvc:interceptors>
<!-- 配置某个拦截器,默认是拦截所有请求的 -->
<bean class="cn.javaex.uscat.config.interceptor.QingInterceptor"></bean>

<!-- 配置某个拦截器更详细的信息 -->
<!-- <mvc:interceptor> -->
<!-- <mvc:mapping path="/**"/> -->
<!-- <bean class="cn.javaex.uscat.config.interceptor.QingInterceptor"></bean> -->
<!-- </mvc:interceptor> -->
</mvc:interceptors>


拦截器配置类:实现HandlerInterceptor接口

package cn.javaex.uscat.config.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
* 拦截器
*/
public class QingInterceptor implements HandlerInterceptor {

/**
* 目标方法执行之前
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}

/**
* 目标方法执行之后
*/
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {

}

/**
* 请求结束后
*/
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {

}

}




回到顶部