struts2 validation入门

好长时间没做struts2项目了,最近在做的时候在带select的数据校验这块居然卡壳了,所以写了个小例子,收藏起来。

用到的jar:

  1. commons-fileupload-1.2.1.jar
  2. commons-io-1.3.2.jar
  3. freemarker-2.3.15.jar
  4. jstl.jar
  5. ognl-2.7.3.jar
  6. standard.jar
  7. struts2-core-2.1.8.1.jar
  8. xwork-core-2.1.6.jar

jsp页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>





添加人员


	

添加人员

编号:
姓名:
 

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 bookIds;
	private List books;
	private List cities;
	private BookService bookService = new BookService();
	private CityService cityService = new CityService();
	private List persons = new ArrayList() {
		{
			City shiJiaZhuang = new City(40, "秦皇岛");
			
			List readedBooks = new ArrayList();
			
			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 readedBooks = person.getBooks();
		if(readedBooks != null){
			bookIds= new ArrayList();
			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 getBooks() {
		return books;
	}

	public void setBooks(List books) {
		this.books = books;
	}

	public List getBookIds() {
		return bookIds;
	}

	public void setBookIds(List bookIds) {
		this.bookIds = bookIds;
	}

	public List getPersons() {
		return persons;
	}

	public void setPersons(List persons) {
		this.persons = persons;
	}

	public List getCities() {
		return cities;
	}

	public void setCities(List 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




	
		
			10
			1000
			编号必须在整数 ${min}和${max}之间
		
	
	
		
			人员姓名必须录入
		
	

  struts.xml





	
	

		
			/person/create.jsp
		
		
			/person/create.jsp
			/person/view.jsp
		
		
			/person/create.jsp
		
		
			/person/view.jsp
		
		
			
		
		
			
		
	

 

 

你可能感兴趣的:(Struts2)