springmvc的执行原理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!--@Controller注解包扫描 -->
<context:component-scan base-package="com.holt.ssm.controller"/>
<!--注解驱动
加载处理器映射器
加载处理器适配器
-->
<context:annotation-config/>
<!--视图解析器,设置前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
在springmvc框架xml配置文件种有这么一个标签,这个标签就是用来扫描controller包中的@controller注解的。
<! --@Controller注解包扫描 -->
<context:component-scan base-package=“com.holt.ssm.controller”/>
这个注解驱动是加载处理器映射器和处理器适配器的,不再需要再springmvc配置文件中配置处理器映射器和处理器适配器。
< !–注解驱动
加载处理器映射器
加载处理器适配器
–>
context:annotation-config/
- controller类:
a:返回modelAndView
@Controller
//@RequestMapping(value="item")
public class ItemListController {
@Autowired
private Service service;
@RequestMapping("itemList")
public ModelAndView itemList() {
ModelAndView mav = new ModelAndView();
List<Item> itemList = service.itemList();
//加入需要显示的列表
mav.addObject(itemList);
mav.setViewName("itemList");
//返回modelAndView给dispatcherServlet
return mav;
}
}
在controller类上加@RequestMapping(value=“item”)是进行二级目录管理,比如现在的url就是localhost:8080/springmvc/item/itemlist.action.多了一层item。
b:也可以使用字符串传递:
/**
* 使用字符串传递到jsp return返回jsp页面的名字
* @return string
*/
@RequestMapping(value ="itemList")
public String itemList(ModelMap model) {
List<Item> itemList = service.itemList();
model.addAttribute("itemList",itemList);
System.out.println("itemList方法执行...");
return "itemList";
}
c:return后面请求转发和重定向
//用pojo的方法传递
@RequestMapping(value="updateItem",method = {RequestMethod.GET,RequestMethod.POST})
public String updateItem( Item item ,ModelMap model) {
service.updateItem(item);
System.out.println(item);
model.addAttribute("item",item);
model.addAttribute("msg1","修改成功");
//返回itemEdit.jsp
// return "itemList";
//return "forward:item.action"
return "redirect:item.action";
}
return 后面本需要跟jsp完整的名字比如WEB-INF/JSP/itemList.jsp,但是在springmvc.xml中有视图解析器的配置 使得只需要写jsp页面的名字即可。
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
拦截器是在controller方法执行前后进行逻辑操作的类。只能对controller请求进行拦截,对其他的一些比如直接访问静态资源的请求则没办法进行拦截处理,拦截器需要继承HandlerInterceptor。
public class Interceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("afterCompletion...");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("postHandler...");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("preHandler...");
return true;
}
}
在springmvc.xml里面配置interceptor
<!--inteceptor拦截器配置 -->
<!-- 定义拦截器 -->
<mvc:interceptors>
<!-- 定义一个拦截器-->
<!--定义一个登录拦截 -->
<mvc:interceptor>
<!--path配置</**>拦截所有请求,包括二级以上目录,
</*>拦截所有请求,不包括二级以上目录-->
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/toLogin.action"/>
<bean class="com.holt.springmvc.Interceptor.UserInterceptier"/>
</mvc:interceptor>
</mvc:interceptors>
preHandle:在@RequestMapping注解方法前进行拦截。
postHandle:在@RequestMapping注解方法执行时返回视图前进行拦截,preHandle 方法的返回值为true 时才能被调用。
afterCompletion:在@RequestMapping返回视图后进行拦截,该方法也是需要当前对应的Interceptor 的preHandle 方法的返回值为true 时才会执行。顾名思义,该方法将在整个请求结束之后,也就是在DispatcherServlet 渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的。
Intercepter详解连接.
283

被折叠的 条评论
为什么被折叠?



