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

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的配置
<?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>
	<package name="default" extends="struts-default">
	
		<interceptors>
			<interceptor name="replace" class="tutorial.TextInterceptor"></interceptor>
		</interceptors>
		
	<!-- Action名字,类以及导航页面定义 -->
		<!-- 通过Action类处理才导航的的Action定义 -->
		<action name="Register" class="tutorial.RegisterAction">
		<!-- 配置拦截器 -->
		<interceptor-ref name="validationWorkflowStack"/>
			<result name="input">reg.jsp</result>
			<result name="success">ok.jsp</result>
			<interceptor-ref name = "defaultStack"></interceptor-ref>
			<interceptor-ref name = "replace"></interceptor-ref>
		</action>
	</package>
</struts>    



reg.jsp 
 <body>
    	<h1><s:text name="title"></s:text></h1>
    	<form name="form1" method="post" action="Register">
    		<s:textfield name="username" label="%{getText('Name')}"></s:textfield><br/>
    		<s:textfield name="birthday" label="%{getText('Birthday')}"></s:textfield><br/>
    		<s:textfield name="height" label="%{getText('Height')}"></s:textfield><br/>
    		<label ><s:text name="Sex"/></label>
    		<input type="radio" name="sex" value="male" checked="checked" ><s:text name="male"/>
    		<input type="radio" name="sex" value="female"><s:text name="female"/><br/>
    		<s:select label = "%{getText('Hobby')}" list="{'其他','文字','体育'}" name="hobby"></s:select><br/>
    		<input type="submit"name="submit" value='<s:text name="submit"/>' >
    	</form>
  </body>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值