如何将百里香叶输入字段中的变量作为 PathVariable 传递 [英] How to pass variable from thymeleaf input field as PathVariable

查看:70
本文介绍了如何将百里香叶输入字段中的变量作为 PathVariable 传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有控制器:

@Slf4j
@RestController
@RequestMapping("/languages")
{
    @PostMapping("/{arg}")
    public void testLanguage(@PathVariable String arg) {
        log.info("testLanguage(): " + arg);
    }
}

html 页面:

<form method="post" th:action="@{/languages/{arg}(arg=${arg})}">
    <label>Enter any message here:
        <!--How I should write my input field(what parameters must be added)
        to receive this variable in cntroller as @PathVariable-->
        <input type="text" th:value="${arg}" th:name="arg">
    </label>
    <input type="submit" value="Test with ARG">
</form>

如何从输入字段传递arg变量并在控制器中接收它?

How to pass arg variable from input field and receive it in controller?

推荐答案

没有 Thymeleaf 方法可以做到这一点.您必须使用 JavaScript 来构建该 url onsubmit,或者创建另一个控制器方法来为您进行转发.像这样的东西,例如:

There isn't a Thymeleaf way to do this. You'll have to either use JavaScript to build that url onsubmit, or alternatively create another controller method to do the forwarding for you. Something like this for example:

@PostMapping("/")
public void forwardLanguage(@RequestParam String arg) {
    return "redirect:/language/" + arg;
}

这篇关于如何将百里香叶输入字段中的变量作为 PathVariable 传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆