Struts2手工配置对Action方法的校验代码

来源:互联网 发布:增值税打印软件 编辑:程序博客网 时间:2024/06/05 12:07
校验文件:
package com.structs2;

import java.util.Date;
import java.util.regex.Pattern;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWordAction extends ActionSupport {
    private String name;
    private String mobile;
   
   
   
   
    private Date birthday;                //当输入数据时,如果不能转换为Date型数据,则也会把错
    public Date getBirthday() {            //误信息添加到FieldError中,并且自动进入input视图
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

   
   
   
   
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String save() {
        ActionContext.getContext().put("name", name);
        ActionContext.getContext().put("mobile", mobile);
        ActionContext.getContext().put("message", "保存成功");
        return "success";
    }

    public String update() {
        ActionContext.getContext().put("name", name);
        ActionContext.getContext().put("mobile", mobile);
        ActionContext.getContext().put("message", "更新成功");
        return "success";
    }

   
      //手工编写代码对所有Action进行校验 public void validate()
     //对Action所有方法进行校验,需要继承ActionSupport类
    public void validate() {// 对Action update方法进行校验,需要继承ActionSupport类
        if (name == null || "".equals(name.trim()))
            this.addFieldError(name, "用户名不能为空");// 会往fieldError集合中添加错误信息struts会将转化到input视图
        if (mobile == null || "".equals(mobile.trim())) // 默认放在input视图中
            this.addFieldError(mobile, "手机号不能为空");
        else if (!Pattern.compile("^1[358]\\d{9}$").matcher(mobile).matches())// 通过正则表达式来实现
            this.addFieldError(mobile, "手机号格式不正确");
    }










    // 手工编写代码对Action指定方法进行校验
   

   

}

struts.xml
<?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>

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

       
        <action name="update" class="com.structs2.HelloWordAction"
            method="update">
            <result name="success" >/hello.jsp</result>
            <result name="input" >/index.jsp</result>
        </action>
       
        <action name="save" class="com.structs2.HelloWordAction"
            method="save">
            <result name="success" >/hello.jsp</result>
            <result name="input" >/index.jsp</result>
        </action>
    </package>
</struts>
   

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>输入校验</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>
  <s:fielderror/><!-- 显示错误信息 -->
  <h1>保存</h1>
 <form action="test/save" method="post">
 用户名:<input type="text" name="name">不能为空<br/>
 手机号:<input type="text" name="mobile">复合手机号的格式1/3/5/8...后边9个数字<br/>
 <input type="submit" value="提交">
 </form><br/>
 <br/>
  <h1>更新</h1>
  <form action="test/update" method="post">
 用户名:<input type="text" name="name">不能为空<br/>
 手机号:<input type="text" name="mobile">复合手机号的格式1/3/5/8...后边9个数字<br/>
 <input type="submit" value="提交">
 </form>
  </body>
</html>

原创粉丝点击