zoukankan      html  css  js  c++  java
  • strut2 输入校验

    struts2输入校验分为对action中的所有方法进行校验和对action中的指定方法进行校验。

    校验方式有两种:手工校验和xml文件校验。

    首先是手工校验:

    1. 输入表单如下:
      <%@ page language="java" pageEncoding="UTF-8"%>
      <%@ taglib uri="/struts-tags" prefix="s"%>
      <!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>
         <s:fielderror/>  ----显示错误信息
         <form action="${pageContext.request.contextPath}/person/manage_update.action" method="post">
                 用户名:<input type="text" name="username"/>不能为空<br/>
                 手机号:<input type="text" name="mobile"/>不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br/>
                 <input type="submit" value="提 交"/></form>
        </body>
      </html>
    2. action中的代码:action中的代码需要重写validate方法,进行字段校验,具体代码如下
      package cn.itcast.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("message", "更新成功");
              return "message";
          }
          
          public String save(){
              ActionContext.getContext().put("message", "保存成功");
              return "message";
          }
          
          @Override
          public void validate() {//会对action中的所有方法校验
              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", "手机号格式不正确");
                  }
              }
          }

          public void validateUpdate() {//会对update()方法校验
              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", "手机号格式不正确");
                  }
              }
          } }
    3. 在struts.xml配置文件中需要配置input视图:<result name="input">/index.jsp</result>用来返回错误信息的提示。
    4. 总结:在action中重写validate方法,这是对所有的方法进行校验,validate_*对特定方法进行校验。此外在struts.xml中还需要提供input视图。在input视图中显示错误信息使用标签<s:fielderror/> ----显示错误信息
  • 相关阅读:
    转 [Lucene.Net] 基本用法
    万商网与Alibaba等的比较
    B2B闯入者 新势力正在崛起
    项目管理随想一
    【转载】/proc目录中的重要信息
    文件名乱码转换器
    C函数调用中对入参取地址引发的问题
    编译通过的代码不算什么,一眼能看懂的代码才算好代码
    Handler使用
    删除system/app下的apk
  • 原文地址:https://www.cnblogs.com/kailing-con/p/4196963.html
Copyright © 2011-2022 走看看