Struts2_对Action中所有方法进行输入校验
来源:互联网 发布:c程序员面试题库 编辑:程序博客网 时间:2024/06/06 00:56
手工编写代码实现对acgion中所有方法输入校验:
通过重写validate()方法实现,validate()方法会校验action中所有与execute()方法签名相同的方法。当某个数据校验失败时,我们应该调用addFieldError()方法往系统的fieldErrors添加校验失败信息(为了使用addFieldError()方法,action可以继承ActionSupport),如果系统的fieldError包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以通过<s:fielderror/>显示失败信息。validate()使用例子:@Overridepublic void validate() {// 会对action中的所有方法进行校验if (this.username == null || "".equals(this.username.trim())) {this.addFieldError("username", "用户名不能为空");}if (this.mobile == null || "".equals(this.mobile.trim())) {this.addFieldError("mobile", "手机号不能为空");} else {// 格式1,3/5/8,后面是9个数字if (!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()) {this.addFieldError("mobile", "手机号格式不正确");}}}验证失败后,请求转发至input视图:<result name="input">/index.jsp</result>在index.jsp页面中使用<s:fielderror/>显示失败信息。
<?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><!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输入 --><constant name="struts.i18n.encoding" value="UTF-8" /><!-- 该属性指定需要struts2外理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --><constant name="struts.action.extension" value="com,action" /><!-- 当struts的配置文件修改后,系统是否自动更新该文件,默认认值为false(生产环境下使作),开发阶断最好打开 --><constant name="struts.configuration.xml.reload" value="true" /><!-- 引用其它struts.xml配置文件 --><include file="struts_validata.xml"/></struts>
<?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="itcast" namespace="/person" extends="struts-default"><action name="list_*" class="cn.itcast.a_action.PersonAction" method="{1}"><result name="input">/index.jsp</result><result name="message">/WEB-INF/page/message.jsp</result></action></package></struts>
package cn.itcast.a_action;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport {private String username;private String mobile;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public String update() {ActionContext.getContext().put("message", "更新成功");return "message";}public String save() {ActionContext.getContext().put("message", "保存成功");return "message";}@Overridepublic void validate() {// 会对action中的所有方法进行校验if (this.username == null || "".equals(this.username.trim())) {this.addFieldError("username", "用户名不能为空");}if (this.mobile == null || "".equals(this.mobile.trim())) {this.addFieldError("mobile", "手机号不能为空");} else {// 格式1,3/5/8,后面是9个数字if (!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()) {this.addFieldError("mobile", "手机号格式不正确");}}}}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><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="<%=request.getContextPath()%>/person/list_update.action"method="post">用户名:<input type="text" name="username">不能为空<br /> 手机号:<inputtype="text" name="mobile" />不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br /> <inputtype="submit" value=" 提 交 " /></form></body></html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><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>${message }</body></html>
阅读全文
0 0
- Struts2_对Action中所有方法进行输入校验
- 第十九章 对Action中所有方法进行输入校验
- struts2对Action中所有的方法进行输入校验
- Struts2_对action指定方法进行校验
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- Struts2_基于XML配置方式实现对action的所有方法进行校验
- 复习struts2之对action中所有方法进行输入校验
- struts2对Action中所有方法进行输入校验(手工编写代码)
- 对Action中所有方法、指定方法进行校验
- Struts2:对Action中方法进行输入校验
- 学习笔记------------------对Action中所有方法进行输入校验和对指定方法输入校验(手工编写代码和xml方式)
- 基于XML配置方式实现对action的所有方法和指定方法进行输入校验
- struts第十天-----对action中的所有方法进行输入校验
- 基于XML配置方式实现对action的所有方法进行输入校验
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的所有方法进行输入校验
- Struts2_基于XML配置方式对指定action方法实现输入校验、基于XML校验的一些特点
- 手工编写代码实现对action中所有方法输入校验
- struts2中以xml配置方式对action的所有方法进行校验
- centos,linux下django 链接mysql提示mysql.sock无法打开问题
- bond,team链路聚合
- UVALive6428 A+B【扩展欧几里得算法+GCD】
- Linux运维笔记-文档总结-通过ssl加密的HTTPS超文本传输协议和网页重写
- 第35课:彻底解密Spark 2.1.X中Sort Shuffle 中TimSort排序源码具体实现
- Struts2_对Action中所有方法进行输入校验
- Linux运维笔记-文档总结-虚拟网络接口bond
- 图解正向代理、反向代理、透明代理
- Struts2_对action指定方法进行校验
- 用国家简写查找对应的国家名称和所在 洲
- LeetCode 394. Decode String
- LeetCode 402. Remove K Digits
- linux 数据库管理
- lintcode刷题(python)——(4)