struts2 基本校验
Struts2对输入校验这方面采用的最基本方法是在每个Action里继承ActionSupport类,并且重写它的输入校验方法validate()。本示例中的RegisterAction代码中也显示,根据页面上输入的各种校验将所有不符合输入校验规则的错误信息都由ActionSupport类中另一个方法addFieldError方法将错误信息加入到表单错误信息,并且在输入数据的页面显示,而不会再由Action导航到注册成功页面。struts.xml也定义了1个名字为“input”的result,它表明将所有输入失败的错误信息导航到一个特定页面。还是将这个特定页面定义为数据输入的页面!
package com.example.struts.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
// Action类公用私有变量,用来做页面导航标志
private static String FORWARD = null;
// 用户名属性
private String username;
// 年龄属性
private int age;
private String password;
private String repassword;
private String mobile;
private Date birthday;
// 取得用户名值
public String getUsername() {
return username;
}
// 设置用户名值
public void setUsername(String username) {
this.username = username;
}
// 取得年龄值
public int getAge() {
return age;
}
// 设置年龄值
public void setAge(int age) {
this.age = age;
}
// 执行注册方法
public String execute() throws Exception {
FORWARD = "success";
return FORWARD;
}
// 校验方法,用来输入校验
public void validate() {
// 校验是否输入用户名
if (getUsername() == null || getUsername().trim().equals("")) {
addFieldError("username", "请输入用户名");
}
// 校验是否输入生日
if(getBirthday()==null){
addFieldError("birthday", "请输入生日日期");
}else
// 校验是否输入正确的生日日期
if(getBirthday().after(new Date())){
addFieldError("birthday", "请不要输入未来日期");
}
// 校验是否输入密码
if (getPassword() == null || getPassword().trim().equals("")) {
addFieldError("password", "请输入密码");
}
// 校验是否输入确认密码
if (getRepassword() == null || getRepassword().trim().equals("")) {
addFieldError("repassword", "请输入确认密码");
}
// 校验输入的密码和确认密码是否一致
if (!getPassword().equals(getRepassword())) {
addFieldError("repassword", "确认密码和密码输入不一致");
}
// 校验输入的手机号码长度是否正确
if (getMobile().length()!=11) {
addFieldError("mobile", "请输入正确的手机号码");
}
// 校验输入的年龄是否正确
if (getAge()<1||getAge()>99) {
addFieldError("age", "请输入您的真实年龄");
}
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
<struts>
<!-- Action所在包定义 -->
<package name="C08.1.1" extends="struts-default">
<!-- Action名字,类以及导航页面定义 -->
<!-- 通过Action类处理才导航的的Action定义 -->
<action name="Register"
class="action.RegisterAction">
<result name="input">/jsp/register.jsp</result>
<result name="success">/jsp/success.jsp</result>
</action>
<!-- 直接导航的的Action定义 -->
<action name="index" >
<result >/jsp/register.jsp</result>
</action>
</package>
</struts>
输入校验的数据输入JSP文件:
<!-- 用户信息注册form表单 -->
<s:form action="Register">
<table width="60%" height="76" border="0">
<!-- 各标签定义 -->
<s:textfield name="username" label="用户名"/>
<s:password name="password" label="密 码" />
<s:password name="repassword" label="密 码确认" />
<s:textfield name="birthday" label="生日"/>
<s:textfield name="mobile" label="手机号码"/>
<s:textfield name="age" label="年龄"/>
<s:submit value="注册" align="center"/>
</table>
</s:form>
validateXXX方法进行输入校验
// 执行注册方法
public String Register() throws Exception {
FORWARD = "success";
return FORWARD;
}
// 校验方法,用来输入校验
public void validateRegister() {
// 校验是否输入用户名
if (getUsername() == null || getUsername().trim().equals("")) {
addFieldError("username", "请输入用户名");
}
// 校验是否输入生日
if(getBirthday()==null){
addFieldError("birthday", "请输入生日日期");
}else
// 校验是否输入正确的生日日期
if(getBirthday().after(new Date())){
addFieldError("birthday", "请不要输入未来日期");
}
// 校验是否输入密码
if (getPassword() == null || getPassword().trim().equals("")) {
addFieldError("password", "请输入密码");
}
// 校验是否输入确认密码
if (getRepassword() == null || getRepassword().trim().equals("")) {
addFieldError("repassword", "请输入确认密码");
}
// 校验输入的密码和确认密码是否一致
if (!getPassword().equals(getRepassword())) {
addFieldError("repassword", "确认密码和密码输入不一致");
}
// 校验输入的手机号码长度是否正确
if (getMobile().length()!=11) {
addFieldError("mobile", "请输入正确的手机号码");
}
// 校验输入的年龄是否正确
if (getAge()<1||getAge()>99) {
addFieldError("age", "请输入您的真实年龄");
}
}
<!-- fielderror标签显示所有校验错误信息 -->
<s:fielderror></s:fielderror>
<!-- 用户信息注册form表单 -->
<s:form action="Register!Register.action">
<table width="60%" height="76" border="0">
<!-- 各标签定义 -->
<s:textfield name="username" label="用户名"/>
<s:password name="password" label="密 码" />
<s:password name="repassword" label="密 码确认" />
<s:textfield name="birthday" label="生日"/>
<s:textfield name="mobile" label="手机号码"/>
<s:textfield name="age" label="年龄"/>
<s:submit value="注册" align="center"/>
</table>
</s:form>
validate方法是对所有Action中方法的输入校验都进行校验,validateRegister方法只对Register方法进行校验。因此两者不能重复使用,都使用会造成两个方法都进行了校验的结果。执行顺序是先validateRegister后validate。如果validateRegister方法有特殊的输入校验则就会被validate方法“覆盖
首先第1个“Register”是RegisterAction中的方法名,一定要和方法名写成一样。而在“!”后的“Register”则是在struts.xml配置文件中定义的RegisterAction的映射里的“name”内容。黑体的内容表明该表单的Action是执行RegisterAction中的Register方法。如果在页面中直接写“!”后面的内容则表示执行的是RegisterAction中的execute方法
l 查找Action中是否有validateXXX方法。如果有则执行该方法。将校验产生的错误信息放置到ActionContext对象中。
l 查找Action中是否有validate方法。如果有则执行该方法。将校验产生的错误信息放置到ActionContext对象中。
l 查找视图界面是否有fielderror标签定义。如果有则返回到result为“input”的视图。同时ActionContext对象中有关的输入校验的错误信息也显示在该视图中。
弱弱问下,如果我不用struts的标签<s:form而用html自带的<form action='aa.action',这样还可以验证(执行validate方法)不?
前台页面验证始终是有弊端的。。。。
呵呵,这样写的话为什么不在前台页面就验证了呢?
还用这种方式验证?难道不知道可以使用xml配置或者annotation配置验证吗??
利用struts2进行验证,如何将其提示放在右边呢?
像这些用户名密码不能为空、长度限制等等的校验 还是用struts2的校验框架方便 且容易管理 不然在action中写那么多代码 杂而乱
JS是可以被关闭的,不安全~
学习了,原来不知到validate()对所有方法都校验,以为知识校验execute()
还是觉得尽可能用JS在客户端验证更好点。毕竟可以在JS里控制错误的提示样式或其它的。
actionName!methodName.action
:oops:
写的很基础的东西,楼上说滴对。
最后三句话可以看