避免为每个URL加载@ModelAttribute(" attr")

时间:2015-05-06 09:33:03

标签: java spring modelattribute

我在jsp中有一个下拉列表,我正在使用@modelAttribute填充数据库中的数据。但是每当url在url执行模型属性代码运行之前命中并且不必要地命中数据库....是任何方式来避免这.... 我的代码是

@ModelAttribute("courseList")
    public Map<String, String> gerCourseNameList(HttpSession session,
        HttpServletRequest httpSevletRequest, HttpServletResponse response,
        ModelMap model) throws IOException {
    Map<String, String> map = null;

 ------ My code here------

}

我的日志文件

 [DEBUG] SessionFilter| Url :http://localhost:8080/EnKalviV1/studentallocation.htm
 [06 May 2015 15:01:11] [Inst Id: INS | Branch Id: BR001 | User Id: bradm1 | Session Id: 97910CBDA9FF68175302717842E9923F]
 [DEBUG] LoggingAspect| Class Name :com.jaw.student.controller.StudentAllocationController,Calling gerCourseNameList() method.
 [06 May 2015 15:01:11] [Inst Id: INS | Branch Id: BR001 | User Id: bradm1 | Session Id: 97910CBDA9FF68175302717842E9923F]

即使对于该控制器中的ajax调用,此代码也在运行......

如何避免这种情况....

1 个答案:

答案 0 :(得分:0)

只要收到不包含该名称的模型属性的请求,就会运行此方法。

向控制器类添加注释

@SessionAttributes ({"courseList"})

这将导致将模型属性添加到会话中,然后在检查之前将其添加回模型,否则将导致方法执行。