美文网首页
Spring MVC form submitting

Spring MVC form submitting

作者: ibyr | 来源:发表于2016-10-25 17:31 被阅读13次

    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
    }
    

    相关文章

      网友评论

          本文标题:Spring MVC form submitting

          本文链接:https://www.haomeiwen.com/subject/sggruttx.html