0

I have controller class with following request mapping method.

  • appStart() method is responsible for redirecting user to login.html and logout() is responsible for invalidating session and redirecting user back to login.jsp
  • if I remove @ModelAttribute from their parameter then these two methods are throwing exception, is there any hack to get these methods working without modelattribute?

controller methods.

@RequestMapping(value="/",method=RequestMethod.GET) 
     public String appStart(@ModelAttribute("tempAdmin") Admin tempAdmin) {
    return "login.jsp";
}

@RequestMapping(method = RequestMethod.POST,name="doLogin")
public ModelAndView doLogin(@ModelAttribute("tempAdmin") Admin tempAdmin, HttpServletRequest request) {

    ModelAndView mvc = new ModelAndView();

    /*
        Buisness logic
    */

    mvc.setViewName("home.jsp");

    return mvc;
}

@RequestMapping("doLogout")
public String logout(HttpServletRequest request) {

    HttpSession session = request.getSession(false);
    if(session != null){
        session.invalidate();
    }

    return "login.jsp";
}

login.jsp

    <form:form action="doLogin" modelAttribute="tempAdmin" cssClass="form-horizontal">
      <div class="form-group">
          <label for="username" class="col-sm-2 control-label">Username</label>
       <div class="col-sm-10">
          <form:input cssClass="form-control" path="adminId" placeholder="username" />
        </div>
     </div>
    <div class="form-group">
         <label for="passwd" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
         <form:password path="password" cssClass="form-control" id="passwd" placeholder="password" />
    </div>
   </div>  
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Sign in</button>
     </div>
   </div>
   </form:form>

stacktrace.

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'tempAdmin' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
  • There has to be an object named `tempAdmin` in the model, when using `@ModelAttribute` that is done automatic, else you have do it manual. So it isn't a hack it is basically how it is supposed to work. – M. Deinum Sep 23 '15 at 07:08
  • return ModelAndView from other two methods i.e. `appStart and logOut` with newly created tempAdmin as modelAttribute – bsingh Sep 23 '15 at 07:41
  • @M.Deinum But appStart() is to handle context-root and I am neither sending any data with a request and nor adding it in response. btw it is working fine with simple HTML form tags. – Govinda Sakhare Sep 23 '15 at 08:10
  • Ofcourse it is, but spring will do data binding and requires an underlying form object for that. – M. Deinum Sep 23 '15 at 08:18
  • Not 100% sure, but it is worth a try adding BindingResult to your method signature. It must be added right after the ModelAttribute: public ModelAndView doLogin(@ModelAttribute("tempAdmin") Admin tempAdmin, BindingResult bindingResult, HttpServletRequest request) { – Fritz Duchardt Sep 23 '15 at 09:38

1 Answers1

1

I will tell you how to change your controller, to avoid binding result problem. Try this :

@RequestMapping(method = RequestMethod.POST,name="doLogin")
public String doLogin(@ModelAttribute("tempAdmin") Admin tempAdmin, HttpServletRequest request,Model model) {
    model.addAttribute("tempadmin",new Admin());
    // business logic
    return "home";
}

Try this out, and if you have any other classes, then add the model.addAttribute for that as well. Can you post your JSP too?

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • you have misunderstood my question.issue is with appStart() and logout() method why do I need modelAttribute there? btw doLogin() is working perfectly fine, – Govinda Sakhare Sep 23 '15 at 14:59
  • Forget the @ModelAttribute in your appStart() and logout(), rest remains same. And why are you posting that controller method then if it's working fine?? – We are Borg Sep 23 '15 at 15:01
  • dude I mean why do we even need that ModelAttribute in appStart() and logout() I binding nothing there and tempAdmin is useless there, these methods are for the redirecting purpose(redirecting to required jsp). I have posted doLogin() because tempAdmin is consumed by that method . – Govinda Sakhare Sep 23 '15 at 15:06
  • Like I said, FORGET the @ModelAttribute, and I am telling you to add a Model, not ModelAttribute, different stuff. Change the "tempadmin" to "tempAdmin". – We are Borg Sep 23 '15 at 15:07
  • Also, check http://stackoverflow.com/questions/32606019/how-correctly-use-the-spring-mvc-formselect-tag-to-show-the-value-of-a-specif/32607058#32607058 . Check the answer I have given, and check the main Post error... Arent they the same? you tell me. – We are Borg Sep 23 '15 at 15:09
  • okay but why do I need model? when i'm using simple html tags I dont need Model/Model attribute in these methods. what is happening in case of spring tags? – Govinda Sakhare Sep 23 '15 at 15:20
  • Kya mamu!!!! Because Spring has to construct an object from the data you gave in your JSP, this is just a way to instruct which Model class it is that you are referring to. A reading of Spring-tag libs is recommended. Also, kindly upvote the answer if it solved your problem. Thanks. Comment if you didnt understood Model. – We are Borg Sep 23 '15 at 15:24