Struts中利用配置文件进行输入校验,其实是挺简单的,只是时间长了有些基本语法容易淡忘。今天特意抽点时间来把这点东西写写(常用的验证),可以说也算是对这块知识的进一步的巩固吧!如果 以后在用到这块知识也可以翻出来看看。
1、对生日的验证:
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>date Validator Example</title> <style type="text/css">@import url(css/main.css);</style> <style> .errorMessage { color:red; } </style> </head> <body> <div id="global" style="width:350px"> <h3>Enter your birthdate</h3> <s:form action="Date2"> <s:textfield name="birthDate" label="Birth Date"/> <s:submit/> </s:form> </div> </body> </html>
package com.erong.struts.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class DateTestAction extends ActionSupport { private Date birthDate; public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } }
<!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="birthDate"> <field-validator type="date"> <param name="max">1/1/2000</param> <message> You must have been born before the year 2000 to register </message> </field-validator> </field> </validators>
2、Email地址的输入验证
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>email Validator Example</title> <style type="text/css">@import url(css/main.css);</style> <style> .errorMessage { color:red; } </style> </head> <body> <div id="global" style="width:350px"> <h3>Enter your email</h3> <s:form action="Email2"> <s:textfield name="email" label="Email"/> <s:submit/> </s:form> </div> </body> </html>
package com.erong.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class EmailTestAction extends ActionSupport {
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
<!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="email"> <field-validator type="email"> <message>Invalid email</message> </field-validator> </field> </validators>
3、表达式的输入验证
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>expression Validator Example</title> <style type="text/css">@import url(css/main.css);</style> <style> .errorMessage { color:red; } </style> </head> <body> <div id="global" style="width:400px"> <s:actionerror/> <h3>Enter the minimum and maximum temperatures</h3> <s:form action="Expression2"> <s:textfield name="min" label="Minimum temperature"/> <s:textfield name="max" label="Maximum temperature"/> <s:submit/> </s:form> </div> </body> </html>
package com.erong.struts.action; import com.opensymphony.xwork2.ActionSupport; public class ExpressionTestAction extends ActionSupport { private int min; private int max; public int getMax() { return max; } public void setMax(int max) { this.max = max; } public int getMin() { return min; } public void setMin(int min) { this.min = min; } }
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <validator type="expression"> <param name="expression"> max > min </param> <message> Maximum temperature must be greater than Minimum temperature </message> </validator> </validators>
4、整形数据的输入验证:
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>int Validator Example</title> <style type="text/css">@import url(css/main.css);</style> <style> .errorMessage { color:red; } </style> </head> <body> <div id="global" style="width:350px"> <h3>Enter a year</h3> <s:form action="Int2"> <s:textfield name="year" label="Year (1990-2009)"/> <s:submit/> </s:form> </div> </body> </html>
package com.erong.struts.action; import com.opensymphony.xwork2.ActionSupport; public class IntTestAction extends ActionSupport { private int year; public int getYear() { return year; } public void setYear(int year) { this.year = year; } }
<!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="year"> <field-validator type="int"> <param name="min">1990</param> <param name="max">2009</param> <message>Year must be between 1990 and 2009</message> </field-validator> </field> </validators>
5、URL地址的输入验证:
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>url Validator Example</title> <style type="text/css">@import url(css/main.css);</style> <style> .errorMessage { color:red; } </style> </head> <body> <div id="global" style="width:350px"> <h3>What is your website?</h3> <s:form action="Url2"> <s:textfield name="url" label="URL" size="40"/> <s:submit/> </s:form> </div> </body> </html>
package com.erong.struts.action; import com.opensymphony.xwork2.ActionSupport; public class UrlTestAction extends ActionSupport { private String url; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
<!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="url"> <field-validator type="url"> <message>Invalid URL</message> </field-validator> </field> </validators>
6、利用正则表达式进行输入的验证(这个方法比较灵活,不管是什么样的都能够通过正则表达式进行匹配):
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>regex Validator Example</title> <style type="text/css">@import url(css/main.css);</style> <style> .errorMessage { color:red; } </style> </head> <body> <div id="global" style="width:350px"> <h3>Enter a phone number</h3> <s:form action="RegEx2"> <s:textfield name="phone" label="Phone (xxx-xxx-xxxx)"/> <s:submit/> </s:form> </div> </body> </html>
package com.erong.struts.action; import com.opensymphony.xwork2.ActionSupport; public class RegExTestAction extends ActionSupport { private String phone; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
<!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="phone"> <field-validator type="regex"> <param name="expression"> <![CDATA[\d\d\d\-\d\d\d\-\d\d\d\d]]> </param> <message> Invalid phone number or invalid format </message> </field-validator> </field> </validators>