Spring MVC와 함께 제공되는 양식 태그는 비교적 간단하며 종종 EL 및 JSTL의 도움으로 완료해야합니다.
다음은 비교적 간단한 양식 제출 페이지 기능입니다.
1. 사용자 모델
package com.my.controller.bean; import java.util.date; import java.util.list; import javax.validation.constraints.future; import javax.validation.constrints.max; import javax.validation.constrants.min; import javax.valid org.hibernate.validator.constraints.email; import org.hibernate.validator.constraints.length; import org.hibernate.validator.constraints.notempty; public class user {private long id; @length (min = 2, max = 50, message = "사용자 이름 길이 범위 = 2-50") 개인 문자열 이름; @Future (메시지 = "시간은 오늘보다 적을 수 없습니다") 개인 날짜 CreateTime; @notempty (메시지 = "고객이 비어질 수 없음") 개인 목록 <fustomer> 고객; @NotNull (메시지 = "소녀가 비어질 수 없다") 사립 부울 소녀; 개인 문자열 [] CBX; @NotNull (메시지 = "Age ="Age No Null ") @min (value = 18, message ="최소 18 세 ") @max (value = 100, message ="maximum 100 -old ") 개인 int 연령; @email (message = "이메일 형식이 잘못되었습니다") 개인 문자열 이메일; public long getid () {return id; } public void setId (long id) {this.id = id; } public String getName () {return name; } public void setName (문자열 이름) {this.name = 이름; } 공개 날짜 getCreateTime () {return createTime; } public void setCreateTime (Date CreateTime) {this.createTime = createTime; } public list <fustomer> getCustomers () {반환 고객; } public void setCustomers (list <fustomer> 고객) {this.Customers = 고객; } public boolean isgirl () {return girl; } public void setgirl (부울 소녀) {this.girl = girl; } public String [] getCBX () {return CBX; } public void setcbx (String [] cbx) {this.cbx = cbx; } public int getage () {반환 연령; } public void 설정 (int Age) {this.age = age; } public String getEmail () {반환 이메일; } public void seteMail (문자열 이메일) {this.email = 이메일; }} 2. 컨트롤러
package com.my.controller; import java.util.arraylist; import java.util.date; import java.util.list; import javax.validation.valid; import org.springframework.stereotyp.controller; import org.springframework.validation.bindingresult; org.springframework.validation.fielderror; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmeThod; org.springframework.web.servlet.modelandview; import com.my.controller.bean.customer; import com.my.controller.bean.user;@controller@requestmapping (value = "/post") public class testpostcontroller <Users = new arraylist <userraylist> (); { //----------------------------------------------- // 设置Entity // ----------------------------------------------- users.add(new User()); 사용자 user = user.get (0); user.setid (1); user.setName ( "Robin"); user.setcreateTime (새 날짜 ()); user.setgirl (true); user.setcbx (new String [] { "1", "2", "3"}); user.setage (18); user.seteMail ( "[email protected]"); user.setCustomers (new ArrayList <customer> ()); Customer Customer1 = 새로운 고객 (); customer1.setId (1); Customer1. SetCompany ( "회사 -1"); customer1.setcreateTime (new date ()); Customer1. SetUser (사용자); user.getCustomers (). add (customer1); Customer Customer2 = 새로운 고객 (); customer2.setId (1); customer2.setcompany ( "회사 -2"); customer2.setcreateTime (new date ()); customer2.setuser (사용자); user.getCustomers (). add (customer2); } @requestmapping public modelview index () {modelandView view = new ModelAndView ( "TestPost/Index"); view.addoBject ( "사용자", 사용자); 리턴 뷰; } @requestmapping (value = "/adduser", method = requestmethod.post) public modelandview addUser (@ModelAttribute @Valid 사용자, bindingResult result) {modelAndView = 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 :" + restaultMessage ()); } view.AdDoBject ( "사용자", 사용자); 리턴 뷰; } user.setId (user.size () + 1); user.getCustomers (). get (0) .setId (1); user.getCustomers (). get (0) .setuser (user); user.add (사용자); view.addoBject ( "사용자", 사용자); 리턴 뷰; }} 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"%>@ taglib prefix = "st" uri = "http://www.springframework.org/tags" %> < %@ taglib uri = "http://www.springframework.org/tags/forfix ="sf " %> <! doctype html public"-// w3c // dtd html 4.01 transitonal // " "http://www.w3.org/tr/tr/html4/loose.dtd"> html> html> http-quiv ="content-type "content ="text/html; charset = utf-8 "> <title> index </title> </head> <fmt : setlocale ="zh_cn action = "<st : url value ="/post/adduser "> </st : url>"method = "post"> <c : foreach items = "$ {user}"var = "user"> user : $ {user.name} <br/> 시간 : <fmt : formatdate value = "$ {user.createtime}"/> <br/ <c : <c : <c : <c : <c : test = "$ {user.girl}"> 예 </c : 언제> <c : test = "$ {! user.girl}"> no </c : when> <c : 그렇지 않으면> n/a </c : 그렇지 않으면> <br/> checkboxs : <c : foreach item = "$ {user.cbx}"Var = "item"> $ {<br}, </c :. Age : $ {user.age} <br/> 이메일 : $ {user.email} <br/> <hr/> <table> <tr style = "text-align : 왼쪽; 배경색 : #eee; "> <th> 회사 이름 </th> <th> user </th> <th> user </th> <th> 시간 생성 시간 </th> </tr> </thead> <tbody> <c : foreach items ="$ {user.customers} "var ="item "> <td> <td> $ {item.company}}} <td> <fmt : formatdate value = "$ {item.createTime}"pattern = "yyyy-mm-dd"/> </td> </td> </c : foreach> </tbody> </table> <hr/> </c : foreach> foreach> "text"name = "name"/> <br/>> <br/>> <bl/> <bl/ c : 이름 = "girl"id = "isgirl"value = "true"checked = "checked"/> <label for = "isgirl"> 예 </label> <입력 유형 = "radio"name "="girl "id ="nogirl "value ="false "/> <레이블에 대한 ="nogirl "> no </label> <br/> checkboxs : <input type ="checkx "cbx" "cbx" "cbx" value = "1" /> <label for = "cbx1"> 1 < /label> <input type = "checkbox"name = "cbx"id = "cbx2"value = "2" /> <label for = "cbx2"> 2 < /label> <input type = "checkbox"name = "cbx"id = "cbx3"value = "3" "cbx3"> <br/> age : <input type = "text"name = "age"id = "age"/> <br/> 이메일 : <input type = "text"name = "email"id = "email"/> <br/> <input type = "text"name = "createTime"id = "createTime"/> <br company : "name ="customer [0] ". id = "고객 [0] .company"/> <br/> <입력 유형 = "제출"value = "add"/> <sf : 오류 경로 = "*"> </sf : errors> </form> <hr // </html>4. 테스트 결과
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.