msmani
msmani

Reputation: 720

Struts 2 validation.xml not working

I am trying to do simple validation using validaton.xml, the validation is working for username which is of type string but not for password which is of type int.

Action class

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>

The error reported was

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

Login.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 version : 2.3.15

Upvotes: 1

Views: 2554

Answers (2)

msmani
msmani

Reputation: 720

If I use Integer instead of int then it works fine.

Upvotes: 0

Atropo
Atropo

Reputation: 12541

IMO looks like that you're using a required for an int field. I guess you should use an int type of validator. The required validator checks for null, struts2 required validator.

You should do something like:

   <field name="password"> 
        <field-validator type="int"> 
            <param name="min">0</param>
            <message>Please enter a password</message> 
        </field-validator> 
    </field> 
</validators>

Upvotes: 0

Related Questions