您的位置:首页 > 其它

使用AbstractWizardFormController提交复杂表单的验证解决方案

2007-06-11 17:37 661 查看
使用其它类型的命令控制器,命令对象(POJO JavaBean)只装载一次,使用向导控制器,用户没完成向导页面中的一步,都会有一个命令对象装载。使用向导,我们只做一次验证是不可行的,因为如果你检查的太早的话,找到的验证问题可能是由于用户没有完成向导而导致的,相反,在完成按钮按下后检查就太迟了,因为发现的问题可能跨越了多了页面,我们应该回退到哪个页面呢?

根据 http://blog.csdn.net/daryl715/archive/2007/06/11/1647870.aspx这篇心得的实现,我们进行修改,加入验证功能

首先编写JavaBean Vote的验证单元




package model;




import org.springframework.validation.Errors;


import org.springframework.validation.Validator;






public class FeedBackValidator implements Validator ...{






public boolean supports(Class cls) ...{


return cls.equals(Vote.class);


}






public void validate(Object object, Errors errors) ...{





}







public void validateName(String name,Errors errors)...{




if(name==null||name.equals(""))...{


errors.rejectValue("name", "not null",null, "name必须输入");


}


}




public void validateOption(String option,Errors errors)...{




if(option==null||option.equals(""))...{


errors.rejectValue("option", "not null",null, "option必须输入");


}


}




public void validateResult(String result,Errors errors)...{




if(result==null||result.equals(""))...{


errors.rejectValue("result", "not null",null, "result必须输入");


}


}




public void validateId(String id,Errors errors)...{




if(id==null||id.equals(""))...{


errors.rejectValue("id", "not null",null, "id必须输入");


}


}




}





修改train-servlet.xml,注入validator




<bean id="FeedbackController" class="Action.FeedBackWizardController">


<property name="successView"><value>formWizard/thankyou</value> </property>


<property name="cancelView"><value>formWizard/first</value> </property>


<property name="commandClass"><value>model.Vote</value></property>


<property name="validator">


<bean class="model.FeedBackValidator"/>


</property>


<property name="pages">


<list>


<value>formWizard/first</value>


<value>formWizard/id</value>


<value>formWizard/name</value>


<value>formWizard/option</value>


<value>formWizard/result</value>


</list>


</property>


</bean>



修改页面,以id.jsp为例

<body>
<spring:bind path="command.id">
<form action="feedback.mvc" method="post">
id: <input type="text" name="id" value="<c:out value="${status.value}"/>"/>
<c:if test="${status.error}">
<font color="#FF0000">
错误:
<c:forEach items="${status.errorMessages}" var="error">
<c:out value="${error}"/>
</c:forEach>
</font>
</c:if>
<input type="submit" value="下一步" name="_target2" />

<input type="submit" value="取消" name="_cancel"/>
<input type="submit" value="完成" name="_finish"/>
</form>
</spring:bind>
</body>



其他页面和id.jsp类似

控制器:新覆盖了validatePage方法,实现验证功能




package Action;




import java.util.Enumeration;




import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;




import model.FeedBackValidator;


import model.Vote;




import org.springframework.validation.BindException;


import org.springframework.validation.Errors;


import org.springframework.web.servlet.ModelAndView;


import org.springframework.web.servlet.mvc.AbstractWizardFormController;


import org.springframework.web.util.WebUtils;






public class FeedBackWizardController extends AbstractWizardFormController ...{




private String successView;


private String cancelView;






protected void validatePage(Object object, Errors errors, int page,boolean isFinish) ...{


Vote vote=(Vote)object;


FeedBackValidator feedBackValidator=(FeedBackValidator)getValidator();




if(page==1)...{//检查第一页的id


feedBackValidator.validateId(vote.getId(), errors);


}




if(page==2)...{//检查第二页的name


feedBackValidator.validateName(vote.getName(), errors);


}




if(page==3)...{//检查第三页的option


feedBackValidator.validateOption(vote.getOption(), errors);


}




if(page==4)...{//检查第四页的result


feedBackValidator.validateResult(vote.getResult(), errors);


}




if(isFinish)...{


//表单向导结束


System.out.println("form finished");


}


}






public String getCancelView() ...{


return cancelView;


}






public void setCancelView(String cancelView) ...{


this.cancelView = cancelView;


}






public String getSuccessView() ...{


return successView;


}






public void setSuccessView(String successView) ...{


this.successView = successView;


}






protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object object, BindException exception) throws Exception ...{




return new ModelAndView(this.getCancelView());


}







protected ModelAndView processFinish(HttpServletRequest request,


HttpServletResponse response, Object object, BindException exception)




throws Exception ...{


Vote vote=(Vote)object;





return new ModelAndView(this.getSuccessView(),"vote",vote);


}




}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐