【问题标题】:How to use a servlet in annotations based struts2 project having no struts.xml如何在没有 struts.xml 的基于注释的 struts2 项目中使用 servlet
【发布时间】:2017-11-15 19:57:27
【问题描述】:

我是 Struts 新手,我必须向旧系统添加功能。

我必须创建一个需要从 JSP 页面访问的 servlet。我创建了一个并在 web.xml 文件中添加了 servlet-mapping,但它总是显示如下错误:

There is no Action mapped for namespace [/myServlet] and action name
   [test] associated with context path [/MyProject]

我已经搜索了解决方案,但我得到的here 是针对 sturts.xml 条目的,我没有使用 struts.xml,而是使用注释。

有人可以告诉我如何在基于注释的 Struts2 项目中使用 excludePattern 吗? 注意:我尝试在我的 web.xml 中添加排除模式,如下所示:

<init-param>
  <param-name>struts.action.excludePattern</param-name>
  <param-value>/FileUploadServlet</param-value>
</init-param>

但是还是不行。

【问题讨论】:

  • 只需创建 struts.xml 或使用属性。旁注:您确定需要 servlet?
  • 感谢@AleksandrM 的回复。实际上我确实需要servlet。我想知道如果我创建 struts.xml,那么我的注释会按原样工作还是我必须在 struts.xml 中添加映射而不是注释?
  • 它会起作用的。实际上,您可以将 struts.xml 定义的操作与注释混合使用。另外因为struts.action.excludePattern 采用模式,您可以将.* 添加到映射中,以便使用多个servlet。见Jersey with Struts2

标签: servlets struts2


【解决方案1】:

好吧,我已经通过将 struts.xml 添加到我的项目并仅在其中添加常量来解决问题,如下所示:

<constant name="struts.action.excludePattern" value="/MyServlet"/>

我首先想知道我将不得不手动提供所有映射而不是注释,但它没有这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-21
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多