简体   繁体   中英

I need to pass the value of userName from Spring controller to JSP page

@RequestMapping(value = "/submitlogin", method = RequestMethod.POST)
public String checkLoginDetails(@ModelAttribute("login") LoginBean loginBean, BindingResult result) {

    if (service.validate(loginBean)) {
        //model.addAttribute("userName", " " + loginBean.getUserName());
        return "success";
    }

    return "invalid";
}

But I'm not allowed to add other parameters to the function.I need to pass userName to the "success.jsp" using Spring. I'm new to spring,plz help me with this.

You are missing a final Model model in your method signature. Therefore your signature should look like following:

public String checkLoginDetails(final Model model, @ModelAttribute("login") LoginBean loginBean, BindingResult result) 

Then you can use the Model parameter to add further attributes. For example:

model.addAttribute(ModelAttributeConstants.SUCCESSFUL, false);

or

model.addAttribute("successful", false);

Or to use your case

model.addAttribute("userName", " " + loginBean.getUserName());

I prefer the way using constants as I can use them in every place without thinking about the correct spelling. For sure you have to define you own constants class and names:)

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