Struts2的手工自定义验证--完整实例代码
生活随笔
收集整理的這篇文章主要介紹了
Struts2的手工自定义验证--完整实例代码
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ActionSupport類實(shí)現(xiàn)了Validateable、ValidationAware接口,
其中Validateable接口就是驗(yàn)證器接口,該接口有一個(gè)validate()方法,
validate()方法專門用來(lái)驗(yàn)證數(shù)據(jù),實(shí)現(xiàn)的時(shí)候需要繼承ActionSupport類,
所以編寫一個(gè)Action類時(shí)重寫validate()方法就可以實(shí)現(xiàn)自定義輸入驗(yàn)證功能。
?
實(shí)例---流程還是老一套
1、在web.xml中配置核心控制器FilterDispatcher
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <!-- 配置Struts2核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 配置Struts2核心Filter的實(shí)現(xiàn)類 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 配置Filter攔截的URL --> <filter-mapping> <!-- 配置Struts2的核心FilterDispatcher攔截所有用戶請(qǐng)求 --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>?
2、編寫視圖組件(register1.jsp)
<%@ page language="java" import="java.util.*" 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>用戶注冊(cè)頁(yè)面</title> </head> <body><center>請(qǐng)輸入信息……<hr><s:form action="register1" method="post"><table border="1"><tr><td><s:textfield name="userName" label="姓名" size="16" /></td></tr><tr><td><s:password name="userPassword" label="密碼" size="18" /></td></tr><tr><td><s:textfield name="userAge" label="年齡" size="16" /></td></tr><tr><td><s:textfield name="userTelephone" label="電話" size="16" /></td></tr><tr><td><s:submit value="提交" /></td></tr></table></s:form></center> </body> </html>?
3、編寫業(yè)務(wù)控制器Action類(RegistAction1.java)
?這里重寫了validate()方法
package validatorAction;import com.opensymphony.xwork2.ActionSupport;public class RegistAction1 extends ActionSupport{private String userName;private String userPassword;private int userAge;private String userTelephone;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPassword() {return userPassword;}public void setUserPassword(String userPassword) {this.userPassword = userPassword;}public int getUserAge() {return userAge;}public void setUserAge(int userAge) {this.userAge = userAge;}public String getUserTelephone() {return userTelephone;}public void setUserTelephone(String userTelephone) {this.userTelephone = userTelephone;}//重寫validate()方法public void validate(){if(userName==null||userName.length()<6||userName.length()>16){addFieldError("userName","用戶姓名的長(zhǎng)度不符合要求,6~16位!");}if(userPassword.length()<6||userPassword.length()>16){addFieldError("userPassword","密碼的長(zhǎng)度不符合要求,6~16位!");}if(userAge>130||userAge<1){addFieldError("userAge","年齡不符合要求,1~130歲");}if(userTelephone.length()!=8){addFieldError("userTelephone","電話號(hào)碼不符合要求,8位");}}public String execute(){return "success";}}?
4、struts.xml配置對(duì)應(yīng)的action(name="register1")
<?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><package name="intercept" extends="struts-default"><interceptors><interceptor name="replace" class="interceptor.MyInterceptor" ></interceptor></interceptors><action name="public" class="interceptor.PublicAction"><result name="success">/success.jsp</result><result name="login">/success.jsp</result><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="replace"></interceptor-ref></action><action name="register1" class="validatorAction.RegistAction1"><result name="input">/validate/register1.jsp</result><result name="success">/validate/success1.jsp</result></action><action name="register2" class="validatorAction.Register2Action"><result name="input">/validate/register2.jsp</result><result name="success">/validate/success2.jsp</result></action></package></struts>?
轉(zhuǎn)載于:https://www.cnblogs.com/Donnnnnn/p/5675754.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的Struts2的手工自定义验证--完整实例代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: EasyUI中dialog中嵌入form
- 下一篇: 内存一致性模型