我正在使用struts2驗證框架來驗證客戶端中的數據。 我已經閱讀了很多驗證框架教程,但仍然無法找到我犯的錯誤。struts2驗證框架在客戶端不起作用
我使用的是Struts 2.2.3。 struts.xml配置如下:
<?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>
在index.jsp中,我去struts2操作並輸入登錄頁面。
<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註釋用於跳過驗證對於特定方法
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中在同一封裝的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>
在s:form中添加validate =「true」後,我進入頁面並嘗試訪問validation.js和utils.js。他們變成了apache tomcat錯誤報告文件。我認爲這意味着客戶端驗證不起作用。任何人都可以幫我解決它嗎?先謝謝你。
你的過濾器映射是什麼樣的? –
我只是添加strut2 StrutsPrepareAndExecuteFilter和映射到.action –