当前位置: 代码迷 >> Web前端 >> 有关webwork输入校验的有关问题
  详细解决方案

有关webwork输入校验的有关问题

热度:277   发布时间:2012-11-01 11:11:33.0
有关webwork输入校验的问题

最近看到了webwork输入校验,相关代码如下:
LoginAction.java代码

  1. import?com.opensymphony.xwork.ActionSupport; ??
  2. ...... ??
  3. public?class?LoginAction?extends?ActionSupport?implements?Action,?ModelDriven?{ ??
  4. ????private?final?static?String?LOGIN_FAIL="loginfail"; ??
  5. ????LoginInfo?loginInfo?=?new?LoginInfo(); ??
  6. ??? ??
  7. ????public?String?execute()?throws?Exception?{ ??
  8. ???????if?("erica".equalsIgnoreCase(loginInfo.getUsername())??&&?"mypass".equals(loginInfo.getPassword()))?{ ??
  9. ???????????return?SUCCESS; ??
  10. ???????}?else?{ ??
  11. ???????????System.out.println("======================================================="); ??
  12. ???????????return?LOGIN_FAIL; ??
  13. ???????}???????? ??
  14. ???} ??
  15. ???public?Object?getModel()?{ ??
  16. ???????return?loginInfo; ??
  17. ???} ??
  18. }??

LoginAction-validation.xml和LoginAction.java在同一个目录下
LoginAction-validation.xml代码

  1. <!DOCTYPE?validators?PUBLIC?"-//OpenSymphony?Group//XWorkValidator?1.0.2//EN" ??
  2. "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> ??
  3. <validators> ??
  4. ?<field?name="model.username"> ??
  5. ??<field-validator?type="requiredstring"> ??
  6. ???<message>Please?enter?Username!</message> ??
  7. ??</field-validator> ??
  8. ?</field> ??
  9. ??
  10. ?<field?name="model.password"> ??
  11. ??<field-validator?type="stringlength"> ??
  12. ???<param?name="minLength">4</param> ??
  13. ???<param?name="maxLength">6</param> ??
  14. ???<message>Password?length?must?between?${minLength}?and?${maxLength}?chars!</message> ??
  15. ??</field-validator> ??
  16. ?</field> ??
  17. </validators> ??

xwork.xml代码

  1. <!DOCTYPE?xwork?PUBLIC?"-//OpenSymphony?Group//XWork?1.0//EN" ??
  2. "http://www.opensymphony.com/xwork/xwork-1.0.dtd"> ??
  3. <xwork> ??
  4. ?<include?file="webwork-default.xml"?/> ??
  5. ?<package?name="default"?extends="webwork-default"> ??
  6. ? ??
  7. ??<interceptors> ??
  8. ???<interceptor-stack?name="modelParamsStack"> ??
  9. ????<interceptor-ref?name="params"?/> ??
  10. ????<interceptor-ref?name="model-driven"?/> ??
  11. ???</interceptor-stack> ??
  12. ??</interceptors> ??
  13. ?? ??
  14. ??<action?name="login"?class="net.xiaxin.webwork.action.LoginAction"> ??
  15. ???<result?name="success"?type="dispatcher"> ??
  16. ????<param?name="location">/main.jsp</param> ??
  17. ???</result> ??
  18. ???<result?name="loginfail"?type="dispatcher"> ??
  19. ????<param?name="location">/validatorindex.jsp</param> ??
  20. ???</result> ??
  21. ???<interceptor-ref?name="modelParamsStack"/>? ??
  22. ???<interceptor-ref?name="validationWorkflowStack"?/> ??
  23. ??</action> ??
  24. ?</package> ??
  25. </xwork> ??


validators.xml和xwork.xml都放在classes根目录下

validators.xml
代码

  1. <validators>? ??
  2. ????<validator?name="requiredstring"?class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/> ??
  3. ??
  4. ????<validator?name="stringlength"?class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/> ??
  5. ???? ??
  6. </validators> ??


validatorindex.jsp代码

  1. <%@?taglib?prefix="ww"?uri="/webwork"%> ??
  2. ?<style?type="text/css"> ??
  3. ??.errorMessage?{ ??
  4. ???color:?red; ??
  5. ??} ??
  6. ?</style>? ??
  7. ??<form?action="login.action"> ??
  8. ????登录<br> ??
  9. ???<ww:if?test="hasFieldErrors()"> ??
  10. ????<span?class="errorMessage"> ??
  11. ?????<b>Errors:</b><br> ??
  12. ?????<ww:iterator?value="fieldErrors"> ??
  13. ??????<li><ww:property?value="value[0]"/></li> ??
  14. ?????</ww:iterator> ??
  15. ????</span> ??
  16. ???</ww:if> ??
  17. ??用户名: ??
  18. ??左尖括号??input?type="text"?name="model.username"?/> ??
  19. ??
  20. ??密?码?: ??
  21. ??左尖括号?input?type="password"?name="model.password"?/> ??
  22. ??
  23. ???左尖括号?input?type="submit"?value="提交"?name="B1"/> ??
  24. ???左尖括号?input?type="reset"?value="重置"?name="B2"/> ??
  25. ?</form>??

运行时,若输入正确的用户名和密码,按提交按钮后可以转向正确的页面。若输入错误的用户名或密码,按提交按钮则页面空白一片。在myeclipse下看到如下信息:
DefaultWorkflowInterceptor - Invoking validate() on action net.xiaxin.webwork.action.LoginAction@a47cc3
PrefixMethodInvocationUtil - cannot find method [validateExecute] in action [net.xiaxin.webwork.action.LoginAction@a47cc3]
PrefixMethodInvocationUtil - cannot find method [validateDoExecute] in action [net.xiaxin.webwork.action.LoginAction@a47cc3]
DefaultWorkflowInterceptor - Errors on action net.xiaxin.webwork.action.LoginAction@a47cc3, returning result name 'input'
DefaultActionInvocation - No result defined for action net.xiaxin.webwork.action.LoginAction and result input
这是什么原因呢?

1 楼 dwr 2006-12-08  
已经知道问题所在,少了input,偶闪~