您的位置:首页 > 编程语言 > Java开发

struts第十天-----对action中的所有方法进行输入校验

2012-10-15 00:27 459 查看
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<constant name="struts.i18n.encoding" value="utf-8"/>

<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false"/> -->

<constant name="struts.action.extension" value="do,action"/>

<constant name="struts.multipart.maxSize" value="10701096"/>

<package name="person" namespace="/person" extends="struts-default">

<action name="manage_*" class="com.isoftstone.study.PersonAction" method="{1}">

<result name="input">/index.jsp</result>

<result name="message">/WEB-INF/page/message.jsp</result>

</action>

</package>

</struts>

package com.isoftstone.study;

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(username.trim())){

this.addFieldError("username", "用户名不能为空");

}

if(this.mobile==null||"".equals(mobile.trim())){

this.addFieldError("mobile", "手机号不能为空");

}else{

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>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">

</head>

<body>

<s:fielderror/>

<form action="<%=request.getContextPath() %>/person/manage_save.do"} method="post">

用户名:<input name="username" type="text"/>不能为空<br/>

手机号:<input name="mobile" type="text"/>必能为空,要符合手机的格式<br/>

<input value="提交" type="submit"/>

</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}<br/>

</body>

</html>

如果想对action中的指定方法进行输入校验,只需要validateXxx()就行了。

package com.isoftstone.study;

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(username.trim())){

// this.addFieldError("username", "用户名不能为空");

// }

// if(this.mobile==null||"".equals(mobile.trim())){

// this.addFieldError("mobile", "手机号不能为空");

// }else{

// if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){

// this.addFieldError("mobile", "手机格式不正确");

// }

// }

// }

// @Override

public void validateUpdate(){//会对该action中的所有方法进行校验

if(this.username==null||"".equals(username.trim())){

this.addFieldError("username", "用户名不能为空");

}

if(this.mobile==null||"".equals(mobile.trim())){

this.addFieldError("mobile", "手机号不能为空");

}else{

if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){

this.addFieldError("mobile", "手机格式不正确");

}

}

}

如果校验真的是没有问题的,那么还是总是跳到input视图,那么就是类型转换器的问题。

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐