Spring MVC在控制器之间传递数据

时间:2012-12-06 18:09:08

标签: java spring spring-mvc

我正在使用SpringMVC编写Web应用程序。我想在会话中保留一些数据,直到用户注销。之前我是通过@SessionAttributes("someData")完成的,但后来我必须将@ModelAttribute("someData") someDataType someData作为每个请求映射方法的参数。所以这就是我所做的:

我的AccessData类:

@Component
@Scope(value = "session")
public class AccessData {

    private long userID;

    private String userKey;

    public AccessData(long ID, String key) {
        this.userID = ID;
        this.userKey = key;
    }

    public long getUserID() {
        return userID;
    }

    public void setUserID(long userID) {
        this.userID = userID;
    }

    public String getUserKey() {
        return userKey;
    }

    public void setUserKey(String userKey) {
        this.userKey = userKey;
    }
}

第一个控制器(这里我从表单中获取并验证用户输入):

@Controller
@Scope(value = "session")
public class LoginController {

    @ModelAttribute("accessData")
    public AccessData getAccessData() {
        return this.accessData;
    }

    private Utils utilsService;

    private LoginService loginService;

    @Autowired
    private AccessData accessData;

    @Autowired
    public LoginController(LoginService loginService, Utils utils) {
        this.loginService = loginService;
        this.utilsService = utils;
    }

    @RequestMapping(value = ControllerPaths.LOGIN, method = RequestMethod.POST)
    public ModelAndView showLoginStatus(
            @ModelAttribute(LOGINDATA) LoginData loginData) {
        try {
            accessData = loginService.validateLoginData(loginData);
        } catch (IncorrectLoginDataException e) {
            logger.trace("Showing fail login screen...");
            return utilsService.getShowView(ViewPaths.LOGIN_FAIL);
        } catch (HTTPException e) {
            return utilsService.getShowViewWithStringAttribute(
                    ViewPaths.INFO_VIEW, MESSAGE, CONNECTION_ERROR);
        }
        return utilsService.getShowView(ViewPaths.LOGIN_SUCCESS);
    }
}

第二控制器:

@Controller
@Scope(value = "session")
public class SecondController {

    @ModelAttribute("accessData")
    public AccessData getAccessData() {
        return this.accessData;
    }

    private Utils utilsService;

    private LoginService loginService;

    @Autowired
    private AccessData accessData;

    @Autowired
    public SecondController(LoginService loginService, Utils utils) {
        this.loginService = loginService;
        this.utilsService = utils;
    }

    @RequestMapping(value = ControllerPaths.SHOW_ACCESS_DATA, method =   RequestMethod.GET)
    public ModelAndView showAccessData(
        System.out.println(accessData.getUserKey());
        return utilsService.getShowView(ViewPaths.INDEX);
    }
}

问题是当我在第二个控制器中打印userKey值时,该值为null。我检查了LoginController我是否从服务器获得了正确的数据,这没关系。那么我做错了什么?在此先感谢您的帮助

1 个答案:

答案 0 :(得分:2)

问题是您要为accessData变量分配一个新对象。相反,您应该更新它已经引用的对象的字段。

accessData = loginService.validateLoginData(loginData);

// Replace above line with something like this. Implement the copyProperties
// method to copy the attributes you need
AccessData newAccessData = loginService.validateLoginData(loginData);
copyPropteries(accessData,newAccessData);

此外,不需要使控制器会话作用域,将proxyMode=TARGET_CLASS添加到AccessData类的范围注释。