好长时间没做struts2项目了,最近在做的时候在带select的数据校验这块居然卡壳了,所以写了个小例子,收藏起来。
用到的jar:
jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!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"> <style type="text/css"> .errorMessage{ color: red; } </style> <title>添加人员</title> </head> <body> <h3>添加人员</h3> <s:fielderror></s:fielderror> <s:action name="initBooks" var="initBooks" /> <s:action name="initCities" var="initCities" /> <s:form id="form" namespace="/person" action="save" method="post"> 编号:<input type="text" id="personId" name="person.id" value="${person.id }"/></br> 姓名:<input type="text" id="personName" name="person.name" value="${person.name }"/></br> <s:select list="#initCities.cities" id="cityId" name="person.city.id" listKey="id" listValue="name" label="籍贯" value="%{person.city.id}"> </s:select> <s:select list="#initBooks.books" id="bookId" name="bookIds" listKey="id" listValue="name" label="读过的书" multiple="true"> </s:select> <input type="submit"/> <input type="reset"/> </s:form> </body> </html>
Action代码:
package org.penguin.study.struts2.action; import java.util.ArrayList; import java.util.List; import org.penguin.study.struts2.model.Book; import org.penguin.study.struts2.model.City; import org.penguin.study.struts2.model.Person; import org.penguin.study.struts2.service.BookService; import org.penguin.study.struts2.service.CityService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; public class PersonAction extends ActionSupport { private int personId; private Person person; private List<Integer> bookIds; private List<Book> books; private List<City> cities; private BookService bookService = new BookService(); private CityService cityService = new CityService(); private List<Person> persons = new ArrayList<Person>() { { City shiJiaZhuang = new City(40, "秦皇岛"); List<Book> readedBooks = new ArrayList<Book>(); Book thinkInJava = new Book(100, "Java编程思想第四版"); Book springInAction = new Book(400, "Spring In Action第二版"); Book coreJava1 =new Book(200, "核心Java卷一第八版"); readedBooks.add(springInAction); readedBooks.add(thinkInJava); readedBooks.add(coreJava1); Person yingHuaCheng = new Person(1000, "程英华"); yingHuaCheng.setCity(shiJiaZhuang); yingHuaCheng.setBooks(readedBooks); add(yingHuaCheng); } }; public String initCities() { cities = cityService.getAll(); return SUCCESS; } public String initBooks(){ books = bookService.getAll(); return SUCCESS; } public String create() { System.out.println("create执行了"); cities = cityService.getAll(); books = bookService.getAll(); return SUCCESS; } public String save() { System.out.println("save执行了"); System.out.println(person); return SUCCESS; } public String edit() { System.out.println("edit执行了"); if (personId != 0) { person = persons.get(0); } List<Book> readedBooks = person.getBooks(); if(readedBooks != null){ bookIds= new ArrayList<Integer>(); for(Book b: readedBooks){ bookIds.add(b.getId()); } } return SUCCESS; } public String view() { System.out.println("view执行了"); return SUCCESS; } public int getPersonId() { return personId; } public void setPersonId(int personId) { this.personId = personId; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public List<Book> getBooks() { return books; } public void setBooks(List<Book> books) { this.books = books; } public List<Integer> getBookIds() { return bookIds; } public void setBookIds(List<Integer> bookIds) { this.bookIds = bookIds; } public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } public List<City> getCities() { return cities; } public void setCities(List<City> cities) { this.cities = cities; } public BookService getBookService() { return bookService; } public void setBookService(BookService bookService) { this.bookService = bookService; } public CityService getCityService() { return cityService; } public void setCityService(CityService cityService) { this.cityService = cityService; } }
校验文件:PersonAction-save-validation.xml
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="person.id"> <field-validator type="int"> <param name="min">10</param> <param name="max">1000</param> <message>编号必须在整数 ${min}和${max}之间</message> </field-validator> </field> <field name="person.name"> <field-validator type="requiredstring"> <message>人员姓名必须录入</message> </field-validator> </field> </validators>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.action.extension" value="do" /> <package name="example" extends="struts-default" namespace="/person"> <action name="create" class="org.penguin.study.struts2.action.PersonAction" method="create"> <result>/person/create.jsp</result> </action> <action name="save" class="org.penguin.study.struts2.action.PersonAction" method="save"> <result name="input">/person/create.jsp</result> <result name="success">/person/view.jsp</result> </action> <action name="edit" class="org.penguin.study.struts2.action.PersonAction" method="edit"> <result>/person/create.jsp</result> </action> <action name="view" class="org.penguin.study.struts2.action.PersonAction" method="view"> <result>/person/view.jsp</result> </action> <action name="initCities" class="org.penguin.study.struts2.action.PersonAction" method="initCities"> <interceptor-ref name="basicStack" /> </action> <action name="initBooks" class="org.penguin.study.struts2.action.PersonAction" method="initBooks"> <interceptor-ref name="basicStack" /> </action> </package> </struts>