Struts2 对Action中所有方法进行输入校验、单个方法进行校验
index.jsp:
<body>
<s:fielderror />
<form action="${pageContext.request.contextPath }/cn/person_add.do" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input name="userName" type="text" value="${userName }" /></td>
</tr>
<tr>
<td>手机号:</td>
<td><input name="phone" type="text" value="${phone }" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录" /></td>
</tr>
</table>
</form>
</body>
action:
public class PersonAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String userName;
private String phone;
private String message;
public String add(){
message="添加成功";
return "message";
}
public String update(){
message="更新成功";
return "message";
}
@Override
public void validate() {
if(userName==null || userName.trim().equals("")){
// 当校验失败时,通过addFieldError为字段添加校验失败信息
// 在页面中通过 <s:fielderror /> 显示失败信息
// 当校验失败以后,Struts会默认调用一个名为input的result所以需要在Struts的Action里面配置input
addFieldError("userName", "用户名不能为空");
}
if(phone==null || phone.trim().equals("")){
addFieldError("phone", "手机号不能为空");
}else{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(phone).matches()){
addFieldError("phone", "手机号格式不正确");
}
}
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
struts.xml
<struts>
<constant name="struts.118n.encoding" value="UTF-8"></constant>
<constant name="struts.action.extension" value="do"></constant>
<package name="mainPackage" namespace="/cn" extends="struts-default">
<global-results>
<result name="message">/WEB-INF/message.jsp</result>
</global-results>
<action name="person_*" class="cn.person.PersonAction" method="{1}" >
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
验证:
对单个方法进行校验
对单个方法进行校验更简单,只需要将重 写的ActionSupport的validate 改成 validateXxxx()即可,即validate+要校验的方法名称(首字母大写)例:
public void validateUpdate() {
if(userName==null || userName.trim().equals("")){
// 当校验失败时,通过addFieldError为字段添加校验失败信息
// 在页面中通过 <s:fielderror /> 显示失败信息
// 当校验失败以后,Struts会默认调用一个名为input的result所以需要在Struts的Action里面配置input
addFieldError("userName", "用户名不能为空");
}
if(phone==null || phone.trim().equals("")){
addFieldError("phone", "手机号不能为空");
}else{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(phone).matches()){
addFieldError("phone", "手机号格式不正确");
}
}
}
Struts2 对Action中所有方法进行输入校验、单个方法进行校验的更多相关文章
- 在Struts2的Action中获得request response session几种方法
转载自~ 在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为se ...
- Struts2 的Action中取得请求参数值的几种方法
先看GetRequestParameterAction类代码: Java代码 public class GetRequestParameterAction extends ActionSupport ...
- 在Struts2的Action中取得请求参数值的几种方法
先看GetRequestParameterAction类代码: public class GetRequestParameterAction extends ActionSupport { priva ...
- JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- 在struts2的action中操作域对象(request、session)
在struts2的Action中,操作域对象一共有三种方式: 1.ActionContext(与servelt API无关联): //相当于request ActionContext.getConte ...
- struts2对action中的方法进行输入校验---xml配置方式(3)
上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍第二种方式:xml配置文件 首先我们来看一个样例: ValidateAction.java: package co ...
- struts2对action中的方法进行输入校验(2)
struts2输入校验流程: 1.类型转换器对请求參数运行类型转换,并把转换后的值赋给aciton中的属性 2.假设在运行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext, ...
- 理解Struts2的Action中的setter方法是怎么工作的
接触过webwork和Struts2的同行都应该知道, 提交表单的时候,只要Action中的属性有setter 方法,这些表单数据就可以正确赋值到Action中属性里:另外对于Spring配置文件中声 ...
- 【JAVA学习】struts2的action中使用session的方法
尊重版权:http://hi.baidu.com/dillisbest/item/0bdc35c0b477b853ad00efac 在Struts2里,假设须要在Action中使用session.能够 ...
随机推荐
- eclipse 软件的背景颜色、字体设置
1.eclipse 背景色设置: Window->Preferences->General->Editors->Text Editors->Backgroud color ...
- Qt 鼠标样式特效探索样例(一)——利用时间器调用QWidget.move()函数
Qt 鼠标样式特效探索样例(一) 心血来潮,突然想在Qt里玩一把鼠标样式,想到在浏览网页时,经常看到漂亮的鼠标动画,于是今天摸索着乱写个粗糙的demo,来满足自己的好奇心. 效果图 方案要 ...
- delete 多表删除的使用(连表删除)
delete 多表删除的使用 1.从数据表t1中把那些id值在数据表t2里有匹配的记录全删除掉 DELETE t1 FROM t1,t2 WHERE t1.id=t2.id 或 DELETE ...
- Unix/Linux环境C编程入门教程(30) 字符串操作那些事儿
函数介绍 rindex(查找字符串中最后一个出现的指定字符) 相关函数 index,memchr,strchr,strrchr 表头文件 #include<string.h> 定义函数 c ...
- Activity和View的区别:
Activity和View的区别: activity相当于控制部分,view相当于显示部分.两者之间是多对多的关系,所有东西必须用view来显示. viewGroup继承自view,实现了ViewM ...
- 全国计算机等级考试二级教程-C语言程序设计_第3章_顺序结构
1输入两个整数给变量x和y:然后输出x和y:在交换x和y中的值后,在输出x和y. #include <stdio.h> main() { int x, y, t; printf(" ...
- mongodb启动关闭;
[正确关闭方法] 方法一 ps -ef |grep mongodb 找到你要查找的进程号 kill -2 pid 杀掉 方法二 也可以进入到mongo数据库里面进行操作./mongouse ...
- 对拍BAT
:loop makedata.exe K.exe Kture.exe fc a.out b.out if %errorlevel%==0 goto loop pause
- q.js实现nodejs顺序调用
nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise 什么是promise promise一个标准,它描述了异步调用的返回结果,包括正确 ...
- Google Map 根据坐标 获取地址信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...