我已经实现了别名拦截器。现在,当我实现验证拦截器时,验证工作正常,但未显示错误消息。我理解的原因是由于action和jsp页面中的字段名称不同,请求您帮助修复。
代码清单如下所示......
index.jsp 文件是
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts 2</title>
<s:head/>
</head>
<body>
<s:form action="hello">
<s:textfield label="User Name" name="username"/> <br/>
<s:textfield label="User password" name="userpwd"/> <br/>
<s:textfield label="Field 1" name="scrField1"/> <br/>
<s:textfield label="Field 2" name="scrField2"/> <br/>
<s:submit value="Say Hello"/>
</s:form>
</body>
</html>
HelloAction 如下所示:
package org.tp.actions;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
private String name;
private String password;
private String field1;
private String field2;
public String getName() {
return name;
}
public void setName(String nm) {
this.name = nm;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getField1() {
System.out.println("inside HelloAction.getField1 field1 =="+field1);
return field1;
}
public void setField1(String scrField1) {
this.field1 = scrField1;
System.out.println("inside HelloAction.setField1 field1=="+field1);
}
public String getField2() {
System.out.println("inside HelloAction.getField2 field2 =="+field2);
return field2;
}
public void setField2(String scrField2) {
this.field2 = scrField2;
}
public String execute() throws Exception{
return "success";
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello" class="org.tp.actions.HelloAction" method="execute">
<param name="aliases">#{ 'username' : 'name','userpwd' : 'password', 'scrField1' : 'field1', 'scrField2' : 'field2'}</param>
<interceptor-ref name="alias">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<result name="success">/MainPage.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
HelloAction-validation 如下所示:
<?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="name" >
<field-validator type="requiredstring">
<message >User Name is Required.</message>
</field-validator>
</field>
</validators>