【问题标题】:Call a struts2 jsp page from a normal jsp page从普通的jsp页面调用struts2 jsp页面
【发布时间】:2013-09-06 06:03:41
【问题描述】:

我有一个现有的普通 jsp Web 应用程序。 根据客户要求,我需要创建一些带有下拉选择框的配置页面。我必须在 Struts 2 中创建这个页面。 我已经创建了一个 struts 页面,但我无法从我现有的应用程序中调用这个 struts2 页面。

我遇到了错误。

The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
    at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
    at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    at jsp_servlet.__addprevdn._jsp__tag0(__addprevdn.java:112)
    at jsp_servlet.__addprevdn._jspService(__addprevdn.java:84)
    Truncated. see log file for complete stacktrace      

【问题讨论】:

  • 你不能那样做,如果你绕过 Struts2 过滤器,框架还没有准备好为你和标签提供服务,这也违反了 MVC。请弄清楚如何使用struts2

标签: jsp struts2 struts


【解决方案1】:

正如 Umesh Awasthi 所说,您不能这样做。您需要了解一些 Struts2 的基础知识。

  • 首先客户端请求通过Servlet容器,比如Tomcat。然后客户端请求通过一个链式系统,该系统涉及 Servlet 过滤器的三个组件。
  • 在链接系统中,首先HttpServletrequest 通过ActionContextCleanUp 过滤器。 ActionContextCleanUpFilterDispatcherStrutsPrepareAndExecuteFilter 一起使用,并允许与 SiteMesh 集成。
  • 接下来FilterDispatcher 调用ActionMapper 来确定哪个请求应该调用一个动作。它还处理执行操作、清理ActionContext 并在 Struts2 的各个部分需要时提供静态内容,如 JavaScript 文件、CSS 文件、HTML 文件等。
  • ActionMapper 映射 HTTP 请求和操作调用请求,反之亦然。如果没有操作调用请求匹配,ActionMapper 可能会返回一个操作或返回空值。
  • ActionMapper 想要调用某个动作时,通过与FilterDispatcher 交互来调用ActionProxyActionProxy 获取 Action 类并调用适当的方法。此方法参考框架配置管理器,该管理器从struts.xml 文件初始化。
  • 读取struts.xml 文件后,ActionProxy 创建一个ActionInvocation 并确定应如何处理该操作。
  • ActionProxy封装了Action应该如何获取,ActionInvocation封装了Action在请求被调用时是如何执行的。
  • 然后ActionProxyActionInvocation 的帮助下调用拦截器。拦截器的主要任务是使用ActionInvocation调用Action。
  • 一旦确定并执行了动作,就会使用Result 组件生成结果。 Result 组件借助映射在struts.xml 中的Action 结果代码从模板中查找数据。模板可能包含用于生成响应的JSP、FreeMarker。

注意:FilterDispatcher (org.apache.struts2.dispatcher.FilterDispatcher) 用于早期的 Struts2 开发,自 Struts 2.1.3 起已弃用。

如果您使用的 Struts 版本 >= 2.1.3,则始终建议升级新的过滤器类 - StrutsPrepareAndExecuteFilter (org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)。

一些有用的链接

  1. apache struts
  2. apache tutorials
  3. How Struts2 works
  4. How Struts2 works
  5. Difference between FilterDispatcher & StrutsPrepareAndExecuteFilter

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多