对Action指定方法进行校验
来源:互联网 发布:淘宝手机估价 编辑:程序博客网 时间:2024/06/05 10:24
手工编写代码实现对action指定方法输入校验:
通过validateXxx()方法实现,validateXxx只会校验action中方法名为Xxx的方法。其中Xxx的第一个字母要大写。
code:
index.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> <form action="${pageContext.request.contextPath}/person/manage_update.action" method="post"> 用户名:<input type="text" name="username"><br/> 手机号:<input type="text" name="mobile"><br/> <input type="submit" value="提交"/> </form> </body></html>
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.itcast.action.PersonAction" method="{1}"> <result name="input">/index.jsp</result> <result name="message">/WEB-INF/page/message.jsp</result> </action> </package></struts>
Person.action
package cn.itcast.action;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport{/** * @author wangfeng */private static final long serialVersionUID = 6609644196829871636L;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";}//will validate the "update" functionpublic void validateUpdate(){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", "手机号格式不正确");}}}}
- 对Action指定方法进行校验
- Struts2_对action指定方法进行校验
- 对Action中所有方法、指定方法进行校验
- 第二十章 对action指定方法进行校验
- struts2对action指定方法进行校验(人工编码)
- 基于XML配置方式实现对action的所有方法和指定方法进行输入校验
- 对action中的某个方法进行校验
- struts2基于XML配置方式对指定action方法进行校验
- struts2中对指定方法进行校验以及校验流程
- struts2-18基于XML配置方式实现对action的所有方法或指定方法进行输入校验
- 学习笔记------------------对Action中所有方法进行输入校验和对指定方法输入校验(手工编写代码和xml方式)
- 对struts2中action的多个方法进行校验
- 第十九章 对Action中所有方法进行输入校验
- 复习Struts2之对action中指定方法进行校验
- struts2对Action中所有的方法进行输入校验
- struts2对Action中指定方法进行校验
- struts2对action中的方法进行输入校验(1)
- struts2对action中的方法进行输入校验(2)
- 找到为何vs2010编的程序为何会一闪而过的原因了
- ASP:500-IIS服务器内部错误解决方法
- PostgreSQL中的抽象数据类型--Datum
- 阶段总结
- JAVA 包与包之间访问(package)
- 对Action指定方法进行校验
- ACMer 2013 Daily Training- 7th Mar for 11x&&12x
- iPad App
- 无net,纯API添加管理员用户--VC
- [Python入门及进阶笔记]Python-基础-字典小结
- 做事必须搞清10个顺序
- 35岁前要做的7件与工作相关的事
- UILabel中的文字顶端对齐
- POJ-2965-The Pilots Brothers' refrigerator