多个@ModelAttribute 执行顺序

正怒月神 2017-06-09 04:10:00
事情是这样的

我创建了一个 BaseController。
使得每个 controller都继承自BaseController。
然后在BaseController中,进行一些操作。比如读取配置文件,读取用户权限等等。

那么问题来了。
如下代码
@ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
this.session = request.getSession();
this.currentUser = (SysUser) this.session.getAttribute("userMessage");

}
//endregion


/**
* 绑定项目根路径供jsp调用
* @param request
* @return
*/
@ModelAttribute("ctx")
public String getContext(HttpServletRequest request) {
basePath = request.getContextPath();
return basePath;
}

@ModelAttribute("text")
public void getText(HttpServletRequest request) {

}


他们的执行顺序到底是怎么个情况呢?
网上资料,有的说顺序执行,有的说反序执行。

我自己测试,
发现根本没有顺序可言
请问@ModelAttribute到底是什么机制呢?
...全文
854 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2021-02-20
  • 打赏
  • 举报
回复
引用 23 楼 fxj02014 的回复:
已知的几种无序: (1)同一个实现类中执行的顺序是按java.lang.Class#getDeclaredMethods。根据javadoc, 此方法不保证顺序。 (2)同一个实现类中接口获得按 java.lang.Class#getInterfaces,此接口按声明顺序返回,无接口内的方法仍然无序 截止到springmvc 5.1.9没有提供Order之类的方式改变顺序,有依赖关系的只能在同一个model方法里解决
多谢解惑。
fxj02014 2021-02-19
  • 打赏
  • 举报
回复
已知的几种无序: (1)同一个实现类中执行的顺序是按java.lang.Class#getDeclaredMethods。根据javadoc, 此方法不保证顺序。 (2)同一个实现类中接口获得按 java.lang.Class#getInterfaces,此接口按声明顺序返回,无接口内的方法仍然无序 截止到springmvc 5.1.9没有提供Order之类的方式改变顺序,有依赖关系的只能在同一个model方法里解决
fxj02014 2021-02-19
  • 打赏
  • 举报
回复
已知的几种顺序: (1)Advice中定义的方法优先于Controller里执行 (2)实现类中的定义的方法优先于接口中执行
正怒月神 2019-07-09
  • 打赏
  • 举报
回复
引用 19 楼 peakzhou 的回复:
在最新版本中,貌似按照方法名首字母倒叙。
晚点我试试看
peakzhou 2019-07-06
  • 打赏
  • 举报
回复
在最新版本中,貌似按照方法名首字母倒叙。
比格斯特帅 2018-02-12
  • 打赏
  • 举报
回复
一个controller映射多个URL的用法来说,要谨慎使用,在一个控制器里执行顺序是乱的,最好是一个控制器限制用一个
正怒月神 2018-01-23
  • 打赏
  • 举报
回复
引用 16 楼 qq_28748797 的回复:
楼主解决了么,我好想知道答案啊
没有解决。我测试下来,没有顺序可言。
Ckuangf 2018-01-09
  • 打赏
  • 举报
回复
楼主解决了么,我好想知道答案啊
正怒月神 2017-10-23
  • 打赏
  • 举报
回复
引用 14 楼 qq_16180745 的回复:
我也试过了,代码顺序是 abcdefghij 输出来的是 bbb iii jjj eee ddd fff ggg hhh aaa ccc , 真不知道按什么顺序来的
只有等这一块懂一点的人来回答了。
qq_16180745 2017-07-18
  • 打赏
  • 举报
回复
我也试过了,代码顺序是 abcdefghij 输出来的是 bbb iii jjj eee ddd fff ggg hhh aaa ccc , 真不知道按什么顺序来的
正怒月神 2017-06-13
  • 打赏
  • 举报
回复
最后,我找了3天3夜。 测试了无数遍。然而并没有什么执行顺序可言,放弃了。
正怒月神 2017-06-12
  • 打赏
  • 举报
回复
引用 2 楼 u010087908 的回复:
http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods 好像没有区别吧
没有说明执行的顺序的情况。 而且我的是在一个基类中的方法添加的 @modelattribute注解。他们是会全部执行的。 并不类似于controller中的@modelattribute只根据访问的action来跑对应的@modelattribute
正怒月神 2017-06-12
  • 打赏
  • 举报
回复
引用 10 楼 u010087908 的回复:
Rule-2: If more than one method in the controller are annotated with the @ModelAttribute, then the execution follows the sequence order of the @ModelAttribute annotated methods. (Check the output)
可我测试下来,并不是按照@ModelAttribute注解顺序输出的。
NANU-NANA 2017-06-12
  • 打赏
  • 举报
回复
Note-1: No need to write any code to set the model attribute into the request scope. Note-2: we need to use @SessionAttributes at the class level in the controller class to set the model attributes into the session scope.
NANU-NANA 2017-06-12
  • 打赏
  • 举报
回复
最后一个有执行顺序 我给你吧文章结论贴出来 Rule-1: If the @ModelAttribute is used at Method level, then those methods are executed first, remaining methods (which are not annotated with @ModelAttribute) are executed with the help of @RequestMapping annotation. Rule-2: If more than one method in the controller are annotated with the @ModelAttribute, then the execution follows the sequence order of the @ModelAttribute annotated methods. (Check the output) Rule-3: If the method parameter is annotated with @ModelAttribute, those methods are executed w.r.t @RequestMapping annotation. Ex: During the submit button execution (in controller it is POST)
正怒月神 2017-06-12
  • 打赏
  • 举报
回复
引用 8 楼 u010087908 的回复:
https://stackoverflow.com/questions/5955875/can-i-user-modelattribute-in-parent-abstract-class-for-controllers http://www.codelooru.com/2010/11/how-does-modelattribute-work.html http://ankursinghal86.blogspot.de/2014/07/how-modelattribute-annotation-works-in.html
前两个都看了,最后一个打不开。 不是讨论的执行顺序为题。只是讨论了执行的机制和用处 我去找找源码看看算了。实在是没办法了。
NANU-NANA 2017-06-12
  • 打赏
  • 举报
回复
https://stackoverflow.com/questions/5955875/can-i-user-modelattribute-in-parent-abstract-class-for-controllers http://www.codelooru.com/2010/11/how-does-modelattribute-work.html http://ankursinghal86.blogspot.de/2014/07/how-modelattribute-annotation-works-in.html
正怒月神 2017-06-12
  • 打赏
  • 举报
回复
引用 6 楼 u010087908 的回复:
这个输出顺序和modelattribute的加载顺序是一致的么? 我想知道,这个执行顺序,在实际项目中给你带来的具体困扰是什么?
不一致,代码上的先后顺序和实际输出不一致。 困扰就是,同事在初始化比如test1方法中的currentUser对象时, 原本想在第二个test2方法中使用。 但是由于加载顺序并没有按照代码顺序执行,导致了test2方法先运行而使得currentUser对象为空的问题。 这里test1和test2方法只是假设,和前面的代码无关。不过modelattribute的执行顺序的确是让我捉摸不透的,测试发觉并不是顺序或者倒叙的。
NANU-NANA 2017-06-12
  • 打赏
  • 举报
回复
这个输出顺序和modelattribute的加载顺序是一致的么? 我想知道,这个执行顺序,在实际项目中给你带来的具体困扰是什么?
正怒月神 2017-06-12
  • 打赏
  • 举报
回复
引用 4 楼 u010087908 的回复:
我觉得,应该就是按你的书写顺序吧
不是哎,我测试了
public class TestBaseController {
    @RequestMapping(value = "test2.do")
    public void test2()
    {
        System.out.println(222222);
    }


    @ModelAttribute("cccc")
    public void test5()
    {
        System.out.println("ccccc");
    }

    @ModelAttribute()
    public void test6()
    {
        System.out.println("6666666");
    }

    @ModelAttribute("aaaa")
    public void test3()
    {
        System.out.println("aaaaa");
    }

    @ModelAttribute("bbbb")
    public void test4()
    {
        System.out.println("bbbbb");
    }

    @ModelAttribute("acc")
    public PageAccessCode setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("acc");
        return null;
    }


    @ModelAttribute("ctx")
    public String getContext(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("ctx");
        return null;
    }
}
结果是: ctx ccccc 6666666 aaaaa acc bbbbb
加载更多回复(3)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧