Spring MVC form submitting

2016-10-25  本文已影响13人  ibyr

1.form in login.jsp

<form action="${pageContext.request.contextPath}/admin/result" method="post">    
用户名:<input type="text" name="userName">  
  <br>    
密 码:<input type="password" name="password"> 
   <br>    
<p id="error"> </p>    
<br/>    
<input type="submit" value="登录"/>    
<input type="reset" value="重置"/></form>

2.controller

@Controller
@RequestMapping("/admin")
public class LoginController {    
    //log4j    
    private static Logger logger = Logger.getLogger(LoginController.class);    

    @RequestMapping("/login")    
    public String login(){      
        logger.debug("login method..");        
        return "login";    
    }    

    @RequestMapping(value="/result", method= RequestMethod.POST)    
    public String loginResult(User user){       //User is a javabean
        logger.debug("loginResult method...");        
        logger.debug("loginResult: userName=" + user.getUserName() 
            + "; password=" + user.getPassword());        
        return "main";    
    }
}

3.javabean -- User

public class User {    
     // attributes must be with form data name.
    private String userName;    
    private String password;    

    // getter && setter
}
上一篇下一篇

猜你喜欢

热点阅读