无法使用 Struts 中的注释调用操作 2
Can't call action using annotation in Struts 2
这是我的操作 class:
FirstAction.java
:
@Namespace(value = "/User")
public class FirstAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Action(value = "run4", results = { @Result(name = "task1", location = "task1.jsp") })
private String task1() {
return "task1";
}
@Action(value = "run5", results = { @Result(name = "task2", location = "task2.jsp") })
private String task2() {
return "task2";
}
@Action(value = "run6", results = { @Result(name = "task3", location = "task3.jsp") })
private String task3() {
return "task3";
}
}
SecondAction.java
:
@Namespace(value = "/Admin")
public class SecondAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Action(value = "run1", results = { @Result(name = "task1", location = "task1.jsp") })
private String task1() {
return "task1";
}
@Action(value = "run2", results = { @Result(name = "task2", location = "task2.jsp") })
private String task2() {
return "task2";
}
@Action(value = "run3", results = { @Result(name = "task3", location = "task3.jsp") })
private String task3() {
return "task3";
}
}
struts.xm
l:
<struts>
<constant name="struts.action.extension" value="html" />
<package name="default" extends="struts-default"></package>
</struts>
web.xml
:
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.sem4.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Session Config -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
这是redirect.jsp
:
<div>
<a href='<s:url action="run1" namespace="Admin"></s:url>'>Action run1, namespace Admin</a>
<br>
<a href='<s:url action="run2" namespace="Admin"></s:url>'>Action run2, namespace Admin</a>
<br>
<a href='<s:url action="run3" namespace="Admin"></s:url>'>Action run3, namespace Admin</a>
</div>
<div>
<a href='<s:url action="run4" namespace="User"></s:url>'>Action run1, namespace User</a>
<br>
<a href='<s:url action="run5" namespace="User"></s:url>'>Action run2, namespace User</a>
<br>
<a href='<s:url action="run6" namespace="User"></s:url>'>Action run3, namespace User</a>
</div>
还有我的图书馆:
antlr-2.7.6.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
freemarker-2.3.13.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-validator.jar
hibernate3.jar
hsqldb.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
junit-3.8.1.jar
log4j-1.2.15.jar
mysql-connector-java-5.0.7-bin.jar
ognl-2.6.11.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
struts2-convention-plugin-2.1.6.jar
struts2-core-2.1.6.jar
struts2-fullhibernatecore-plugin-1.4-GA.jar
xwork-2.1.2.jar
当我单击 redirect.jsp
中的 link 调用操作时,例如:在命名空间 "User"
中调用操作 run4
,它会抛出
HTTP Status 404 - There is no Action mapped for namespace /User and action name run4
我正在尝试找出导致此错误的原因,但看起来我的代码没有问题。
动作方法应该是public
。 Struts2 不允许将操作映射到 private
方法,例如
@Action(value = "run4", results = { @Result(name = "task1", location = "task1.jsp") })
public String task1() {
return "task1";
}
此外,无需通过约定插件编写注释来配置操作。它已经创建了一个可以按约定使用的配置。您可以查看文档 page 以了解如何使用此配置。
这是我的操作 class:
FirstAction.java
:
@Namespace(value = "/User")
public class FirstAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Action(value = "run4", results = { @Result(name = "task1", location = "task1.jsp") })
private String task1() {
return "task1";
}
@Action(value = "run5", results = { @Result(name = "task2", location = "task2.jsp") })
private String task2() {
return "task2";
}
@Action(value = "run6", results = { @Result(name = "task3", location = "task3.jsp") })
private String task3() {
return "task3";
}
}
SecondAction.java
:
@Namespace(value = "/Admin")
public class SecondAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Action(value = "run1", results = { @Result(name = "task1", location = "task1.jsp") })
private String task1() {
return "task1";
}
@Action(value = "run2", results = { @Result(name = "task2", location = "task2.jsp") })
private String task2() {
return "task2";
}
@Action(value = "run3", results = { @Result(name = "task3", location = "task3.jsp") })
private String task3() {
return "task3";
}
}
struts.xm
l:
<struts>
<constant name="struts.action.extension" value="html" />
<package name="default" extends="struts-default"></package>
</struts>
web.xml
:
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.sem4.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Session Config -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
这是redirect.jsp
:
<div>
<a href='<s:url action="run1" namespace="Admin"></s:url>'>Action run1, namespace Admin</a>
<br>
<a href='<s:url action="run2" namespace="Admin"></s:url>'>Action run2, namespace Admin</a>
<br>
<a href='<s:url action="run3" namespace="Admin"></s:url>'>Action run3, namespace Admin</a>
</div>
<div>
<a href='<s:url action="run4" namespace="User"></s:url>'>Action run1, namespace User</a>
<br>
<a href='<s:url action="run5" namespace="User"></s:url>'>Action run2, namespace User</a>
<br>
<a href='<s:url action="run6" namespace="User"></s:url>'>Action run3, namespace User</a>
</div>
还有我的图书馆:
antlr-2.7.6.jar
commons-collections-3.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
freemarker-2.3.13.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-validator.jar
hibernate3.jar
hsqldb.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
junit-3.8.1.jar
log4j-1.2.15.jar
mysql-connector-java-5.0.7-bin.jar
ognl-2.6.11.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
struts2-convention-plugin-2.1.6.jar
struts2-core-2.1.6.jar
struts2-fullhibernatecore-plugin-1.4-GA.jar
xwork-2.1.2.jar
当我单击 redirect.jsp
中的 link 调用操作时,例如:在命名空间 "User"
中调用操作 run4
,它会抛出
HTTP Status 404 - There is no Action mapped for namespace /User and action name run4
我正在尝试找出导致此错误的原因,但看起来我的代码没有问题。
动作方法应该是public
。 Struts2 不允许将操作映射到 private
方法,例如
@Action(value = "run4", results = { @Result(name = "task1", location = "task1.jsp") })
public String task1() {
return "task1";
}
此外,无需通过约定插件编写注释来配置操作。它已经创建了一个可以按约定使用的配置。您可以查看文档 page 以了解如何使用此配置。