使用AbstractWizardFormController提交复杂表单的验证解决方案
2007-06-11 17:37
661 查看
使用其它类型的命令控制器,命令对象(POJO JavaBean)只装载一次,使用向导控制器,用户没完成向导页面中的一步,都会有一个命令对象装载。使用向导,我们只做一次验证是不可行的,因为如果你检查的太早的话,找到的验证问题可能是由于用户没有完成向导而导致的,相反,在完成按钮按下后检查就太迟了,因为发现的问题可能跨越了多了页面,我们应该回退到哪个页面呢?
根据 http://blog.csdn.net/daryl715/archive/2007/06/11/1647870.aspx这篇心得的实现,我们进行修改,加入验证功能
首先编写JavaBean Vote的验证单元
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package model;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.Errors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.Validator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class FeedBackValidator implements Validator ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public boolean supports(Class cls) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return cls.equals(Vote.class);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validate(Object object, Errors errors) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateName(String name,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(name==null||name.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("name", "not null",null, "name必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateOption(String option,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(option==null||option.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("option", "not null",null, "option必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateResult(String result,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(result==null||result.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("result", "not null",null, "result必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateId(String id,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(id==null||id.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("id", "not null",null, "id必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
修改train-servlet.xml,注入validator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="FeedbackController" class="Action.FeedBackWizardController">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="successView"><value>formWizard/thankyou</value> </property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="cancelView"><value>formWizard/first</value> </property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="commandClass"><value>model.Vote</value></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="validator">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="model.FeedBackValidator"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="pages">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/first</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/id</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/name</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/option</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/result</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</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方法,实现验证功能
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Enumeration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import model.FeedBackValidator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import model.Vote;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.BindException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.Errors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.ModelAndView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.util.WebUtils;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class FeedBackWizardController extends AbstractWizardFormController ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String successView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String cancelView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
protected void validatePage(Object object, Errors errors, int page,boolean isFinish) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Vote vote=(Vote)object;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FeedBackValidator feedBackValidator=(FeedBackValidator)getValidator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==1)...{//检查第一页的id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateId(vote.getId(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==2)...{//检查第二页的name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateName(vote.getName(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==3)...{//检查第三页的option
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateOption(vote.getOption(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==4)...{//检查第四页的result
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateResult(vote.getResult(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(isFinish)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//表单向导结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("form finished");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getCancelView() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return cancelView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setCancelView(String cancelView) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.cancelView = cancelView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getSuccessView() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return successView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setSuccessView(String successView) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.successView = successView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object object, BindException exception) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(this.getCancelView());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected ModelAndView processFinish(HttpServletRequest request,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpServletResponse response, Object object, BindException exception)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Vote vote=(Vote)object;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(this.getSuccessView(),"vote",vote);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
根据 http://blog.csdn.net/daryl715/archive/2007/06/11/1647870.aspx这篇心得的实现,我们进行修改,加入验证功能
首先编写JavaBean Vote的验证单元
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package model;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.Errors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.Validator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class FeedBackValidator implements Validator ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public boolean supports(Class cls) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return cls.equals(Vote.class);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validate(Object object, Errors errors) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateName(String name,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(name==null||name.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("name", "not null",null, "name必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateOption(String option,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(option==null||option.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("option", "not null",null, "option必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateResult(String result,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(result==null||result.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("result", "not null",null, "result必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void validateId(String id,Errors errors)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(id==null||id.equals(""))...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
errors.rejectValue("id", "not null",null, "id必须输入");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
修改train-servlet.xml,注入validator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="FeedbackController" class="Action.FeedBackWizardController">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="successView"><value>formWizard/thankyou</value> </property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="cancelView"><value>formWizard/first</value> </property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="commandClass"><value>model.Vote</value></property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="validator">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="model.FeedBackValidator"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="pages">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/first</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/id</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/name</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/option</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>formWizard/result</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</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方法,实现验证功能
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package Action;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Enumeration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import model.FeedBackValidator;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import model.Vote;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.BindException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.validation.Errors;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.ModelAndView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.springframework.web.util.WebUtils;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class FeedBackWizardController extends AbstractWizardFormController ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String successView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String cancelView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
protected void validatePage(Object object, Errors errors, int page,boolean isFinish) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Vote vote=(Vote)object;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FeedBackValidator feedBackValidator=(FeedBackValidator)getValidator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==1)...{//检查第一页的id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateId(vote.getId(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==2)...{//检查第二页的name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateName(vote.getName(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==3)...{//检查第三页的option
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateOption(vote.getOption(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(page==4)...{//检查第四页的result
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
feedBackValidator.validateResult(vote.getResult(), errors);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(isFinish)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//表单向导结束
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("form finished");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getCancelView() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return cancelView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setCancelView(String cancelView) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.cancelView = cancelView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getSuccessView() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return successView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setSuccessView(String successView) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.successView = successView;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
protected ModelAndView processCancel(HttpServletRequest request, HttpServletResponse response, Object object, BindException exception) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(this.getCancelView());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected ModelAndView processFinish(HttpServletRequest request,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpServletResponse response, Object object, BindException exception)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Vote vote=(Vote)object;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return new ModelAndView(this.getSuccessView(),"vote",vote);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- Spring MVC 复杂表单分部提交--使用AbstractWizardFormController解决方案
- liferay中使用jQuery Validation Engine进行ajax验证,不通过也提交表单的解决方案
- 黄聪:jquery mobile使用form进行post提交表单没有反应,显示空白页解决方案
- 使用JS对form的内容验证失败后阻止提交 &&js校验表单后提交表单的三种方法总结
- jQuery form插件的使用--用 formData 参数校验表单,验证后提交(简单验证).
- MVC client validation after PartialView loaded via Ajax MVC3中 弹出 Dialog时候 提交的时候 使用 Jquery 不验证 form表单 的解决办法
- django(7)modelform操作及验证、ajax操作普通表单数据提交、文件上传、富文本框基本使用
- form的onsubmit事件--表单提交前的验证最佳实现方式
- 在使用django 的过程中难免的会使用到format表单进行提交,如果出现“CSRF验证失败. 相应中断”.该如何解决呢?
- Form表单提交前进行JS验证的3种方式
- 使用ajax提交不带文件的form表单
- Easyui1.4使用FORM表单无法sumbit提交(待解决)
- Javascript:使用jQuery提交Form表单
- 使用FormData对象提交表单及上传图片
- form的onsubmit事件--表单提交前的验证最佳实现方式
- 使用jQuery.form插件实现表单异步提交+上传文件
- Jq form表单提交验证与响应简单示例
- 表单验证之validform.js使用方法