日期:2014-05-16  浏览次数:20323 次

validator的使用,jsp回显提示信息
UserAction.java
package com.rs.struts2.web.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.jgroups.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rs.struts2.model.User;
/**
 * @author 
 * @version 1.0
 * 
 * */
public class UserAction extends ActionSupport implements ModelDriven<User>{
	//struts2会自动new一个user对象,只需要给它setter和getter
	private User user=new User();
	private int a = 0;
	
	public String add(){
		addFieldError("add_success",getText("add.success"));
		addActionError("成功了");
		addActionMessage("成功了2");
		return "success";
	}
	public String addUser(){
		System.out.println("___________________");
		addFieldError("add_success",getText("add.success"));
		addActionError("成功了");
		addActionMessage("成功了2");
		return "success";
	}
	
	public String update(){
		addFieldError("update_success",getText("update.success"));
		return "success";
	}
	public String del(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		ArrayList list = new ArrayList();
		for (int i = 0; i <10; i++) {
			list.add(i);
		}
		
		request.setAttribute("name", "zheng-gang");
		request.setAttribute("list", list);
		request.setAttribute("number1", 10);
		
		addFieldError("del_success",getText("del.success"));
		return "success";
	}
	
	public String list(){
		addFieldError("list_success",getText("list.success"));
		return "success";
	}
	public User getModel() {
		return this.user;
	}
}



UserAction-validation.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE validators PUBLIC
 "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
 "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators> 
<field name="username">  
   <field-validator type="requiredstring">  
    <param name="trim">true</param>  
    <message key="requiredadd.name"/> 
   </field-validator>
</field>
<field name="pwd">
<field-validator type="requiredstring">
	<param name="trim">true</param>
	<message key="requiredadd.password"></message>
</field-validator>
</field>
</validators>



ResourceMessages.properties
requiredadd.name = 用户名不能为空
requiredadd.password = 密码不能为空
add.success= 添加成功


struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	 <constant name="struts.devMode" value="true" />
	 <constant name="struts.custom.i18n.resources" value="ResourceMessages" />
	 
	 <package name="User" namespace="/user" extends="struts-default">
        <action name="User_*" class="com.rs.struts2.web.action.UserAction" method="{1}">
        	<!-- 只允许add方法和update方法使用校验器 -->
        	<interceptor-ref name="defaultStack">
        		<param name="validation.excludeMethods">*</param>
        		<param name="validation.includeMethods">add,update,addUser</param>
        	</interceptor-ref>
        	<result name="success">/user_success.jsp</result>
        	<result name="input">/addUser.jsp</result>
        </action>
    </package>
</struts>




jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+reques