`
lijunaccp
  • 浏览: 160518 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

输入校验

阅读更多
一. 基本校验
1. 页面:register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table width="40%" align="center" border="0">
		<tr>
			<td>
				<s:actionerror cssStyle="color:red"/>
				--------------------------------------------
				<s:fielderror cssStyle="color:blue"></s:fielderror>
			</td>
		</tr>
	</table>
	<form action="register.action" method="post">
		<table width="40%" align="center" border="1">
			<tr>
				<td>username:</td>
				<td><input type="text" name="username" size="20"/></td>
			</tr>
			<tr>
				<td>password:</td>
				<td><input type="password" name="password" size="20"/></td>
			</tr>
			<tr>
				<td>re-password:</td>
				<td><input type="password" name="repassword" size="20"/></td>
			</tr>
			<tr>
				<td>age:</td>
				<td><input type="text" name="age" size="20"/></td>
			</tr>
			<tr>
				<td>birthday:</td>
				<td><input type="text" name="birthday" size="20"/></td>
			</tr>
			<tr>
				<td>graduaction:</td>
				<td><input type="text" name="graduaction" size="20"/></td>
			</tr>
			<tr>
				<td><input type="submit" value="submit"/></td>
				<td><input type="reset" value="reset"/></td>
			</tr>
		</table>
	</form>
</body>
</html>


2.控制器:RegisterAction.java

package com.test.action;

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

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 graduaction;
	
	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 String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	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 getGraduaction() {
		return graduaction;
	}
	public void setGraduaction(Date graduaction) {
		this.graduaction = graduaction;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println("execute invoked");
		return SUCCESS;
	}
	
	@Override
	public void validate() {
		System.out.println("invalid=========================");
		
		if(null==username||username.length()<6||username.length()>10){
			this.addActionError("username invalid");
			//this.addFieldError("username", "field error: invalid username");
		}
		if(null==password||password.length()<6||password.length()>10){
			this.addActionError("password invalid");
		}else if(null==repassword||repassword.length()<6||repassword.length()>10){
			this.addActionError("repassword invalid");
		}else if(!password.equals(repassword)){
			this.addActionError("two password should be the same");
		}
		if(age<1||age>150){
			this.addActionError("age invalid");
			//this.addFieldError("age", "field error: invalid age");
		}
		if(null==birthday){
			this.addActionError("birthday invalid");
		}
		if(null==graduaction){
			this.addActionError("graduaction invalid");
		}
		if(null!=birthday&&null!=graduaction){
			Calendar c1=Calendar.getInstance();
			c1.setTime(birthday);
			Calendar c2=Calendar.getInstance();
			c2.setTime(graduaction);
			if(c1.after(c2)){
				this.addActionError("birthday should be before graduaction"); 
			}
		}
		
	}
}


3.配置文件:struts.xml
<action name="register" class="com.test.action.RegisterAction" >
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
		</action>

4. 友好提示(相当于国际化):RegisterAction.properties(放到Action相同的包下)
注:这是类型转换错误时系统添加的提示,另invalid.fieldvalue.是固定的
invalid.fieldvalue.username=\u7528\u6237\u540D\u4E0D\u5408\u6CD5
invalid.fieldvalue.age=\u5E74\u9F84\u4E0D\u5408\u6CD5
invalid.fieldvalue.birthday=\u751F\u65E5\u4E0D\u5408\u6CD5
invalid.fieldvalue.graduaction=\u6BD5\u4E1A\u65F6\u95F4\u4E0D\u5408\u6CD5

二. 同一个Action中,不同方法的不同验证
1. 改配置文件:struts.xml
<action name="register" class="com.test.action.RegisterAction" method="test">
			<result name="success">/success.jsp</result>
			<result name="input">/register.jsp</result>
		</action>

2. 改Action:RegisterAction.java
注:自定义校验方法名,是validate加上执行方法名,执行方法名首字母大写,另执行完自定义的校验后还会去执行默认的validate验证方法。
package com.test.action;

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

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 graduaction;
	
	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 String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	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 getGraduaction() {
		return graduaction;
	}
	public void setGraduaction(Date graduaction) {
		this.graduaction = graduaction;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println("execute invoked");
		return SUCCESS;
	}
	
	public String test() throws Exception {
		System.out.println("test invoked");
		return SUCCESS;
	}
	
	public void validateTest(){
		System.out.println("invalidTest=========================");
		
		if(null==username||username.length()<6||username.length()>10){
			this.addActionError("username invalid");
			//this.addFieldError("username", "field error: invalid username");
		}
		if(null==password||password.length()<6||password.length()>10){
			this.addActionError("password invalid");
		}else if(null==repassword||repassword.length()<6||repassword.length()>10){
			this.addActionError("repassword invalid");
		}else if(!password.equals(repassword)){
			this.addActionError("two password should be the same");
		}
		if(age<1||age>150){
			this.addActionError("age invalid");
			//this.addFieldError("age", "field error: invalid age");
		}
		if(null==birthday){
			this.addActionError("birthday invalid");
		}
		if(null==graduaction){
			this.addActionError("graduaction invalid");
		}
		if(null!=birthday&&null!=graduaction){
			Calendar c1=Calendar.getInstance();
			c1.setTime(birthday);
			Calendar c2=Calendar.getInstance();
			c2.setTime(graduaction);
			if(c1.after(c2)){
				this.addActionError("birthday should be before graduaction"); 
			}
		}
	}
	
	@Override
	public void validate() {
		
		System.out.println("invalid=========================");
		
		if(null==username||username.length()<6||username.length()>10){
			this.addActionError("username invalid");
			//this.addFieldError("username", "field error: invalid username");
		}
		if(null==password||password.length()<6||password.length()>10){
			this.addActionError("password invalid");
		}else if(null==repassword||repassword.length()<6||repassword.length()>10){
			this.addActionError("repassword invalid");
		}else if(!password.equals(repassword)){
			this.addActionError("two password should be the same");
		}
		if(age<1||age>150){
			this.addActionError("age invalid");
			//this.addFieldError("age", "field error: invalid age");
		}
		if(null==birthday){
			this.addActionError("birthday invalid");
		}
		if(null==graduaction){
			this.addActionError("graduaction invalid");
		}
		if(null!=birthday&&null!=graduaction){
			Calendar c1=Calendar.getInstance();
			c1.setTime(birthday);
			Calendar c2=Calendar.getInstance();
			c2.setTime(graduaction);
			if(c1.after(c2)){
				this.addActionError("birthday should be before graduaction"); 
			}
		}
	}
}

三. Struts2标签库自带获取fielderror
1. login2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

	<s:form action="login">
			<s:textfield name="username" label="username" ></s:textfield>
			<s:password name="password" label="password"></s:password>
			<s:submit value="submit"></s:submit>
	</s:form>
	
</body>
</html>

2. LoginAction.java
package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	private String username;
	private String password;
	
	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 String execute() throws Exception{
		return "success";
	}
	
	@Override
	public void validate() {
		if(username!=null&&-1!=username.indexOf("hello")){
			this.addFieldError("username", "username invalid");
		}
		if(password!=null&&password.length()<4){
			this.addFieldError("password", "password invalid");
		}
	}
}

四. 验证框架
1. 修改下RegisterAction.java
package com.test.action;

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

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 graduaction;
	
	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 String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	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 getGraduaction() {
		return graduaction;
	}
	public void setGraduaction(Date graduaction) {
		this.graduaction = graduaction;
	}
	
	@Override
	public String execute() throws Exception {
		System.out.println("execute invoked");
		return SUCCESS;
	}
	
//	public String test() throws Exception {
//		System.out.println("test invoked");
//		return SUCCESS;
//	}
//	
//	public void validateTest(){
//		System.out.println("invalidTest=========================");
//		
//		if(null==username||username.length()<6||username.length()>10){
//			this.addActionError("username invalid");
//			//this.addFieldError("username", "field error: invalid username");
//		}
//		if(null==password||password.length()<6||password.length()>10){
//			this.addActionError("password invalid");
//		}else if(null==repassword||repassword.length()<6||repassword.length()>10){
//			this.addActionError("repassword invalid");
//		}else if(!password.equals(repassword)){
//			this.addActionError("two password should be the same");
//		}
//		if(age<1||age>150){
//			this.addActionError("age invalid");
//			//this.addFieldError("age", "field error: invalid age");
//		}
//		if(null==birthday){
//			this.addActionError("birthday invalid");
//		}
//		if(null==graduaction){
//			this.addActionError("graduaction invalid");
//		}
//		if(null!=birthday&&null!=graduaction){
//			Calendar c1=Calendar.getInstance();
//			c1.setTime(birthday);
//			Calendar c2=Calendar.getInstance();
//			c2.setTime(graduaction);
//			if(c1.after(c2)){
//				this.addActionError("birthday should be before graduaction"); 
//			}
//		}
//	}
//	
//	@Override
//	public void validate() {
//		
//		System.out.println("invalid=========================");
//		
//		if(null==username||username.length()<6||username.length()>10){
//			this.addActionError("username invalid");
//			//this.addFieldError("username", "field error: invalid username");
//		}
//		if(null==password||password.length()<6||password.length()>10){
//			this.addActionError("password invalid");
//		}else if(null==repassword||repassword.length()<6||repassword.length()>10){
//			this.addActionError("repassword invalid");
//		}else if(!password.equals(repassword)){
//			this.addActionError("two password should be the same");
//		}
//		if(age<1||age>150){
//			this.addActionError("age invalid");
//			//this.addFieldError("age", "field error: invalid age");
//		}
//		if(null==birthday){
//			this.addActionError("birthday invalid");
//		}
//		if(null==graduaction){
//			this.addActionError("graduaction invalid");
//		}
//		if(null!=birthday&&null!=graduaction){
//			Calendar c1=Calendar.getInstance();
//			c1.setTime(birthday);
//			Calendar c2=Calendar.getInstance();
//			c2.setTime(graduaction);
//			if(c1.after(c2)){
//				this.addActionError("birthday should be before graduaction"); 
//			}
//		}
//	}
}

2. 在RegisterAction.java包下增加校验文件RegisterAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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="username">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>username required</message>
		</field-validator>
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">12</param>
			<message>username should be between ${minLength} and ${maxLength}</message>
		</field-validator>
	</field>
	
	<field name="password">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>password required</message>
		</field-validator>
		
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">12</param>
			<message>password should be between ${minLength} and ${maxLength}</message>
		</field-validator>
	</field>
	
	<field name="repassword">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>repassword required</message>
		</field-validator>
		
		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">12</param>
			<message>repassword should be between ${minLength} and ${maxLength}</message>
		</field-validator>
	</field>
	
	<field name="age">
		<field-validator type="required">
			<message>age required</message>
		</field-validator>
		<field-validator type="int">
			<param name="min">1</param>
			<param name="max">150</param>
			<message>age should be between ${min} and ${max}</message>
		</field-validator>
	</field>
	
	<field name="birthday">
		<field-validator type="required">
			<message>birthday required</message>
		</field-validator>
		<field-validator type="date">
			<param name="min">1900-1-1</param>
			<param name="max">2200-1-1</param>
			<message>birthday should be between ${min} and ${max}</message>
		</field-validator>
	</field>
	
	<field name="graduaction">
		<field-validator type="required">
			<message>graduaction required</message>
		</field-validator>
		<field-validator type="date">
			<param name="min">1920-1-1</param>
			<param name="max">2200-1-1</param>
			<message>graduaction should be between ${min} and ${max}</message>
		</field-validator>
	</field>
</validators>

注:以上是field级别的校验,还有validator级别的校验,两种级别效果是一样的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!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="requiredstring">
		<param name="fieldName">username</param>
		<message>username required</message>
	</validator>
	
	<validator type="stringlength">
		<param name="fieldName">username</param>
		<param name="minLength">6</param>
		<param name="maxLength">12</param>
		<message>username should be between ${minLength} and ${maxLength}</message>
	</validator>
<validators>

总结:
1.首先执行类型转换
2.执行对应的校验框架
3.执行特定方法对应的validate验证(test,validateTest)
4.执行validate()方法
如果在以上所有过程中,发现了任何错误,都不会再去执行execute()方法或
指定的特定方法(test),页面转向了struts.xml中input这个result所指向的页面。
分享到:
评论

相关推荐

    Struts2输入校验总结

    ### Struts2输入校验深度解析 #### 一、手动输入完成校验 在Struts2框架中,输入校验是确保数据完整性和安全性的重要环节。对于手动输入完成校验,Struts2提供了灵活的机制。 1. **普通处理方式**:在Action类中...

    struts2的输入校验

    struts2的输入校验有两种方式: 一.重写validate()方式 二.采用配置文件的方式 先我们就以一个简单的登录的例子来讲解这个吧. 重点讲解下,第二个采用配置文件的方式: 1.添加一个xml的校验文件,保存在...

    struts2输入校验深入总结

    输入校验是Web应用中不可或缺的一部分,确保了数据的准确性和安全性。本文将深入探讨Struts2中的输入校验机制,帮助开发者更好地理解和应用。 一、Struts2输入校验概述 在Struts2中,输入校验主要用于验证用户通过...

    Struts2的输入校验

    在Struts2中,输入校验是确保数据安全、准确和有效的重要环节。本篇文章将深入探讨Struts2的输入校验机制及其相关知识点。 **输入校验的重要性** 输入校验是Web应用开发中的关键步骤,它可以防止恶意用户提交无效或...

    页面输入校验

    在IT行业中,页面输入校验是一项至关重要的技术实践,它涉及到用户在网页上提交数据时的数据有效性检查。页面输入校验的主要目标是确保用户输入的数据符合预设的规则,从而防止错误的数据进入系统,提高数据的准确性...

    Struts2类型转换与输入校验 .zip

     实验目的:  熟悉和掌握Struts2自定义类型转换。  熟悉和掌握Struts2内建校验的操作方法 ...(2) 根据实验四的图书管理系统,扩展图书新增功能,要求对新增页面表单中的每个文件输入框进行输入校验

    输入校验之validateXXX方法校验

    在软件开发中,输入校验是一项至关重要的任务,它确保了程序接收到的数据是有效、安全且符合预期的。"validateXXX"方法是输入校验的一种常见实现方式,这里的"XXX"通常代表具体的校验规则或者字段名称。下面将详细...

    struts2输入校验

    本文将深入探讨Struts2中的输入校验机制,以及如何对指定方法进行输入校验。 在Struts2中,输入校验通常分为两种方式:客户端校验和服务器端校验。客户端校验主要通过JavaScript在用户端进行,可以提供即时反馈,但...

    struts2输入校验总结

    Struts2是一个流行的Java web开发框架,它提供了一套强大的输入校验机制,确保用户提交的数据符合业务逻辑的要求。在本文中,我们将深入探讨Struts2的输入校验功能,包括手动输入校验和基于验证框架的输入校验。 ##...

    Struts2 输入校验

    输入校验是确保数据安全和应用程序稳定性的重要环节,防止了非法或无效数据进入系统,可能导致的安全漏洞或逻辑错误。本篇文章将深入探讨Struts2中的输入校验机制。 一、Struts2 输入校验概述 Struts2提供了多种...

    android 输入校验源代码

    在Android开发中,输入校验是一项至关重要的任务,它确保用户在交互过程中提供的数据是有效、合法的。本文将深入探讨Android输入校验的概念、重要性,并基于提供的"android-saripaar-master"源代码库,详细介绍一种...

    struts2中的输入校验

    在Struts2中,输入校验是确保用户提交的数据符合预设规范的重要环节,可以防止非法数据进入系统,保护数据的完整性和安全性。本文将详细探讨Struts2中的输入校验机制。 首先,Struts2提供了两种主要的校验方式:...

    Text的输入校验

    在IT行业中,文本的输入校验是一个至关重要的环节,它涉及到数据的准确性、系统的安全性和用户体验。本篇文章将深入探讨“Text的输入校验”这一主题,并基于提供的资源——一个名为"textValidation.html"的文件,来...

    Struts2的输入校验实例代码

    在Struts2中,输入校验是确保数据完整性和安全性的重要环节。输入校验可以防止恶意用户提交无效或有害的数据,并确保应用程序的稳定运行。以下是关于Struts2输入校验的详细知识: 1. **客户端校验**: 客户端校验...

    使用struts2进行输入校验

    在Struts2中,输入校验是一个至关重要的部分,它确保了用户从客户端提交的数据符合预设的业务规则,从而避免了无效或不合法数据进入系统。本篇文章将详细介绍如何在Struts2中进行输入校验,包括两种主要实现方式:...

Global site tag (gtag.js) - Google Analytics