Reputation: 237
I am using the struts2 validation framework to validate the data from in client. I have read a lot of validation framework tutorials but still cannot not find where i make the mistake.
I use Struts 2.2.3. The struts.xml configuration as following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="admin" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<action name="login" class="com.text.LoginAction">
<result name="login">/login_page.jsp</result>
<result name="success">/login_success.jsp</result>
<result name="error">/login_page.jsp</result>
</action>
</package>
</struts>
In the index.jsp, i go struts2 action and enter login page.
<body>
<a href="login!handle.action">Go to Login Page</a>
</body>
login_page.jsp
<body>
<s:form action="login!login.action" validate="true" method="post">
<s:textfield name="username" label="Username"/>
<s:textfield name="password" label="Password"/>
<s:submit value="Submit"/>
</s:form>
</body>
LoginAction, @SkipValidation annotation is used to skip the validation for specific method
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
if (username.equalsIgnoreCase("root") && password.equalsIgnoreCase("1234")) {
return "success";
} else {
return "fail";
}
}
@SkipValidation
public String handle() {
return "login";
}
}
LoginAction-validation.xml in the same package with LoginAction.
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Login name is required</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Password is required</message>
</field-validator>
</field>
</validators>
After adding the validate="true" in s:form, i go to the page and try to access validation.js and utils.js. They turn out to be the apache tomcat error report file. I think it means the client side validation doesn't work. Can anyone help me to fix it? Thank you in advance.
Upvotes: 0
Views: 2202
Reputation: 160191
By mapping to .action, instead of the wildcard as documented in the S2 web.xml docs you prevent S2 from serving the required JavaScript files.
You may extract the static files S2 normally serves itself, or correct your mapping.
Upvotes: 1