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>
分享到:
相关推荐
Struts2输入校验总结 Struts2输入校验
struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置...
struts2的输入校验有两种方式: 一.重写validate()方式 二.采用配置文件的方式 先我们就以一个简单的登录的例子来讲解这个吧. 重点讲解下,第二个采用配置文件的方式: 1.添加一个xml的校验文件,保存在...
struts2输入校验 对指定方法进行输入校验
Struts2输入校验,Struts2提供了功能强大的输入校验机制,通过Struts2内建的输入校验器,应用无需书写任何代码,即可完成大部分的校验功能,并可以同时完成客户端和服务器端的校验。
使用struts2进行输入校验入门 (附图)
·Struts2配置文件介绍 struts.xml action package ·Struts2配置文件介绍 struts.xml action package
熟悉和掌握Struts2内建校验的操作方法 实验环境与设备: 已经接入局域网的网络实验室,机器上装有IE浏览器等; strtus2.5.8 实验内容: (1) 编写一个自定义类型转换器,并基于应用程序进行全局...
第六章_struts2的国际化与输入校验,主要介绍struts2的国际化与输入校验。
struts2核心配置文件,struts2.xml和struts2.properties
struts2开发的struts.xml配置文件配置以及web.xml配置说明
配置SSH框架时关于Struts2的配置文件
Struts应用采用两个基于XML配置文件来配置,分别是web.xml和struts-cofig.xml文件.web.xml文件 是配置所有web应用而struts-config.xml文件是struts专用配置文件,事实上也是可以根据需要给这个配置文件 起其他名称.
Struts2的配置文件的详细说明,写的好不错!
NULL 博文链接:https://chaoyi.iteye.com/blog/2161948
struts.xml配置文件详解struts.xml配置文件详解struts.xml配置文件详解
struts2输入校验总结,struts2自带的校验器,以及校验器不能做的check的处理的总结