4

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);
    }
}

2番目のコントローラー:

@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);
    }
}

問題は、2番目のコントローラーでuserKey値を出力しているときに、値がnullになることです。サーバーから正しいデータを取得しているかどうかを確認しましたが、問題ありませんLoginController。だから私は何が間違っているのですか?助けてくれてありがとう

4

1 に答える 1

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);

また、Controllersセッションのスコープを設定する必要はなく、クラスproxyMode=TARGET_CLASSのスコープアノテーションに追加します。AccessData

于 2012-12-07T08:12:10.510 に答える