Struts2学习笔记3-数据输入校验

Struts2数据输入校验
首先是JSP页面,但没有使用Strust2的标签
<s:fielderror></s:fielderror>
	<form action="register.action" method="post">
		username:<input type="text" name="username"/><br/>
		password:<input type="password" name="password"/><br/>
		password:<input type="password" name="repassword"/><br/>
		age:<input type="text" name="age"/><br/>
		birthday:<input type="text" name="birthday"/><br/>
		graduation:<input type="text" name="graduation"/><br/>
		<input type="submit" value="submit"/>
	</form>


对应的Action extends ActionSupport来验证

package com.langhua.action;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {
	private String username;
	private String password;
	private String repassword;
	private int age;
	private Date birthday;
	private Date graduation;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Date getGraduation() {
		return graduation;
	}
	public void setGraduation(Date graduation) {
		this.graduation = graduation;
	}
	
	@Override
	public String execute() throws Exception {		
		return Action.SUCCESS;
	}
	
	@Override
	public void validate() {
		
		if(null == username){
			this.addFieldError("username", "用户名不能为空");
		}
		
		if(username.length()<6 || username.length()>10){
			this.addFieldError("username", "用户名长度不正确");
		}
		
		if(null == password || null == repassword){
			this.addFieldError("password", "密码不能为空");
		}else if(password.length()<6 || password.length()>10 || repassword.length()<6 || repassword.length()>10){
			this.addFieldError("password", "密码长度应该在6位到10位之间");
		}else if(!password.equals(repassword)){
			this.addFieldError("password", "两次输入的密码不正确");
		}
		//如果客户端输入的是字符串,自动转成0
		if(age<0 || age>150){
			this.addFieldError("age", "age不正确");
		}
		Calendar c1 = null;
		Calendar c2 = null;
		if(null != birthday){
			c1 = Calendar.getInstance();
			c1.setTime(birthday);
		}
		
		if(null != graduation){
			c2 = Calendar.getInstance();
			c2.setTime(graduation);
			if(!c2.after(c1)){
				this.addFieldError("birthday", "graduation is before birthday");
			}
		}		
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
}


配置文件
<action name="register" class="com.langhua.action.RegisterAction">
	<result name="success">/success.jsp</result>
	<result name="input">/register.jsp</result>
</action>


下面是使用struts2标签重写的
<s:form action="register">
		<s:textfield name="username" label="username"></s:textfield>
		<s:password name="password" label="password"></s:password>
		<s:password name="repassword" label="password"></s:password>
		<s:textfield name="age" label="age"></s:textfield>
		<s:textfield name="birthday" label="birthday"></s:textfield>
		<s:textfield name="graduation" label="graduation"></s:textfield>
		<s:submit value=" submit "></s:submit>
</s:form>


配置文件
<action name="register" class="com.langhua.action.RegisterAction">
	<result name="success">/success.jsp</result>
	<result name="input">/register2.jsp</result>
</action>


struts2标签的好处就是方便

struts2的国际化

全局国际化
在struts.xml里面配置
        <constant name="struts.custom.i18n.resources" value="message"></constant>

然后就可以在SRC目录新建一个message.properties文件(实际上会放到classes下面)
xwork.default.invalid.fieldvalue = {0} error


局部国际化
在和Action同目录的地方建一个RegisterAction.properties文件,文件名和Action同名
内容
invalid.fieldvalue.age = \u5e74\u9f84\u8f93\u5165\u4e0d\u6b63\u786e
invalid.fieldvalue.birthday = \u751f\u65e5\u683c\u5f0f\u4e0d\u6b63\u786e
invalid.fieldvalue.graduation = \u6bd5\u4e1a\u65e5\u671f\u683c\u5f0f\u4e0d\u6b63\u786e

这就是局部的转换失败之后的信息

使用JDK里面的native2ascii.exe将中文转为ASCII码
这样就支持中文了


Struts2有两种错误
this.addFieldError("username", "用户名不能为空");
this.addActionError("用户名不能为空");

但是struts2标签就不支持,如
<s:textfield name="username" label="username"></s:textfield>

就不会把错误显示出来
addActionError只能使用
<s:actionerror/>

来显示,但是有的时候addFieldError,addActionError会重复错误,就用theme属性解决
<s:textfield name="username" label="username" theme="simple"></s:textfield>


Struts2也有类似struts1 DispathAction的功能
如下配置文件:
<action name="register" class="com.langhua.action.RegisterAction">
			<result name="success">/success.jsp</result>
			<result name="input">/register2.jsp</result>
		</action>
		
		<action name="add" class="com.langhua.action.RegisterAction" method="addmessage">
			<result name="success">/success.jsp</result>
			<result name="input">/register3.jsp</result>
		</action>

在Action的方法里面就加上(add.action)
public String addmessage() throws Exception {		
		return Action.SUCCESS;
	}
	
	public void validateAddmessage(){
		System.out.println("validateAddmessage");
	}

就实现了验证和业务LOGIC的处理

解决每次都要调用validate()方法来验证的问题

1.使validate()方法为空,不写他
2.把默认的validate()方法改成validateExecute()这样也能解决。

类型转换与输入校验的流程

1. 首先Struts2对客户端传来的数据进行类型转换

2. 类型转换完毕后再进行输入校验

3. 如果类型转换和输入校验都没有错误发生,那么进入execute方法(调用商业逻辑)


注意:如果类型转换不成功,也同样要进行输入校验

你可能感兴趣的:(jdk,html,xml,jsp,struts)