2016-09-20 103 views
0

我想使用选择框重定向到包含选择值的URL。Thymeleaf + Spring如何从窗体选择选项值传递给控制器​​@PathVariable

这里娄的HTML页面thymeleaf变量:

<form th:action="@{/app/}"> 
    <select id="app" name="app"> 
     <option th:each="app : ${apps}" 
      th:value="${app.id}" 
      th:text ="${app.name}"> 
     </option> 
    </select> 
    <button>Go</button> 
</form> 

控制器写入如下:

@RequestMapping("/app/{appId}") 
public ModelAndView getApp(@PathVariable String appId){...} 

我的目标是通过URL来访问控制:mydomain/app/{app.id}/ 我试着使用th:field到选择没有成功。我在这里错过了一些东西。

请问,你能解释我如何将选择值添加到控制器预期的URL?

谢谢你的提示

编辑以下@NicolaiEhemann答案

我使用jQuery和Ajax搬到了JavaScript的:

$('button').click(function() { 
    $.ajax({ 
     url: '/app/' + $('#app').val(), 
     dataType: 'html' 
    }).done(function(data) { 
     $('#result').html(data) 
    }); 
}); 

谢谢。

回答

1

Thymeleaf只会生成静态HTML代码。为了实现你想要的,你必须编写客户端JavaScript来处理“输入”事件事件,以便在选择不同的值时更改表单动作,或者处理表单“提交”事件以覆盖表单提交的默认动作。相关的js代码可能取决于您使用的框架。

相关问题