1, direct mapping parameters to form POJO through annotation ModelAttribute. In action from the write path submission, write the name of the input parameters of the name.
package com.demo.model; public class user { private String username; private String password; private int nsex; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void setNsex(int nsex) { this.nsex = nsex; } public int getNsex() {return nsex;} }
<%-- Created by IntelliJ IDEA. User: wym Date: 2019/10/8 Time: 23:17 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login</title> </head> <body> <form action="${pageContext.request.contextPath}/login" method="post"> 用户名:<input type="text" name="username"/> < Password:>br> <br<input type="password" name="password"/> <br><br> <input type="submit" value="提交"/> </form> </body> </html>
package com.demo.controller; import com.demo.model.user; import com.demo.service.Userservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.servlet.http.HttpSession; @Controller public class LoginController { @Autowired private Userservice userService; @RequestMapping(value="/login", method= RequestMethod.POST) public String hello(@ModelAttribute user u, HttpSession session){ session.setAttribute("user", u); user user = userService.findbyname(u.getUsername()); if(user == null) return "loginfail"; else if(!user.getPassword().equals(u.getPassword())) return "falsepaswd"; else return "helloworld"; } }
note! ! There's only input parameter name and the name of a member of the domain name pojo said exactly the same can be directly mapped by @ModelAttribute, otherwise parameter will not be assigned a default value in the manner of presentation.
2. clearly can not form the content acquisition is always a pojo property, entirely possible that occur separately. Then you can use @RequestParam acquisition parameters