0

I want to login with popup in Spring MVC.I use bootstrap for this.So I can open popup.But when I start app I am getting "either BindingResult nor plain target object for bean name 'loginModel' available as request attribute" error.login.jsp is a popup that I included into my index.jsp page.

<body>
<form:form method="post" action="login" modelAttribute="loginModel">
    <table>
        <tr>
            <td>Kullanıcı Adı:</td>
            <td><form:input type="text" path="username" /></td>
            <td><form:errors path="username" cssClass="red" /></td>
        </tr>
        <tr>
            <td>Şifre:</td>
            <td><form:input type="password" path="password" /></td>
            <td><form:errors path="password" cssClass="red" /></td>
        </tr>
    </table>
    <input type="submit" />
    <input type="reset" />
</form:form>
</body>

and here is my controller class;

@Controller
public class LoginPageController {

@RequestMapping(value = "/popupPages/login", method = RequestMethod.GET)
public ModelAndView displayLogin(HttpServletRequest request,
    HttpServletResponse response) {
ModelAndView model = new ModelAndView("login");
model.addObject("loginModel", new LoginModel());
return model;
}

@RequestMapping(value = "/popupPages/login", method = RequestMethod.POST)
public ModelAndView actionLogin(HttpServletRequest request,
    HttpServletResponse response,
    @ModelAttribute("loginModel") LoginModel loginModel) {
ModelAndView model = new ModelAndView();
String username = loginModel.getUsername();
String password = loginModel.getPassword();
if (username.isEmpty() || password.isEmpty()) {
    model.addObject("error", "Kullanıcı adı veya şifre boş geçilemez!");
} else {
    // TODO: Bu kısımda kontrol yapılacak olup kontrole göre yönlendirme
    // yapılacaktır.
}
return model;
}

}

Is there any wrong ?

emreturka
  • 846
  • 3
  • 18
  • 44

1 Answers1

0

Try using the following

@RequestMapping(value = "/popupPages/login", method = RequestMethod.GET)
public String displayLogin(HttpServletRequest request, HttpServletResponse response) {
  return "login";
}


@RequestMapping(value = "/popupPages/login", method = RequestMethod.POST)
public ModelAndView actionLogin(HttpServletRequest request, HttpServletResponse response, LoginModel loginModel) {

Also, your jsp should be like

<body>
  <form:form method="post" action="${pageContext.request.contextPath}/popupPages/login" commandName="loginModel">
    -----
  </form:form>
</body>

For info kindly read this.

Community
  • 1
  • 1
Pallav Jha
  • 3,409
  • 3
  • 29
  • 52