【问题标题】:How to override parent form mapping in a child form?如何覆盖子表单中的父表单映射?
【发布时间】:2016-04-28 15:07:51
【问题描述】:

抱歉标题,不知道如何措辞。我有一个表格,可以在表格中显示一些数据。

<form action="#" th:action="@{/do/stuff}" th:object="${foo}" method="post">
    <table>
        ....
        <tr th:each="currentFoo : *{fooList}">

        <td>...<td>
        <td>
            <a th:href="'/anotherPage/' + ${currentFoo.getId()}">
               <button>Go to Different page</button>
            </a> 
        </td>
     ...
    </table>
    <button type="submit" value="submit">Submit</button>
</form>

然后是控制器:

@RequestMapping(value="/do/stuff", method = RequestMethod.POST)
public String processQuery(@ModelAttribute FooWrapper wrapper, Model model){....}

@RequestMapping(value="anotherPage/{fooID}", method=RequestMethod.GET)
public String getClient(@PathVariable int id, Model model){...}

问题是,当我单击“转到不同页面”按钮时,表单被映射/do/stuff 发布和拾取。

我怎样才能有一个按钮,通过get 传递一个ID,将我带到一个完全不同的页面(将其视为具有编辑条目的选项以及发布所有条目的标准选项)?我只需要到达页面anotherPage/{fooID}

我尝试添加一个按钮表单,希望它会覆盖父表单并对我想要的映射执行get 请求,但它不起作用,这一切都被外部拾取形式。

<form th:action="'/anotherPage/' + ${currentFoo.getID()}" style="display: inline" th:method="get">
    <input type="submit" th:value="Go to Page" role="button" />
</form>

这甚至可能吗?如果必须,我什至可以将子方法更改为post,但父表单无法更改。 有什么建议么?

更新 - 我已经找到并发布了一个解决方案,但直到明天我才能接受!

【问题讨论】:

  • 是否可以将“转到不同页面”按钮拉到表单外?
  • @ndrone 我不这么认为,因为需要为表格中的每一行动态生成按钮,而且我很确定我不能通过结束表格来“拆分”表格&lt;/table&gt; 之前的标签,因为 java spring 不会让你在一个表单中分解 &lt;table&gt;...&lt;/table&gt; 标签(它的全部或全部) - 如果这有意义的话

标签: java spring post spring-boot thymeleaf


【解决方案1】:

原来有一个非常简单的解决方案。我认为单击 &lt;button&gt;...&lt;/button&gt; 元素时,因为它们位于导致回发的表单内,而不管指定的 URL 是什么。

但是,使用一个简单的超链接并将其装扮成一个按钮解决了这个问题:

<a th:href="'/anotherPage/' + ${currentFoo.getID()}" class="btn btn-default">Go to page</a>

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 2021-09-09
    • 2011-02-05
    相关资源
    最近更新 更多