【问题标题】:How to pass th:object values from html to controller如何将:object 值从 html 传递到控制器
【发布时间】:2015-11-09 18:54:50
【问题描述】:

如何将 thymeleaf(th:object) 值传递给控制器​​。

HTML:

<form id="searchPersonForm" action="#" th:object="${person}" method="post" >  
</form>

SearchPersonController:

@RequestMapping(value = "/modify/{pid}", method = RequestMethod.GET)
    public String modifyPersonData(Principal principal, @ModelAttribute("person") Person person, UserRoles userRoles, Model model, @PathVariable("pid") Long pid ) {
         //modify data
    }

我正在尝试像 @ModelAttribute("person") Person person 一样传递,但这不是从前一页检索表单值。

谁能帮忙解决这个问题。

谢谢。

【问题讨论】:

    标签: java html spring-boot thymeleaf modelattribute


    【解决方案1】:

    最好使用th:action 作为表单属性而不是action,并指定如下绑定:

    <form th:action="@{/the-action-url}" method="post"
        th:object="${myEntity}">
    
        <div class="modal-body">
            <div class="form-group">
                <label for="name">Name</label> <input type="text"
                    class="form-control" id="name" th:field="*{name}"> </input>
            </div>
    
            <div class="form-group">
                <label for="description">Description</label> <input type="text"
                    class="form-control" id="description"
                    th:field="*{description}"> </input>
            </div>
        </div>
    </form>
    

    我用一个初始化模型属性的 Spring 控制器支持这个表单(表单中的myEntity 对象)。这是控制器类的相关部分:

    @ModelAttribute(value = "myEntity")
    public Entity newEntity()
    {
        return new Entity();
    }
    

    @ModelAttribute 注解确保模型对象由 Spring 为每个请求初始化。

    在对控制器的第一次获取请求期间设置一个名为“command”的模型:

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView getRanks(Model model, HttpServletRequest request)
    {
        String view = "the-view-name";
        return new ModelAndView(view, "command", model);
    }
    

    并且,要在表单提交后访问模型,实现相关方法:

    @RequestMapping(value = "/the-action-url", method = RequestMethod.POST)
    public View action(Model model, @ModelAttribute("myEntity") Entity myEntity)
    {
        // save the entity or do whatever you need
    
        return new RedirectView("/user/ranks");
    }
    

    这里,@ModelAttribute注解的参数自动绑定到提交的对象上。

    【讨论】:

    • 谢谢您的解释。我可以问几个疑问。 1. 我在当前页面(搜索页面)中有 5 个提交按钮,例如搜索、复制、查看 /modify ,.. 所以我们没有为 th:action="@{/the-action-url}" 指定默认 URL。 2.之前我使用`RequestMethod.POST`作为modifyPersonData,我只使用Person person而不使用@ModelAttribute("person")从视图中获得Person表单值。但是,当我更改为 `RequestMethod.GET, I am not able to retrieve Person` 值并尝试使用 @ModelAttribute("person") Person person 时,这也不起作用。
    • 1.您可以使用 5 种表单(每个按钮一个)具有不同的操作 url 或一个附加参数(例如隐藏字段)和一些 javascript 来区分被按下的按钮。 2.如果将控制器方法映射到GET请求(@RequestMapping(value = "/modify/{pid}", method = RequestMethod.GET)),那么它与设置为使用post的表单不匹配。
    • 谢谢你 EvilTodd。我想将控制器映射到 GET 请求,因此必须将表单更改为 get。但这会传递 url 中的所有输入字段。所以我认为modelattribute可以在表单设置为post并且控制器为get时处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多