1

我正在尝试使用 validaton.xml 进行简单验证,验证适用于字符串类型的用户名,但不适用于 int 类型的密码。

动作类

public class LoginAction extends ActionSupport  {
private static final long serialVersionUID = 5271055255991498361L;
private String username;
private int password;

public String execute() {

    if (this.username.equals("admin")&& this.password==123) {
        addActionMessage("Welcome admin");
        return SUCCESS;
    } else {
        addActionError("Invalid credentials");
        return ERROR;
    }
}

public String getUsername() {

    return username;
}

public void setUsername(String username) {

    this.username = username;
}

public int getPassword() {
    return password;
}

public void setPassword(int password) {
    this.password = password;
}
}

LoginAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators> 
    <field name="username"> 
        <field-validator type="requiredstring"> 
            <message>Please enter a user name</message> 
        </field-validator> 
    </field> 

    <field name="password"> 
        <field-validator type="required"> 
            <message>Please enter a password</message> 
        </field-validator> 
    </field> 
</validators>

struts.xml

<struts>
    <constant name="struts.action.extension" value=","/>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="user" namespace="/" extends="struts-default">
        <action name="login" class="net.training.user.action.LoginAction"   method="execute">
          <result name = "input">Login.jsp</result>
          <result name = "error">Login.jsp</result>    
          <result name= "success">/customer/CustomerForm.jsp</result>
        </action>
    </package>

</struts>

报告的错误是

WARNING: Error setting expression 'password' with value '[Ljava.lang.String;@1f2428d'
ognl.MethodFailedException: Method "setPassword" failed for object net.training.user.action.LoginAction@1d532ae [java.lang.NoSuchMethodException: net.training.user.action.LoginAction.setPassword([Ljava.lang.String;)]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1305)
    at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1494)
    ...
Caused by: java.lang.NoSuchMethodException: net.training.user.action.LoginAction.setPassword([Ljava.lang.String;)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1246)
    ... 63 more
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: net.training.user.action.LoginAction.setPassword([Ljava.lang.String;)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1246)
    at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1494)
    ...
\--------------------------------------/
Jul 24, 2013 3:26:18 PM com.opensymphony.xwork2.interceptor.ParametersInterceptor error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'password' on 'class net.training.user.action.LoginAction: Error setting expression 'password' with value '[Ljava.lang.String;@1f2428d'
Error setting expression 'password' with value '[Ljava.lang.String;@1f2428d' - Class: ognl.OgnlRuntime
File: OgnlRuntime.java
Method: callAppropriateMethod
Line: 1305 - ognl/OgnlRuntime.java:1305:-1
    at com.opensymphony.xwork2.ognl.OgnlValueStack.handleOgnlException(OgnlValueStack.java:211)
    at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:172)
    ...
Caused by: ognl.MethodFailedException: Method "setPassword" failed for object net.training.user.action.LoginAction@1d532ae [java.lang.NoSuchMethodException: net.training.user.action.LoginAction.setPassword([Ljava.lang.String;)]
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1305)
    at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1494)
    ...
Caused by: java.lang.NoSuchMethodException: net.training.user.action.LoginAction.setPassword([Ljava.lang.String;)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1246)
    ... 63 more

登录.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h2>Struts 2 - Login Application</h2>

<s:actionerror/>
<s:fielderror/>

<s:form action="login" method="post" namespace="/" > 
    <s:textfield name="username" key="label.username" size="20" />
    <s:textfield name="password" key="label.password" size="20" />
    <s:submit  />
</s:form>
</body>
</html>

Struts 版本:2.3.15

4

2 回答 2

0

IMO 看起来您正在使用 arequired字段int。我想你应该使用一种int验证器。所需的验证器检查nullstruts2 所需的验证器

您应该执行以下操作:

   <field name="password"> 
        <field-validator type="int"> 
            <param name="min">0</param>
            <message>Please enter a password</message> 
        </field-validator> 
    </field> 
</validators>
于 2013-07-24T11:20:50.490 回答
0

如果我使用 Integer 而不是 int 那么它工作正常。

于 2013-07-25T08:21:57.967 回答