第一步:web项目创建
第二步:导入jar包
第三步:配置web.xml
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
CharacterEncodingFilter
/*
springMvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:SpringMVC.xml
springMvc
/
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
default
*.js
default
*.css
default
*.jpg
第四步:配置SpringMVC.xml
===================
第四步:配置SpringMVC.xml
xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
第五步:配置applicationContext.xml
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
第六步:创建包结构
第七步:完成各层类和接口编写
控制层:@Controller
业务层:@Service
持久层:@Repository
自动出入的属性注解:
@Resource
javaEE 提供的注解 jdk1.5之后才有 先根据名称自动注入(属性名称和spring容器中bean名称一致)
@Autowired
spring提供的注解 先根据类型自动注入 (属性的类型和spring容器中bean的class属性一致)
拦截器
步骤:自定义拦截器
方式一:实现接口HandlerInterceptor
/*
- 自定义拦截器 :
- 实现HandlerInterceptor接口
- */
public class LoginInterceptor implements HandlerInterceptor {
/*
* preHandle:
* 在处理器方法之前执行的方法,返回值为布尔类型
* 当返回true的时候就会放行去执行处理器方法
* 当返回false的时候 会终止本次请求
*
* 可以根据参数列表中的request response对象进行重定向或者转发
*
* */
@Override
public boolean preHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o) throws Exception {
User user = (User) httpServletRequest.getSession().getAttribute("user");
if(user==null){
//重定向到登录方法
httpServletResponse.sendRedirect("../user/toLogin");
return false;
}
return true;
}
/*
* postHandle:是在处理器方法之后执行的方法
* 参数中的modelAndView 可以修改处理器的结果 可以重新设置视图和数据
*
* */
@Override
public void postHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
/*
* afterCompletion:是跟preHandle的返回值有关系
* 如果preHandle 返回为false 那么这个方法就不再执行
* 如果preHandle 返回的为true springMvc 会将afterCompletion 放入方法栈中等待执行
* 视图解析之后的方法 就不能再改变视图和数据
* */
@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
方式二:继承类HandlerInterceptorAdapter
public class LoginInterceptor2 extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
User user = (User) request.getSession().getAttribute("user");
if(user==null){
//重定向到登录方法
response.sendRedirect("../user/toLogin");
return false;
}
return true;
}
}
springMvc.xml中配置拦截器
{
}
拦截器的执行顺序:
单个拦截器:preHandler > 处理器方法 > postHandler > afterCompletion
两个拦截器:
pre0000 第一个preHandler
pre 第二个preHandler
list处理器方法
post 第二的postHandler
post0000 第一个的postHandler
afterCompletion 第二个的afterCompletion
afterCompletion00000 第一个的afterCompletion