Tiles、Struts 2、链式操作以及传递放置属性值

发布于 2024-11-16 15:47:04 字数 4111 浏览 2 评论 0原文

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

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

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

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

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

** 切换到“链”是为了允许更自动化地传递算法参数。旧方法使用隐藏字段,并且通过重定向维护起来非常痛苦,正如您将在下面的 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>

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

...但是现在我很困惑,因为 SearchPage 和 CrazySearchPage 共享的链接操作都显示在“结果”图块中:

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

并且“结果”图块包含在常规搜索页面:

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 似乎并不直接相互交谈,它们只是传递注释。

I'm writing a search engine to explore multiple search algorithms. The web portion floats on Struts 2.1.6 and Tiles 2.2.2. It's currently a complete mess, so below I've factored out a simplified version to focus on the exact problem I'm having.

The normal flow through the site is this:

  1. Searchform splash. Select a search algorithm and enter search parameters.
  2. Submit the form. The algorithm selection sets the action the form is submitted to.
  3. Results page. Includes a copy of the searchform at the top (the same searchform jsp code is used in both the splash and results).

The Problem: I want to add a search form variant. Variant 2 would submit to some of the same search algorithms as Variant 1, with the same results page, but of course I want the Variant 2 searchform to show up at the top for consistency. The new flow would be:

  1. Select a searchform. For Vanilla, goto 2. For Crazy, goto 3.
  2. Select a search algorithm from {A, B, C} and enter search parameters. Goto 4.
  3. Select a search algorithm from {B, C, D, E} and enter search parameters. Goto 4.
  4. Submit. Chain (using Struts "chain" resulttype**) to the appropriate search action for the selected algorithm.
  5. Display results. Include a copy of {Vanilla | Crazy} search form.

** The switch to "chain" is to allow more automated passing of algorithm parameters. The old way used hidden fields and was a huge pain to maintain with redirects, as you'll see in the struts.xml below.

So for the single variant I have something like this:

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>

So what I plan to add is this:

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

(probably switching the search input display for both search pages to tiles to save copypasta) (and nuking all the input redirect param crap for the search algorithms, since chaining automatically keeps those values on the stack)

...but now I'm hosed, because the chained actions shared by both SearchPage and CrazySearchPage all display in the Results tile:

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

And the Results tile includes the regular searchform code used in the regular 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>
...

I want to believe that by switching

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

to

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

I can then either (1) make a new tile for CrazyResults that passes in the appropriate searchform jsp, or (B) find some way to pass in the tiles attribute based on which *SearchPage runs.

The problem with (1) is that the chained algorithm actions already go to the regular Results tile, and I don't want to disallow those algorithms from the regular SearchPage just so I can use them on the CrazySearchPage. I suppose I could define a duplicate set of algorithm actions for use by the CrazySearchPage, that call the same class with a different name and a different result, but this makes a mess in struts.xml and my other configuration files (I have extensive display settings associated with each action name).

The problem with (B) is that I've yet to find enough tiles documentation to tell me whether this is possible or not and how to do it. Struts and Tiles don't appear to talk to each other directly, they just pass notes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空袭的梦i 2024-11-23 15:47:04

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

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

这里有一些建议:

避免链接和重定向操作。在它们看起来有意义的地方使用拦截器。

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

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

I think there are a couple independent issues, so this will not be a complete solution, it will just address what I think would be stumbling blocks.

The primary reason for the mess is the use of redirectAction and your new plan to use chain isn't much better. These in the struts2 world are akin to the goto statement. That is it's easy to use, great for a quick fix but manages to make a great mess if used without restraint (and like goto it is possible to avoid them altogether).

Here is some advice:

Avoid chaining and redirectAction. Where they seem to make sense use an interceptor.

Add the conventions plugin so you can avoid most maintenance of struts.xml.

If the above is done then your application I'm confident won't be a mess but the issues with the view seem to be a separate issue. I do use tiles but am unable to understand what isn't working. All your tiles results are pointing at Results. The way I use tiles is much like you would use a JSP. You should have different definitions for different pages (tiles results). I think you are missing a lot of the power of Tiles if you don't have any tiles definitions which use the extends attribute. Tiles is very much like a JSP, it's just that with tiles you're able to factor out all the commonality of the page.

灰色世界里的红玫瑰 2024-11-23 15:47:04

我的问题是如何将操作的响应插入到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>

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

My problem was how to insert the response from a action in a tiles attribute. I did that this way:

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

And at the 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>

I don't know if it is exactly this you want to do, but this is independent from the result you set for your action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文