拦截器——替换表单中的敏感词

RegistAction.java
package tutorial;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private Date birthday;
	private double height;
	private String sex;
	private String hobby;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}


	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	// 校验方法,用来输入校验
	public void validate() {
		// 校验是否输入用户名
		if (getUsername() == null || getUsername().trim().equals("")) {
			addFieldError("username",getText("nameNotNull"));
		}else
        //校验用户名长度
			if(username.length()<2||username.length()>50){
			addFieldError("username", getText("nameRange"));
		}
		// 校验是否输入密码
		if (getHeight()== 0.0) {
			addFieldError("height", getText("heightNotNull"));
		}
		//校验密码的组成与长度
		else if(height<100||height>250){
			addFieldError("height", getText("heightRange"));
		}
		
		
		DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");  
        Date myDate1 = null;
        Date myDate2 = null;
		try {
			myDate1 = dateFormat1.parse("1900-01-01");
			myDate2 = dateFormat1.parse("2020-12-31"); 
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		// 校验是否输入生日
		if (getBirthday() == null) {
			addFieldError("birthday", getText("birthdayNotNull"));
		} else
		// 校验是否输入正确的生日日期
		if (getBirthday().before(myDate1)||getBirthday().after(myDate2)) {
			addFieldError("birthday", getText("birthdayRange"));
		}
		
		
	}
}


 
  

TextInterceptor.java
package tutorial;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class TextInterceptor extends AbstractInterceptor{/** * */private static final long serialVersionUID = 1L;@Overridepublic String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stubObject object = invocation.getAction();if(object!=null){//判断object是否是Text的实例if(object instanceof RegisterAction){RegisterAction action = (RegisterAction)object;String username=action.getUsername();if(username.contains("obm")){username=username.replaceAll("obm", "***");action.setUsername(username);}return invocation.invoke();}else{return Action.LOGIN;}}return Action.LOGIN;}}
 
  
struts.xml的配置



	
	
		
			
		
		
	
		
		
		
		
			reg.jsp
			ok.jsp
			
			
		
	
    


 
  

reg.jsp 
 
    	








你可能感兴趣的:(struts)