简体   繁体   中英

How to access parameter in spring controller method when its called second time

I have updateNewPassword() in my controller which gets hString from request for the first time and when the second time its called I need to access hString again so am adding it to ModelAndView object mav before return statement as below but on the second call to updateNewPassword() am getting hString as null. Can someone please help me how to fix this?

Am using Spring MVC 4 and JDK 7

@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView updateNewPassword(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException 
{
    getCurrentRequestProperties().put(CurrentRequestProperties.IS_VALID_REQUEST, true);

    ModelAndView mav = new ModelAndView();
    String hString = request.getParameter("hString");
    String newPassword = request.getParameter("newPassword");
    String confirmPassword = request.getParameter("confirmPassword");

    if (StringUtils.isNotEmpty(newPassword) || StringUtils.isNotEmpty(confirmPassword)) 
    {
        UserSecurityQuestions qRecord = api.search.query(UserSecurityQuestions.class,
                api.search.property("hashString").eq(hString)).first();
        if (qRecord != null && !qRecord.isValidRequest()) 
        {
            mav.addObject("failedMessage", "forgot.url.expired");
        } 
        else 
        {
          // updates the password
         }
        } else {
        mav.addObject("hString", hString);
        mav.addObject("notimeout", true);
    }
    return mav;
}

need to access hString again so am adding it to ModelAndView object mav before return statement as below but on the second call to updateNewPassword() am getting hString as null.

When you add it to the model . it will be available only till the current request is alive (ie) only till your view is rendered.

So it returned null . if you want the variable to be available in the further request , you could store it in the session .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM