小弟写了个后台验证,想判断时间,如果大于>24返回到界面显示一个错误,并且数据不保存到数据库里,可是现在第一步作到了,不过还是保存到数据库里了,这是怎么回事?如果>24checkHours已经返回到界面了,为啥save()还执行呢?
public class Times  {
public ActionForward actionExecute(ActionMapping actionMapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
  ......
  checkHours(..);
  save(..);} private void checkHours(String[] sun,HttpServletRequest request,ActionMapping actionMapping)throws Exception
    {
     if(sun!=null)
{
 float a=0;
 float sum=0;
for(int j=0;j<sun.length;j++)
{
if(!sun[j].equals(""))
{
a=Float.parseFloat(sun[j]);
sum+=a;
}
}
if(sum>24)
{
ActionErrors error = new ActionErrors();
error.add(ActionErrors.GLOBAL_ERROR, new ActionError("errors"));
saveErrors(request, error);
actionMapping.findForward("success");

}
}
    }
}

解决方案 »

  1.   

    把private void checkHours改为private boolean checkHours,如果大于24就返回false,这样就不调用save(..);了。
    也不知道为什么actionMapping.findForward之后还继续执行后面的代码,帮你顶
      

  2.   

    你也可以把checkHours的返回值改成int 这样写试试
    if(checkHours(..) == 1){   //1表示成功
    save(..)
    }
      

  3.   

    首先把方法定义成这样的:
    private boolean checkHours(String[] sun,HttpServletRequest request,ActionMapping 
    然后if(checkHours(..))
    {
        save();
    }
    else
    {
        return;
    }
    应该是这样的吧?