Struts 2自定义验证器问题

时间:2010-08-04 17:34:20

标签: struts2

我有一个针对CRUD操作的Action和3个自定义验证器。

两个验证器正常工作,但另一个验证器没有。它正确处理验证,并返回相应的错误,但如果没有错误,工作流程无法完成,它永远不会到达Action。就好像它被卡在验证器中一样。日志文件中没有错误。如果我删除验证程序,则会到达操作。

我不确定我做错了什么,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

package com.timesheet.validator;

import java.util.Map;

import com.timesheet.action.TimeSheetAction;


import com.timesheet.util.TimeSheetClient;

import com.timesheet.util.TimeSheetHolder;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.ValidatorSupport;

public class TimeSheetSubmission extends ValidatorSupport{


 @Override
 public void validate(Object arg0) throws ValidationException {
  // TODO Auto-generated method stub

  Map session = ActionContext.getContext().getSession();
  TimeSheetAction act = (TimeSheetAction)ActionContext.getContext().getActionInvocation().getAction();
  TimeSheetClient client = act.getClient();

  if(session.get("holder") != null){
   TimeSheetHolder holder = (TimeSheetHolder)session.get("holder");

       if(!(excessTime(holder) && lessTime(holder))){

        client.cleanUp();
     addActionError(arg0);
     return;
    }else{

     client.cleanUp();
     return;
    }

  }
 }
 public boolean lessTime(TimeSheetHolder holder){
  boolean done = true;
  TimeSheetHolder.Day day = holder.dailyHours();
  switch(day){

   case Mon: setMessageKey("timeSheet.mon.less.hours"); 
        done = false;  
      break;
   case Tue: done = false;  
      setMessageKey("timeSheet.tue.less.hours"); 
      break;
   case Wed: done = false;  
      setMessageKey("timeSheet.wed.less.hours"); 
      break;
   case Thu: done = false;  
      setMessageKey("timeSheet.thu.less.hours"); 
      break;
   case Fri: done = false;  
      setMessageKey("timeSheet.fri.less.hours"); 
      break;
   default:  break;
   }
  return done;
 }
 public boolean excessTime(TimeSheetHolder holder){
  boolean done = true;
  TimeSheetHolder.Day day = holder.moreTime();
  switch(day){

   case Mon: setMessageKey("timeSheet.mon.excess.hours"); 
        done = false;  
      break;
   case Tue: done = false;  
      setMessageKey("timeSheet.tue.excess.hours"); 
      break;
   case Wed: done = false;  
      setMessageKey("timeSheet.wed.excess.hours"); 
      break;
   case Thu: done = false;  
      setMessageKey("timeSheet.thu.excess.hours"); 
      break;
   case Fri: done = false;  
      setMessageKey("timeSheet.fri.excess.hours"); 
      break;
   default:  break;
   }
  return done;
 }

}