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




 
 
 
 
  
   
   /index.jsp  
   /WEB-INF/page/message.jsp    
  
  
 
 
  

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


     
    My JSP 'index.jsp' starting page
 
 
    
 
      
    
       


     用户名:不能为空

     手机号:必能为空,要符合手机的格式

     
   

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


    
    结果   
 
 
  
   
 
    ${message}
    
 

 

如果想对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视图,那么就是类型转换器的问题。
 

}

 


 


 

 

你可能感兴趣的:(action,struts,string,mobile,手机,null,java)