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")); } } }
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;}}TextInterceptor.java
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>
拦截器——替换表单中的敏感词
最新推荐文章于 2022-07-18 10:53:04 发布