调用action时struts2注释错误

时间:2014-07-18 13:52:53

标签: java spring struts2 struts2-convention-plugin action-mapping

大家好我在使用Struts2 Annotated Actions时遇到了问题

pom.xml我有

<!-- STRUTS2 annotation -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-convention-plugin</artifactId>
    <version>2.3.16.3</version>
</dependency>

<!-- STRUTS2 SPRING integration -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>2.3.15.1</version>
</dependency> 

web.xml

<filter>
  <filter-name>struts2</filter-name>
    <filter-class>com.agiletec.apsadmin.system.dispatcher.StrutsPrepareAndExecuteFilter</filter-class>
      <init-param>
      <param-name>struts.devMode</param-name>
      <param-value>true</param-value>        
      </init-param>
      <init-param>
      <param-name>struts.objectFactory</param-name>
      <param-value>spring</param-value>        
      </init-param>        
<init-param>
    <param-name>actionPackages</param-name>
    <param-value>it.aicof.projects.openMED.aps.internalservlet.actions</param-value>
</init-param>          
</filter>  

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/do/*</url-pattern>
</filter-mapping>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/struts/*</url-pattern>
</filter-mapping>

我有两个动作类:

第一个

@Namespace("/budget")
@SuppressWarnings("serial")
public class BudgetAction extends BaseAction {

    @Action(value = "/start", results = { @Result(name = "success", location = "/WEB-INF/openmed/aps/jsp/internalservlet/budget/budget.jsp") })
    public String start(){
.......

第二个

@Namespace("/upload")
@SuppressWarnings("serial")
public class UploadAction extends BaseAction {

    @Action(value = "/init", results = {
        @Result(name = "success", location = "/WEB-INF/openmed/aps/jsp/internalservlet/upload/upload.jsp")})
    public String init() {
        .....

如果我打电话给第一个http://localhost:8080/openMED/do/budget/start.action一切正常 但当我拨打第二个http://localhost:8080/openMED/do/upload/init.action时,我收到此错误

 There is no Action mapped for namespace [/do] and action name [init] associated with context path [/openMED]. - [unknown location]

    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:744)

出了什么问题?两个类以相同的方式配置。

1 个答案:

答案 0 :(得分:1)

根据错误消息

  

2014-07-18 15:49:15,454 - ERROR - Dispatcher - 处理请求期间发生异常:没有为名称空间[/ do]映射的Action和与上下文路径[/ openMED]关联的action name [init]。 - [未知位置]。

您使用了网址/openMED/do/upload/init.action,但您的操作已映射到命名空间/upload。命名空间是从上下文路径到操作名称的路径。您可以找到有关namespace configuration的更多信息。您还可以查看this示例以更好地理解名称空间概念。

您应该更改动作映射

@Namespace("/do/upload")
@SuppressWarnings("serial")
public class UploadAction extends BaseAction {

@Action(value = "init", results = {@Result(name = "success", location = "/WEB-INF/openmed/aps/jsp/internalservlet/upload/upload.jsp")})
public String init() {