2016-07-07 35 views
-1
<body> 
<jsp:useBean id="loginBean" class="com.ss.sms.bean.LoginBean"></jsp:useBean> 
    <div class="row"> 
     <div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 col-md-4 col-md-offset-4"> 
      <div class="login-panel panel panel-default"> 
       <div class="panel-heading">Log in</div> 
       <div class="panel-body"> 

        form:form id="loginForm" method="post" action="login" modelAttribute="login"> 
        <fieldset> 
         <div class="form-group"> 
          <input class="form-control" placeholder="UserId" name="userId" type="text" autofocus="autofocus" value=${loginBean.userName}> 
         </div> 
         <div class="form-group"> 
          <input class="form-control" placeholder="Password" name="password" type="password" value=${loginBean.password}> 
         </div> 
         <div class="checkbox"> 
          <label> 
           <input name="remember" type="checkbox" value="Remember Me">Remember Me 
          </label> 
         </div> 
         <input type="submit" class="btn btn-primary"> 
         <div style="color: red">${error}</div> 
        </fieldset> 
       </form:form> 
      </div> 
     </div> 
    </div><!-- /.col--> 
</div><!-- /.row -->  

@RequestMapping(value="/login",method = RequestMethod.POST) 
public ModelAndView login(Model md,@ModelAttribute("login")LoginBean loginBean) { 
    System.out.println("hello done"); 
    System.out.println(loginBean.getUserName()); 
    ModelAndView model = null; 
    if (loginBean != null && loginBean.getUserName() != null & loginBean.getPassword() != null) { 
     if (loginBean.getUserName().equals("santosh") && loginBean.getPassword().equals("[email protected]")) { 
      model = new ModelAndView("hello"); 
      return model; 
     } else { 
      model = new ModelAndView("login"); 
      return model; 
     } 
    } else { 
     model = new ModelAndView("login"); 
     return model; 
    } 
} 
+0

你可以添加說明你的代碼有什麼問題? –

回答

0

如果我從你的代碼正確理解你有問題看到頁面正確。

在你的ModelAndView你補充一點,視圖,而不是模型

所以,你需要添加的對象

因此,代碼可以看看這樣的事情

@RequestMapping(value="/login",method = RequestMethod.POST) public ModelAndView login(Model md,@ModelAttribute("login")LoginBean loginBean) { 
    System.out.println("hello done"); 
    System.out.println(loginBean.getUserName()); 
    ModelAndView model = null; 
    if (loginBean != null && loginBean.getUserName() != null & loginBean.getPassword() != null) { 
     if (loginBean.getUserName().equals("santosh") && loginBean.getPassword().equals("[email protected]")) { 
      //if the view hello expects an object add it here 
      model = new ModelAndView("hello"); 
      return model; 
     } else { 
      model = new ModelAndView("login").addObject("login",loginBean); 
      return model; 
     } 
    } else { 
     model = new ModelAndView("login").addObject("login",loginBean); 
     return model; 
    } 
} 
相關問題