Spring mvc 笔记 @Controller @ModelAttribute @SessionAttributes @ControllerAdvice

简介:

Spring MVC中的配置中一般会遇到这两个标签,作为<context:component-scan>的子标签出现。

但在使用时要注意一下几点:

1.在很多配置中一般都会吧Spring-common.xml和Spring-MVC.xml进行分开配置,这种配置就行各施其职一样,显得特别清晰。

在Spring-MVC.xml中只对@Controller进行扫描就可,作为一个控制器,其他的事情不做。

在Spring-common.xml中只对一些事务逻辑的注解扫描。

2.现在给定一个项目包的机构:

com.fq.controlller

com.fq.service

就先给定这两个包机构

(1)在Spring-MVC.xml中有以下配置:

1
2
3
4
5
<!-- 扫描 @Controller 注解 -->
<context:component-scan base- package = "com.fq.controller" >
     <context:include-filter type= "annotation"
     expression= "org.springframework.stereotype.Controller"  />
     </context:component-scan>

可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描, 而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use- default-filters=”false”。

(2)在Spring-common.xml中有如下配置:

1
2
3
4
5
<!-- 配置扫描注解,不扫描 @Controller 注解 -->
<context:component-scan base- package = "com.fq" >
     <context:exclude-filter type= "annotation"
         expression= "org.springframework.stereotype.Controller"  />
         </context:component-scan>

可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。



如果把@ModelAttribute放在方法的注解上时,代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法


在 默认情况下,ModelMap 中的属性作用域是 request 级别是,也就是说,当本次请求结束后,ModelMap 中的属性将销毁。如果希望在 多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样ModelMap 的属性才可以被跨请求访问。

 

      Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的ModelMap 的属性 列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。

 

使模型对象的特定属性具有 Session 范围的作用域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  com.baobaotao.web;  
   
…  
import  org.springframework.ui.ModelMap;  
import  org.springframework.web.bind.annotation.SessionAttributes;  
   
@Controller  
@RequestMapping ( "/bbtForum.do" )  
@SessionAttributes ( "currUser" //①将ModelMap中属性名为currUser的属性  
//放到Session属性列表中,以便这个属性可以跨请求访问 
public  class  BbtForumController {  
…  
     @RequestMapping (params =  "method=listBoardTopic" )  
     public  String listBoardTopic( @RequestParam ( "id" ) int  topicId, User user,  
ModelMap model) {  
         bbtForumService.getBoardTopics(topicId);  
         System.out.println( "topicId:"  + topicId);  
         System.out.println( "user:"  + user);  
         model.addAttribute( "currUser" ,user);  //②向ModelMap中添加一个属性
         return  "listTopic" ;  
     }  
   
}

我们在 ② 处添加了一个 ModelMap 属性,其属性名为 currUser,而 ① 处通过 @SessionAttributes 注解将ModelMap 中名为 currUser 的属性放置到 Session 中, 所以我们不但可以在 listBoardTopic() 请求所对应的 JSP视图页面中通 过 request.getAttribute(“currUser”) 和 session.getAttribute(“currUser”) 获 取 user 对象,还可以在下一个请求所对应的 JSP 视图页面中通 过 session.getAttribute(“currUser”) 或 ModelMap#get(“currUser”) 访问到这个属性。

 

    这里我们仅将一个 ModelMap 的属性放入 Session 中,其实 @SessionAttributes 允许指定多个属性。你可以通过字符 串数组的方式指定多个属性,如 @SessionAttributes({“attr1”,”attr2”})。此 外,@SessionAttributes 还可以通过属性类型指定要 session 化的 ModelMap 属性, 如 @SessionAttributes(types = User.class),当然也可以指定多个类,如 @SessionAttributes(types
= {User.class,Dept.class}),还可以联合使用属性名和属性类型指定:@SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。

 

二、@ModelAttribute

 

     我们可以在 需要访问 Session 属性的 controller 上加上 @SessionAttributes,然后在 action 需要的 User 参数上加上 @ModelAttribute,并保证两者的属性名称一致。SpringMVC 就会自动将 @SessionAttributes 定义的属性注入到 ModelMap 对象,在 setup action 的参数列表时,去
ModelMap 中取到这样的对象,再添加到参数列表。只要我们不去调用 SessionStatus 的 setComplete() 方法,这个对象就会一直保留在 Session 中,从而实现 Session 信息的共享。

1
2
3
4
5
6
7
8
9
10
@Controller  
@SessionAttributes ( "currentUser" )
public  class  GreetingController{  
@RequestMapping  
public  void  hello( @ModelAttribute ( "currentUser" )
 
  User user){  
   //user.sayHello()  
}  
}

@ControllerAdvice 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import  org.springframework.web.bind.annotation.ControllerAdvice;  
import  org.springframework.web.bind.annotation.ExceptionHandler;  
import  org.springframework.web.servlet.ModelAndView;  
    
@ControllerAdvice  
public  class  ExceptionControllerAdvice {  
    
     @ExceptionHandler (Exception. class )  
     public  ModelAndView exception(Exception e) {  
            
         ModelAndView mav =  new  ModelAndView( "exception" );  
         mav.addObject( "name" , e.getClass().getSimpleName());  
         mav.addObject( "message" , e.getMessage());  
    
         return  mav;  
     }
1
2
3
4
<context:component-scan base- package = "com.sishuok.es"  use- default -filters= "false" >  
        <context:include-filter type= "annotation"  expression= "org.springframework.stereotype.Controller" />  
        <context:include-filter type= "annotation"  expression= "org.springframework.web.bind.annotation.ControllerAdvice" />  
    </context:component-scan>          本文转自yunlielai51CTO博客,原文链接:http://blog.51cto.com/4925054/1715220,如需转载请自行联系原作者
相关文章
|
1月前
|
缓存 前端开发 Java
Spring MVC 面试题及答案整理,最新面试题
Spring MVC 面试题及答案整理,最新面试题
88 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
ssm(Spring+Spring mvc+mybatis)——updateDept.jsp
10 0
|
1月前
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
ssm(Spring+Spring mvc+mybatis)——showDept.jsp
9 0
|
1月前
|
SQL JavaScript Java
springboot+springm vc+mybatis实现增删改查案例!
springboot+springm vc+mybatis实现增删改查案例!
25 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
41 1
|
11天前
|
数据采集 前端开发 Java
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
数据塑造:Spring MVC中@ModelAttribute的高级数据预处理技巧
23 3
|
11天前
|
存储 前端开发 Java
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
会话锦囊:揭示Spring MVC如何巧妙使用@SessionAttributes
14 1
|
11天前
|
前端开发 Java Spring
数据之桥:深入Spring MVC中传递数据给视图的实用指南
数据之桥:深入Spring MVC中传递数据给视图的实用指南
29 3
|
21天前
|
前端开发 安全 Java
使用Java Web框架:Spring MVC的全面指南
【4月更文挑战第3天】Spring MVC是Spring框架的一部分,用于构建高效、模块化的Web应用。它基于MVC模式,支持多种视图技术。核心概念包括DispatcherServlet(前端控制器)、HandlerMapping(请求映射)、Controller(处理请求)、ViewResolver(视图解析)和ModelAndView(模型和视图容器)。开发流程涉及配置DispatcherServlet、定义Controller、创建View、处理数据、绑定模型和异常处理。
使用Java Web框架:Spring MVC的全面指南
|
27天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
61 0