最近看到了webwork输入校验,相关代码如下:
LoginAction.java代码
- import com.opensymphony.xwork.ActionSupport;
- ......
- public class LoginAction extends ActionSupport implements Action, ModelDriven {
- private final static String LOGIN_FAIL="loginfail";
- LoginInfo loginInfo = new LoginInfo();
-
- public String execute() throws Exception {
- if ("erica".equalsIgnoreCase(loginInfo.getUsername()) && "mypass".equals(loginInfo.getPassword())) {
- return SUCCESS;
- } else {
- System.out.println("=======================================================");
- return LOGIN_FAIL;
- }
- }
- public Object getModel() {
- return loginInfo;
- }
- }
LoginAction-validation.xml和LoginAction.java在同一个目录下
LoginAction-validation.xml代码
- <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWorkValidator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
- <validators>
- <field name="model.username">
- <field-validator type="requiredstring">
- <message>Please enter Username!</message>
- </field-validator>
- </field>
-
- <field name="model.password">
- <field-validator type="stringlength">
- <param name="minLength">4</param>
- <param name="maxLength">6</param>
- <message>Password length must between ${minLength} and ${maxLength} chars!</message>
- </field-validator>
- </field>
- </validators>
xwork.xml代码
- <!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
- "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
- <xwork>
- <include file="webwork-default.xml" />
- <package name="default" extends="webwork-default">
-
- <interceptors>
- <interceptor-stack name="modelParamsStack">
- <interceptor-ref name="params" />
- <interceptor-ref name="model-driven" />
- </interceptor-stack>
- </interceptors>
-
- <action name="login" class="net.xiaxin.webwork.action.LoginAction">
- <result name="success" type="dispatcher">
- <param name="location">/main.jsp</param>
- </result>
- <result name="loginfail" type="dispatcher">
- <param name="location">/validatorindex.jsp</param>
- </result>
- <interceptor-ref name="modelParamsStack"/>
- <interceptor-ref name="validationWorkflowStack" />
- </action>
- </package>
- </xwork>
validators.xml和xwork.xml都放在classes根目录下
validators.xml代码
- <validators>
- <validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>
-
- <validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>
-
- </validators>
validatorindex.jsp代码
- <%@ taglib prefix="ww" uri="/webwork"%>
- <style type="text/css">
- .errorMessage {
- color: red;
- }
- </style>
- <form action="login.action">
- 登录<br>
- <ww:if test="hasFieldErrors()">
- <span class="errorMessage">
- <b>Errors:</b><br>
- <ww:iterator value="fieldErrors">
- <li><ww:property value="value[0]"/></li>
- </ww:iterator>
- </span>
- </ww:if>
- 用户名:
- 左尖括号 input type="text" name="model.username" />
-
- 密 码 :
- 左尖括号 input type="password" name="model.password" />
-
- 左尖括号 input type="submit" value="提交" name="B1"/>
- 左尖括号 input type="reset" value="重置" name="B2"/>
- </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
这是什么原因呢?