struts2对Action中所有的方法进行输入校验
来源:互联网 发布:电商美工教程 编辑:程序博客网 时间:2024/05/22 14:46
DataValidateAction.java:
package blog.validate;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionSupport;public class DataValidateAction extends ActionSupport {private static final long serialVersionUID = -1817223356525882414L;private String user;private String mobile;public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}@Overridepublic void validate() {if (this.user == null || "".equals(this.user)) {this.addFieldError("user", "用户名不能为空");}if (this.mobile == null || "".equals(this.mobile)) {this.addFieldError("mobile", "手机号码不能为空");}else {if (!Pattern.compile("^1[358]\\d{9}$").matcher(mobile).matches()) {this.addFieldError("mobile", "手机号码格式不正确");}}}public String execute(){return "success";}}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="validate" extends="struts-default" namespace="/person"><action name="validate" class="blog.validate.DataValidateAction" method="execute"><result name="input">/index.jsp</result><result name="success">/index.jsp</result></action></package></struts>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>校验输入</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> </head> <body> <s:fielderror/> <form action="" method="post" > 用户姓名:<input type="text" name="user"/>不能为空<br/> 手机号码:<input type="text" name="mobile"/>不能为空,并且要符合规则<br/> <input type="submit" value="提交"/> </form> </body></html>
- struts2对Action中所有的方法进行输入校验
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- 复习struts2之对action中所有方法进行输入校验
- struts2对Action中所有方法进行输入校验(手工编写代码)
- 第十九章 对Action中所有方法进行输入校验
- Struts2_对Action中所有方法进行输入校验
- Struts2:对Action中方法进行输入校验
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的所有方法进行输入校验
- struts2中以xml配置方式对action的所有方法进行校验
- struts2-18基于XML配置方式实现对action的所有方法或指定方法进行输入校验
- 对struts2中action的多个方法进行校验
- struts2对action中的方法进行输入校验(1)
- struts2对action中的方法进行输入校验(2)
- struts2-16对action中的方法进行输入校验
- 基于XML配置方式实现对action的所有方法和指定方法进行输入校验
- 对Action中所有方法、指定方法进行校验
- 复习struts2之基于XML配置方式实现对action的所有方法进行校验
- Struts2 基于XML配置方式实现对action的所有方法进行校验
- 用 ant 编译你的 flex 项目
- 六种异常处理的陋习
- 【lizhi125】免费且实用的软件截图+屏幕视频录像工具——Free Screen Video Recorder
- [androidpn客户端] androidpn-client 常见BUG解决方法
- 【总结】java文件移动
- struts2对Action中所有的方法进行输入校验
- 深入理解iPhone委托模式兼谈iPhone生命周期
- TableModel.JTable和AbstractTableModel
- NO.81 #一个码农的异想天开# 利用问题管理工具管理城市一般问题?
- 第四次学习java
- rhel内核源码下载
- 如何解决fireworks cs5 启动 “发生内部错误”
- IBM MQ 学习笔记
- iPhone开发经典语录集锦