【问题标题】:Tiles, Struts 2, chained actions, and passing through put-attribute valuesTiles、Struts 2、链式动作和传递 put-attribute 值
【发布时间】:2011-06-21 19:41:44
【问题描述】:

我正在编写一个搜索引擎来探索多种搜索算法。 web 部分浮动在 Struts 2.1.6 和 Tiles 2.2.2 上。目前完全是一团糟,所以下面我提取了一个简化版本,以专注于我遇到的确切问题。

网站的正常流程是这样的:

  1. 搜索表单飞溅。选择搜索算法并输入搜索参数。
  2. 提交表格。算法选择设置表单提交到的操作。
  3. 结果页面。在顶部包含搜索表单的副本(在启动画面和结果中使用相同的搜索表单 jsp 代码)。

问题:我想添加一个搜索表单变体。变体 2 将提交与变体 1 相同的一些搜索算法,具有相同的结果页面,但我当然希望变体 2 搜索表单显示在顶部以保持一致性。新流程是:

  1. 选择一个搜索表单。对于香草,转到 2。对于疯狂,转到 3。
  2. 从 {A, B, C} 中选择搜索算法并输入搜索参数。转到 4。
  3. 从 {B, C, D, E} 中选择搜索算法并输入搜索参数。转到 4。
  4. 提交。链(使用 Struts“链”结果类型**)到所选算法的适当搜索操作。
  5. 显示结果。包括一份{Vanilla | Crazy} 搜索表单。

** 切换到“链”是为了更自动化地传递算法参数。旧方法使用隐藏字段,并且使用重定向来维护非常痛苦,正如您将在下面的 struts.xml 中看到的那样。

所以对于单一变体,我有这样的东西:

struts.xml:
    <action name="SearchPage" class="nies.actions.search.SearchForm">
        <result name="input">/Search.jsp</result>
    </action>

<!-- List of available search algorithms to fill 'searcher' param above: --> 
    <action name="AlgorithmA" class="nies.actions.search.AlgorithmA">
        <result name="success" type="tiles">Results</result>
        <result name="input"type="redirectAction">
                <param name="actionName">SearchPage</param>
                <param name="namespace">/</param>
                <param name="searchAlgorithm">AlgorithmA</param>
        </result>
    </action>
    <action name="AlgorithmB" class="nies.actions.search.AlgorithmB">
        <result name="success" type="tiles">Results</result>
        <result name="input" type="redirectAction">
                <param name="actionName">SearchPage</param>
                <param name="namespace">/</param>
                <param name="searchAlgorithm">AlgorithmB</param>
        </result>
    </action>
    <action name="AlgorithmC" class="nies.actions.search.AlgorithmC">
        <result name="success" type="tiles">Results</result>
        <result name="input" type="redirectAction">
                <param name="actionName">SearchPage</param>
                <param name="namespace">/</param>
                <param name="searchAlgorithm">AlgorithmC</param>
        </result>
    </action>

所以我打算补充的是:

struts.xml:
    <action name="CrazySearchPage" class="nies.actions.search.CrazySearchForm">
        <result name="input">/CrazySearch.jsp</result>
        <result name="success" type="chain">${searcher}</result>
    </action>

(可能将两个搜索页面的搜索输入显示切换为图块以保存 copypasta)(并为搜索算法消除所有输入重定向参数废话,因为链接会自动将这些值保留在堆栈中)

...但现在我被淹没了,因为 SearchPage 和 CrazySearchPage 共享的链接操作都显示在结果图块中:

tiles.xml:
<tiles-definitions>
  <definition name="Results" template="/resultsTemplate.jsp">
<put-attribute name="tabPane" value="/resultsEagerTabPane.jsp"/>
  </definition>
  ...

结果图块包含常规 SearchPage 中使用的常规搜索表单代码:

resultsTemplate.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>
  <title><s:property value="query.queryString"/></title>
</head>
<body>
...
<div id="searchform">
<tiles:insertTemplate template="/searchform.jsp" flush="true"/>
</div>
...

我想通过切换来相信

<tiles:insertTemplate template="/searchform.jsp" flush="true"/>

<tiles:insertAttribute name="searchform" flush="true"/>

然后我可以 (1) 为 CrazyResults 制作一个新的磁贴,该磁贴在适当的搜索表单 jsp 中传递,或者 (B) 找到某种方法来传递基于 *SearchPage 运行的磁贴属性。

(1) 的问题在于,链式算法操作已经转到常规结果图块,我不想禁止常规 SearchPage 中的这些算法,以便我可以在 CrazySearchPage 上使用它们。我想我可以定义一组重复的算法操作供 CrazySearchPage 使用,它们以不同的名称和不同的结果调用同一个类,但这会使 struts.xml 和我的其他配置文件变得一团糟(我有广泛的显示与每个操作名称关联的设置)。

(B) 的问题是我还没有找到足够的瓷砖文档来告诉我这是否可能以及如何去做。 Struts 和 Tiles 似乎并不直接相互交谈,它们只是传递注释。

【问题讨论】:

    标签: attributes struts2 polymorphism chaining tiles2


    【解决方案1】:

    我认为有几个独立的问题,所以这不是一个完整的解决方案,它只会解决我认为的绊脚石。

    造成混乱的主要原因是使用了redirectAction,而您使用chain 的新计划也好不到哪里去。 struts2 世界中的这些类似于 goto 语句。那就是它易于使用,非常适合快速修复,但如果不加限制地使用会造成很大的混乱(并且像 goto 一样可以完全避免它们)。

    以下是一些建议:

    避免链接和redirectAction。在它们似乎有意义的地方使用拦截器。

    添加约定插件,这样您就可以避免大多数对 struts.xml 的维护。

    如果完成上述操作,那么我相信您的应用程序不会一团糟,但视图问题似乎是一个单独的问题。我确实使用瓷砖,但无法理解什么不起作用。您所有的图块结果都指向 Results。我使用图块的方式很像您使用 JSP。您应该对不同的页面(平铺结果)有不同的定义。如果您没有任何使用 extends 属性的图块定义,我认为您会错过很多图块的功能。 Tiles 与 JSP 非常相似,只是使用 Tile 可以分解出页面的所有共性。

    【讨论】:

    • (augh enter) On Tiles:搜索算法都会返回一个文档列表,无论它们如何生成该列表,我总是希望以相同的方式显示文档列表。顺便说一句,我 am 使用 extends 来显示结果的变化,但这些变化与多搜索算法问题是正交的。
    【解决方案2】:

    我的问题是如何将动作的响应插入到tiles 属性中。我是这样做的:

    <tiles:insertTemplate template="/workflow/login.action"/>
    

    在 web.xml 中:

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
        <dispatcher>FORWARD</dispatcher> 
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>        
    </filter-mapping>
    

    我不知道这是否正是您想要做的,但这与您为操作设置的结果无关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多