Java 如何将 th:object 值从 html 传递到控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32045271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:58:37  来源:igfitidea点击:

How to pass th:object values from html to controller

javahtmlspring-bootthymeleafmodelattribute

提问by Java_User

How to pass thymeleaf(th:object) values to controller.

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

HTML:

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
    }

I am trying to pass like @ModelAttribute("person") Person personbut this is not retrieving form values from previous page.

我试图通过,@ModelAttribute("person") Person person但这不是从上一页检索表单值。

Can anyone help on this.

任何人都可以帮助解决这个问题。

Thanks.

谢谢。

回答by Evil Toad

Preferably use th:actionas a form attribute instead of action, and specify the binding like the following:

最好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>

I back this form with a Spring controller that initializes the model attribute (myEntityobject in the form). This is the relevant part of the controller class:

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

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

The @ModelAttributeannotation ensures that the model object is initialized by Spring for every request.

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

Set a model named "command" during the first get request to your controller:

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

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

And, to access the model as it results after the form submission, implement the relative method:

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

@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");
}

Here, the parameter annotated with @ModelAttributeis automatically bound to the submitted object.

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