java - Struts2 Action 正则表达式/URL 通配符

标签 java struts2

我是 Struts2 的新手,但到目前为止,我在使用 API 方面取得了不错的进步。但是,我被困在一些我需要摆脱的事情上。我正在使用 Struts2 与 Spring 集成。我正在编写带有注释的 Action 类,就像你们中的许多人一样,我喜欢注释。

我的要求是 URL 具有以下性质:

http://<DOMAIN>/program/program1.jspx
http://<DOMAIN>/program/program2.jspx
http://<DOMAIN>/program/program3.jspx

如您所见,URL 中有一个特定的模式,program1、program2 和 program3 各不相同,其余都是静态的。我已经很容易地使用 Spring MVC 处理了类似的情况(我没有为当前项目选择使用 Spring MVC 的选项),比如我的其他项目中的 "/program/{program_name}.jspx"

但是当我在 struts2 中使用相同的方法时,我得到了错误。我的 Action 类如下:

@Result(name="program", location="program", type="tiles")
public class ProgramAction extends ActionSupport {

   @Action("program/{programName}")
   public String getProgramPage() {
     // few more lines of code  
     return "program";      
  }
}

错误是

2013-02-28 15:46:35.591 WARN  [http-bio-8080-exec-3] CommonsLogger.java:60 
Could not find action or result
com.opensymphony.xwork2.config.ConfigurationException: There is no Action mapped for namespace / and action name program1.
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189) ~[xwork-core-2.2.1.jar:2.2.1]
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61) ~[struts2-core-2.2.1.jar:2.2.1]
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39) ~[struts2-core-2.2.1.jar:2.2.1]
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58) ~[xwork-core-2.2.1.jar:2.2.1]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:475) ~[struts2-core-2.2.1.jar:2.2.1]
....
....

我的struts.xml文件如下:

<struts>
<constant name="struts.convention.default.parent.package" value="default"/>
<constant name="struts.action.extension" value="jspx" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />

<package name="default" extends="struts-default, json-default, rest-default" namespace="/">       
    <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
        <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
    </result-types>
</package>
</struts>

最佳答案

您的操作名称中有一个斜线。 struts默认不喜欢这样改正补充:

<constant name="struts.enable.SlashesInActionNames" value="true"/>

关于java - Struts2 Action 正则表达式/URL 通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145933/

相关文章:

java - 从另一个类中调用一个类

java - GCM 未在 Android 4.0.4 设备上接收消息

java - 没有关系数据库的多语言网站

java - Struts2登录MySQL错误

java - 如何在 Java EE 中检查用户是否登录

struts2 - 如何在 Struts 2 中获取请求 URL?

java - 矩阵乘法-java中的双数组计算

java - 使用spring r2dbc时找不到javax.persistence注解

java - MissingResourceException - 找不到基本名称的包

apache - 调用 tiles 时 putAttribute 中的 NPE