SpringMVC表單和HTML表單
阿新 • • 發佈:2018-11-02
SpringMVC表單和HTML表單
-
HTML表單
-
編寫action,返回adduser.jsp
@RequestMapping(value = "/addUser",method = RequestMethod.GET) public String addUser(ModelMap map){ return "add_user"; }
-
編寫action,用於接受引數並展示
@RequestMapping(value = "/result",method = RequestMethod.POST) public String result
-
編寫HTML表單(adduser.jsp)
<form action="result" method="get"> 名字:<input type="text" name="name"/> <br> 年齡:<
-
結果展示
-
-
SpringMVC表單
可以自動填充或者實現一些SpringMVC提供的功能
-
編寫實體類
public class User { private String name="m"; private int age=20; public String getName() { return name; }
-
編寫action,返回adduser.jsp
@RequestMapping(value = "/addUser",method = RequestMethod.GET) public String addUser(ModelMap map){ User user=new User(); user.setName("666"); map.addAttribute("user",user); return "add_user"; }
-
編寫action,用於接受引數並展示
@RequestMapping(value = "/result",method = RequestMethod.POST) public String result(ModelMap map, @RequestParam String name, @RequestParam int age){ map.addAttribute("name",name); map.addAttribute("age",age); return "result"; }
-
編寫SpringMVC表單(adduser.jsp)
<form:form action="result" method="post" modelAttribute="user"> 名字:<form:input path="name"/><br> 年齡:<form:input path="age"/><br> <input type="submit"> </form:form>
-
結果展示
-