2016-12-04 35 views
0

我有一个奇怪的行为与SessionAttribute。 我已经定义了一个名为nafSelection的SessionAttribute,用于2个控制器:NafController和StatsController。Spring MVC SessionAttribute在所有控制器中都没有初始化(Spring Boot 1.4.2)

当我通过NafController时,SessionAttribute被创建并且StatsController可以使用它。如果首先,我使用StatsController我有一个错误“Missing会话属性'nafSelection'类型NafSelection”。

共享模型属性,我编写了一个ControllerAdvice:

@ControllerAdvice('com.dyndata.sirene.controllers') 
class ModelAdvice { 
    @ModelAttribute("nafSelection") 
    NafSelection newSelection() { 
     new NafSelection() 
    } 
} 

我宣布会议属性在我的2个控制器:

@Controller 
@SessionAttributes("nafSelection") 
class NafController { 
    private static Logger logger = Logger.getLogger(NafController.class.name); 

    @RequestMapping('/naf') 
    def naf() { 
     'naf' 
    } 

    @RequestMapping(path = '/naf/{nafCode}', method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) 
    @ResponseBody 
    def affNafCode(@PathVariable String nafCode, @SessionAttribute NafSelection nafSelection) { 
     logger.info("Adds the NAF " + nafCode) 
     nafSelection.nafs << nafCode 

     return new Counter(count: 5600) 
    } 

    @RequestMapping(path = '/naf/{nafCode}', method = RequestMethod.DELETE) 
    @ResponseBody 
    def delNafCode(@PathVariable String nafCode, @SessionAttribute NafSelection nafSelection) { 
     logger.info("Deletes the NAF " + nafCode) 
     nafSelection.nafs.remove(nafCode) 

     return new Counter(count: 80000) 
    } 
} 

第二控制器:

@Controller 
@SessionAttributes("nafSelection") 
class StatsController { 
    private static Logger logger = Logger.getLogger(StatsController.class.name); 

    @RequestMapping(path = '/stats') 
    def stats(@SessionAttribute NafSelection nafSelection) { 
     'stats' 
    } 
} 

为什么会话属性由NafController而不是由StatsController管理?

注意:我的代码采用Groovy语言。

回答

0

我发现:)

我必须标记方法的参数为的ModelAttribute而不是SessionAttribute。

相关问题