如何进行声明式验证之字段验证?
来源:互联网 发布:js jq时间段选择控件 编辑:程序博客网 时间:2024/06/06 01:44
使用一个声明式验证程序需要 3 个步骤:
1. 确定哪些 Action 字段需要验证
2. 编写一个验证程序配置文件. 它的文件名必须是以下两种格式之一:
若一个 Action 类的多个 action 使用同样的验证规则: ActionClassName-validation.xml
若一个 Action 类的多个 action 使用不同的验证规则: ActionClass-alias-validation.xml, 例如 UserAction-User_create-validation.xml (想了解这个条如何使用,请点击)
3. 确定验证失败时的响应页面: 在 struts.xml 文件中定义一个 <result name=“input”> 的元素.
如何进行声明式验证之字段验证?
1 搭建环境:
eclipase:Luna Release (4.4.0)
sturts2:2.3.28 下载地址:http://download.csdn.net/detail/chuck_kui/9513090
项目结构:
~~~~~~~~~~~~~~~~~~~华华 ......,严肃点 。嗯 ,好的,这是分割线~~~~~~~~~~
类
Person.java
<span style="font-size:14px;">package com.baidu.domain;public class Person {private Integer age;public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Person [age=" + age + "]";}}</span><span style="font-weight: bold; font-size: 18px;"></span>Action :TestAgeValidation.java
<span style="font-size:14px;">package com.baidu.ActionValidation;import com.baidu.domain.Person;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.Preparable;public class TestAgeValidation extends ActionSupport implementsModelDriven<Person>, Preparable {private static final long serialVersionUID = 1L;@Overridepublic String execute() throws Exception {return super.execute();}private Person person;@Overridepublic Person getModel() {person = new Person();return person;}@Overridepublic void prepare() throws Exception {System.out.println("进来了吗?");}}</span>验证器:TestAgeValidation-validation.xml
<span style="font-size:14px;"><!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"><validators> <field name="age"> <field-validator type="int"> <param name="min">20</param> <param name="max">50</param> <message>Age needs to be 22222 between ${min} and ${max}</message> </field-validator> </field> </validators></span>
配置:
struts.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 配置全局的国际化资源文件 --><constant name="struts.custom.i18n.resources" value="nihao"></constant><package name="default" namespace="/" extends="struts-default"><!-- 这个就是起烂笔头的作用 --><interceptors><interceptor-stack name="baiduStack"><interceptor-ref name="paramsPrepareParamsStack"><param name="prepare.alwaysInvokePrepare">false</param></interceptor-ref></interceptor-stack></interceptors><default-interceptor-ref name="baiduStack"/><action name="testAge" class="com.baidu.ActionValidation.TestAgeValidation"><result>/success.jsp</result><result name="input">/validation.jsp</result></action></package></struts></span>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
页面:validation.jsp
<span style="font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ 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=UTF-8"><title>Insert title here</title></head><body><center><br><br><s:form action="testAge" ><s:textfield name="age" label="Age"></s:textfield><s:submit></s:submit></s:form></center></body></html></span>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ 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=UTF-8"><title>Insert title here</title></head><body>Age:<s:property value="age" /><h3>恭喜你有为青年!</h3></body></html>
55 大于50 报提示与页面
等于20 验证通过,
验证器好用!
0 0
- 如何进行声明式验证之字段验证?
- 如何进行非字段验证?
- struts2之声明式验证
- struts2之声明式验证后续及自定义验证器
- strut2验证之XML声明式文档验证
- Struts2声明式验证
- Struts2验证(声明式验证、自定义验证器)
- 如何进行DataGridView单元格验证
- 字段验证
- 使用Form类进行表单字段验证 (phpsa系列教程之二)
- 如何使用验证控件对DropDownList进行验证
- 如何使用curl进行验证码生成和验证
- 验证码识别打码是如何进行验证码识别的
- 如何对这些用户进行验证
- 如何使用ValidatorForm进行服务器端验证
- 如何进行验证测试(上)
- 如何进行验证测试(下)
- 如何C#程序进行验证码识别
- 设计模式—代理模式
- 浅谈命令查询职责分离(CQRS)模式
- 5+1道题
- Python excel 读写操作
- android6.0源码分析之Camera API2.0简介
- 如何进行声明式验证之字段验证?
- ArcGIS Engine控件运行许可
- SERVLETJSP学习(五)—— 转发 、JSP开发常见问题
- C# 文本文件读写问题
- 8天学通MongoDB——第一天 基础入门
- Oracle 11gR2 RAC alert_+ASM1.log出现WARNING: failed to online diskgroup resource ora.CRS.dg (unable to
- 做点Tomcat的笔记
- 数据分析框架Pandas进阶
- echart实例