struts2-16对action中的方法进行输入校验
来源:互联网 发布:sql loader 语法 编辑:程序博客网 时间:2024/06/05 13:32
对action中的所有方法进行输入校验:
输入校验:
在struts2中我们可以实现对action的所有方法进行校验或者对action中的指定方法进行校验。
对于输入校验struts2提供了2种实现方法:
1、采用手工编写代码实现。
2、基于XML配置文件方式实现。
手工编写代码实现对action中所有方法的输入验证:
通过重写validate()方法实现,validate()方法会校验action中所有与execute()方法签名相同的方法。当某个数据校验失败时,应该调用addFieldError()方法往系统的filedError()方法添加校验失败信息(为了使用AddFieldError()方法,action可以实现ActionSupport),如果系统的filedErrors包含失败信息,struts会将请求转发到名为input的result。在input视图中可以通过显示失败信息。
validate()使用例子:[action中]
public void validate(){//会对action中的所有方法进行校验 if(this.mobile==null||"".equals(this.mobile.trim())){ this.addFieldError("mobile","手机号不能为空"); }else{ if(!Pattern.compile("1[358]\d{9}").matcher(this.mobile.trim()).matches()){ this.addFieldError("mobile","手机格式不正确"); } }}
验证失败后,请求转发至input视图:
<result name = "input" >WEN-INF/page/addUser.jsp</result>
在addUser.jsp页面使用显示失败信息。
例子:
action:
package cn.gz.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"; } @Override public 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{ if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){ this.addFieldError("mobile", "手机号格式不正确"); } } } }
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="person" namespace="/person" extends="struts-default"> <action name="manage_*" class="cn.gz.action.PersonAction" method="{1}"> <result name="input">/index.jsp</result> <result name="message">/WEB-INF/page/message.jsp</result> </action> </package></struts>
index.jsp:
<%@ page language="java" 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="${pageContext.request.contextPath}/person/manage_update.action" method="post"> 用户名:<input type="text" name="username"/>不能为空<br/> 手机号:<input type="text" name="mobile"/>不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br/> <input type="submit" value="提 交"/></form> </body></html>
message.jsp:
<%@ 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>
对action中指定的方法进行输入校验:
手工编写代码实现对action中指定方法的输入校验:
通过重新validateXxx()方法实现,validateXxx()只会校验action中方法名为Xxx()的方法。其中Xxx的第一个字要大写。当某个数据校验失败时,应该调用addFieldError()方法往系统的filedError()方法添加校验失败信息(为了使用AddFieldError()方法,action可以实现ActionSupport),如果系统的filedErrors包含失败信息,struts会将请求转发到名为input的result。在input视图中可以通过显示失败信息。
validateXxx()方法使用例子:
public String add() throws Exception{ return "success";}public void validateAdd(){ if(username == null || "".equals(username.trim())){ this.addFieldError("username","用户名不能为空"); }}
验证失败后,请求转发至input视图:
<result name = "input">WEB-INF/page/addUser.jsp</result>
在addUser.jsp页面用显示验证信息。
例子:
action中:
package cn.gz.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"; } public void validateUpdate() {//会对update()方法校验 if(this.username==null || "".equals(this.username.trim())){ this.addFieldError("username", "用户名不能为空"); } if(this.mobile==null || "".equals(this.mobile.trim())){ this.addFieldError("mobile", "手机号不能为空"); }else{ if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){ this.addFieldError("mobile", "手机号格式不正确"); } } }}
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="person" namespace="/person" extends="struts-default"> <action name="manage_*" class="cn.gz.action.PersonAction" method="{1}"> <result name="input">/index.jsp</result> <result name="message">/WEB-INF/page/message.jsp</result> </action> </package></struts>
index.jsp:
<%@ page language="java" 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="${pageContext.request.contextPath}/person/manage_save.action" method="post"> 用户名:<input type="text" name="username"/>不能为空<br/> 手机号:<input type="text" name="mobile"/>不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br/> <input type="submit" value="提 交"/></form> </body></html>
- struts2-16对action中的方法进行输入校验
- struts2对action中的方法进行输入校验(1)
- struts2对action中的方法进行输入校验(2)
- struts2对Action中所有的方法进行输入校验
- Struts2:对Action中方法进行输入校验
- struts2对action中的方法进行输入校验---xml配置方式(3)
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- struts2中对Action类中的方法进行校验
- 对action中的某个方法进行校验
- 复习struts2之对action中所有方法进行输入校验
- struts2对Action中所有方法进行输入校验(手工编写代码)
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的所有方法进行输入校验
- struts第十天-----对action中的所有方法进行输入校验
- 对struts2中action的多个方法进行校验
- 复习Struts2之对action中指定方法进行校验
- struts2对Action中指定方法进行校验
- struts2基于xml配置方式对Action方法进行校验
- struts2对action指定方法进行校验(人工编码)
- python json.dumps() json.dump()的区别
- JVM虚拟机结构
- (详解)无指针AC自动机
- 统计信息收集进程----数据架构师的PostgreSQL修炼
- 第11周 【项目3
- struts2-16对action中的方法进行输入校验
- linux安装tomcat以及启动多个tomcat
- CSDN 初识第一天
- 从零开始搭建个人网站(0) 准备工作
- xamarin学习笔记B07(ios的UITableView)
- c语言动态内存申请 melloc()使用注意
- 三个重要配置work_mem, maintenance_work_mem, effective_cachesize----数据架构师的PostgreSQL修炼
- 100经典算法之(三)---分解质因数
- 第11周 【项目4