struts2 输入校验

1.使用手工编写代码校验:
package com.ethan.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("msg", "更新成功!");
		return "message";
	}
	
	public String save() {
		ActionContext.getContext().put("msg", "保存成功!");
		return "message";
	}
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if(this.username==null||"".equals(this.username.trim())) {
			ActionContext.getContext().put("username", username);
			this.addFieldError("username", "用户名不能为空");
		}
		if(this.mobile==null||"".equals(this.mobile.trim())) {
			ActionContext.getContext().put("mobile", mobile);
			this.addFieldError("mobile", "电话号码不能为空");
		} else {
			if(!Pattern.compile("^1[358]\\d{9}{1}quot;).matcher(this.mobile).matches()) {
				ActionContext.getContext().put("mobile", mobile);
				this.addFieldError("mobile", "手机号码格式不正确");
			}
		}
		
	}
}

jsp输入页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <%=new Date() %>中国<br/>
    <s:fielderror/>
    <form action="${pageContext.request.contextPath }/person/manage_save.do" method="post">
    	用户名:<input type="text" name="username" value="${param.username }"/><br/>
    	手机号:<input type="text" name="mobile" value="${param.mobile }"/>
    	<input type="submit" value="提交"/>
    </form>
  </body>
</html>

2.基于xml输入校验:

PersonAction-validation.xml放在action对应的包下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator 1.0.3//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">

<!-- START SNIPPET: validators-default -->
<validators>
	
	<field name="username">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>用户名不能为空!</message>
		</field-validator>
	</field>
	
	<field name="mobile">
	
		<field-validator type="requiredstring">
			<message>手机号码不能为空!</message>
		</field-validator>
		<field-validator type="regex">
			<param name="expression"><![CDATA[^1[358]\\d{9}$]]></param>
			<message>手机号码格式不正确</message>
		</field-validator>
	</field>
</validators>

xml配置方式 对指定方法 manage_save--->action name
PersonAction-manage_save-validation.xml


校验文件搜索顺序:
1.ActionClassName-validation.xml
2.ActionClassName-ActionName-validation.xml
如果规则冲突,使用后面的


如果action继承了父类action,则先搜索父类的校验规则,然后它自己,最后汇总

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值