如何将th:对象值从html传递给控制器 [英] How to pass th:object values from html to controller
问题描述
HTML:
< form id =searchPersonFormaction =#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模型,@ PathVariable(pid)Long pid){
//修改数据
}
我试图像 @ModelAttribute(person)Person person
那样通过,但这并不是从前一页中检索表单值。
任何人都可以帮忙解决这个问题。
谢谢。
最好使用 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>名称< / label> < input type =text
class =form-controlid =nameth:field =* {name}> < /输入>
< / div>
< div class =form-group>
< label for =description>描述< / label> < input type =text
class =form-controlid =description
th:field =* {description}> < /输入>
< / div>
< / div>
< / form>
我使用Spring控制器返回此表单,该控制器初始化模型属性( myEntity
表单中的对象)。这是控制器类的相关部分:
@ModelAttribute(value =myEntity)
public实体newEntity ()
{
return new Entity();
@ModelAttribute
注释可以确保Spring对每个请求都初始化模型对象。
在向控制器发送第一个请求时设置一个名为command的模型:
@RequestMapping(value =/,method = RequestMethod.GET)
public ModelAndView getRanks(Model model,HttpServletRequest request)
{
String view =the-view-name;
返回新的ModelAndView(view,command,model);
}
并且,要在提交表单后访问模型,请执行相对方法:
@RequestMapping(value =/ the-action-url,method = RequestMethod.POST)
公共视图动作(模型模型,@ModelAttribute(myEntity)实体myEntity)
{
//保存实体或做任何你需要的事
返回新的RedirectView( /用户/行列);
}
这里,用 @ModelAttribute
会自动绑定到提交的对象。
How to pass thymeleaf(th:object) values to controller.
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 person
but this is not retrieving form values from previous page.
Can anyone help on this.
Thanks.
Preferably use th:action
as a form attribute instead of action
, and specify the binding like the following:
<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 (myEntity
object in the form). This is the relevant part of the controller class:
@ModelAttribute(value = "myEntity")
public Entity newEntity()
{
return new Entity();
}
The @ModelAttribute
annotation ensures that the model object is initialized by Spring for every request.
Set a model named "command" during the first get request to your controller:
@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 @ModelAttribute
is automatically bound to the submitted object.
这篇关于如何将th:对象值从html传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!