使用AbstractWizardFormController提交復雜表單
系統
2168 0
使用其它類型的命令控制器,命令對象(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);
????}
}
使用AbstractWizardFormController提交復雜表單的驗證解決方案
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元