Spring MVCに付属するフォームタグは比較的単純であり、ELとJSTLの助けを借りて完了する必要があることがよくあります。
これは比較的単純なフォーム送信ページ関数です:
1。ユーザーモデル
パッケージcom.my.controller.bean; Import java.util.date; import java.util.list; import javax.validation.constraints.future; import javax.validation.constraints.max; Import javax.validation.constraints.min; Import javax.validation.constraints.notnull; org.hibernate.validator.constraints.email; Import org.hibernate.validator.constraints.length; Import org.hibernate.validator.constraints.notempty; public classユーザー{private long id; @length(min = 2、max = 50、message = "user name length range = 2-50")private string name; @future(message = "時間は今日よりも短くできません")private date createTime; @notempty(message = "顧客は空にすることはできません")プライベートリスト<Customer>顧客; @notnull(message = "girl ca n't ve veemy")private boolean girl;プライベート文字列[] cbx; @notnull(message = "年齢はnull")@min(value = 18、message = "最低18歳")@max(value = 100、message = "Maximum 100 yeger")private int age; @email(message = "電子メール形式は間違っています")プライベート文字列メール。 public long getId(){return id; } public void setid(long id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; } public date getCreatetime(){return createTime; } public void setCreatetime(date createTime){this.createtime = createTime; } public list <Customer> getCustomers(){return Customers; } public void setcustomers(list <customer> customers){this.customers = customers; } public boolean isgirl(){return girl; } public void setgirl(boolean girl){this.girl = girl; } public string [] getcbx(){return cbx; } public void setcbx(string [] cbx){this.cbx = cbx; } public int getage(){return age; } public void Setage(int age){this.age = age; } public string getemail(){return email; } public void setemail(string email){this.email = email; }} 2。コントローラー
パッケージcom.my.controller; import java.util.arraylist; import java.util.date; import java.util.list; import javax.validation.valid; import org.springframework.stereotype.controller; import org.springframework.validation.validation; Import; Import; org.springframework.validation.fielderror; import org.springframework.web.bind.annotation.modelattribute; Import org.springframework.web.bind.annotation.requestmapping; Import org.springframework.web.bind.notation.Requestmethed.Bindation.Requestmethod; org.springframework.web.servlet.modelandview; Import com.my.controller.bean.customer; Import com.my.my.controller.bean.user;@controller@requestmapping(value = "/post")public class testpostcontroller {private static <ユーザー> = new Arraylist <ユーザー> { //----------------------------------------------- // 设置Entity // ----------------------------------------------- users.add(new User());ユーザーユーザー= users.get(0); user.setId(1); user.setName( "Robin"); user.setcreatetime(new date()); user.setgirl(true); user.setcbx(new String [] {"1"、 "2"、 "3"}); user.setage(18); user.setemail( "[email protected]"); user.setCustomers(new ArrayList <Customer>()); CustomerCustomer1 = new Customer(); customer1.setid(1); customer1.setcompany( "company -1"); customer1.setcreatetime(new date()); customer1.setuser(user); user.getCustomers()。add(customer1);顧客customer2 = new Customer(); customer2.setid(1); customer2.setcompany( "Company -2"); customer2.setcreatetime(new date()); customer2.setuser(user); user.getCustomers()。add(customer2); } @RequestMapping public ModelandView index(){modelandView View = new ModelAndView( "testPost/index"); view.addobject( "users"、users);戻りビュー。 } @RequestMapping(value = "/adduser"、method = requestmethod.post)public modelandview adduser(@modelattribute @valid user user、bindingResult result){modelandview view = new ModelAndView( "Redirect:/post"); if(result.haserrors()){list <fielderror> errors = result.getFielderrors(); for(fielderror err:errors){system.out.println( "objectname:" + err.getobjectname() + "/tfieldname:" + err.getfield() + "/tfieldValue:" + err.getRejectedValue() + "/tmessage:" + err.efaultMessage; } view.addobject( "users"、users);戻りビュー。 } user.setId(users.size() + 1); user.getCustomers()。get(0).SetID(1); user.getCustomers()。get(0).setuser(user); users.add(user); view.addobject( "users"、users);戻りビュー。 }} 3。表示
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@ page import = "com.my.controller.bean。 uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt"%> <%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "" fn "%> < uri = "http://www.springframework.org/tags"%> <%@ taglib uri = "http://www.springframework.org/tags/form" freix = "sf"%> <!doctype html public "// w3c/dtd html 4.01 "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv =" content-type "content =" text/html; charset = utf-8 "> <title> index </</head> <ffmt"/>>>>>>>>> < action = "<st:url value ="/post/adduser "> </st:url>" method = "post"> <c:foreach items = "$ {users}" var = "user"> user:$ {user.name} <br/> create time:<fmt:formatdate value = "$ {user}"/<c:<c:<c:<c:<c:<c: test = "$ {user.girl}"> yes </c:when> <c:when> <c:test = "$ {!user.girl}"> no </c:when> <c:rething> n/a </c:rething> </c:chooks> <br/> checkboxs:<c:foreachアイテム= "$ {ユーザー。年齢:$ {user.age} <br/>電子メール:$ {user.email} <br/> <hr/> <table> <thead> <tr style = "text-align:left;バックグラウンドカラー:#eee; "> <th> company名</th> <th> user </th> <th> create time </th> </tr> </thead> <tbody> <c:foreachアイテム=" $ {user.customers} <td> <fmt:formatdate value = "$ {item.createtime}" pattern = "yyyy-mm-dd"/>> </td> </td> </c:foreach> </tbody> </table> <hr/> </c:foreach>ユーザー名>ユーザー名:<入力タイプ= "Text" name "/< name = "girl" id = "isgirl" value = "true" checked "/> <label for =" isgirl "> yes </label> <input type =" radio "name" name "name" name "=" nogirl "value =" false "/> <ラベル=" nogirl "> no </label> <br/ /> <label for = "cbx1"> 1 </label> <入力タイプ= "チェックボックス" name = "cbx" id "cbx2" value = "2"/> <ラベル= "cbx2"> 2 </label> <入力タイプ= "チェックボックス" name = "cbx" id "id =" cbx3 "cbx3" type = "text" name = "age" id = "age" /> <br/> e-mail:<input type = "text" name "id" id = "email" /> <br/>作成時間:<入力タイプ= "テキスト" name = "createtime" id = "createTime" /> <br/> company:<入力タイプ= "テキスト=" customer " <入力型= "submit" value = "add"/> <sf:errors path = "*"> </sf:errors> </form> <hr // </body> </html>4。テスト結果
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。