java Spring MVC 和 Thymeleaf Button 禁用更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40377341/
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
Spring MVC and Thymeleaf Button disabled update
提问by Anton Styopin
I have two buttons in my form(first and second). Now, I want to update the disable function of my second button when I click the first one.
我的表单中有两个按钮(第一个和第二个)。现在,我想在单击第一个按钮时更新第二个按钮的禁用功能。
<form action="updateButton" method="post" id="something">
<h1 th:text="${application.text}"></h1>
<button type="submit" name="first">First</button>
<button type="submit" name="second" th:disabled="${model.disabled}">Second</button>
</form>
My Controller:
我的控制器:
private Boolean disabled = false;
public Boolean getDisabled() {
return disabled;
}
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
}
Is there a way to bind the boolean variable with my second button, so that the button enables/disables onclick at the first button? I know I have to write a method with PostMapping("updateButton"). But I don't know how to bind the variable.
有没有办法将布尔变量与我的第二个按钮绑定,以便按钮在第一个按钮上启用/禁用 onclick?我知道我必须用 PostMapping("updateButton") 编写一个方法。但我不知道如何绑定变量。
回答by Pau
This implementation should be done with Javascript
and not Thymeleaf
.
这个实现应该用Javascript
而不是来完成Thymeleaf
。
It has not sense do it with Thymeleaf and reload the page for such thing.
用 Thymeleaf 做这件事并重新加载页面是没有意义的。
But if you are too much interested in it you could do it adding an action url to the form with something liket this:
但是如果你对它太感兴趣,你可以用类似这样的东西在表单中添加一个动作 url:
<form action="updateButton" method="get" id="something" action="/ws">
<h1 th:text="${application.text}"></h1>
<button type="submit" name="first">First</button>
<button type="submit" name="second" th:disabled="${model.disabled}">Second</button>
</form>
Then you would need a Web service in your controller which handles this situation and redirects to the mapping view
putting an attribute to true.
然后您需要在您的控制器中使用一个 Web 服务来处理这种情况并重定向到mapping view
将一个属性设置为 true。
@RequestMapping(value = "/ws")
public ModelAndView disable(@RequestParam("second") String second, Model model) {
Model model = new Model();
model.addAttribute("disableSecondButton", true);
return "yourview";
}
Finally add the conditional:
最后添加条件:
<div th:switch="${disableSecondButton}">
<button type="submit" name="second" th:case="'true'" disabled>Second</button>
<button type="submit" name="second" th:case="*">Second</button>
</div>
回答by Byeon0gam
Try like this.
像这样尝试。
controller
控制器
@RequestMapping(value = "/test")
public ModelAndView showMain(ModelAndView mav) {
Model model = new Model();
model.setDissetDisabled(true);
mav.addObject("model", model);
mav.setViewName("/test/yourViewName");
return mav;
}
html
html
<div th:if="${model.disabled}">
// put your second button
<input type="button" value="Second" />
<div>
model
模型
public class Model {
private Boolean disabled;
public Boolean getDisabled() {
return disabled;
}
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
}
}
回答by Vikram
There are many ways resolving this issue by considering the above adding to model will also work no need for variable creation also please have a look at the below options
Using the model Attribute
@GetMapping("/users/edit/{id}")
public String example1(@PathVariable("id") Long id, Model model) {
model.addAttribute("users", service.readUserById(id));
model.addAttribute("disabled", true);
return "editUsers";
}
Use ModelMap if your adding multiple times on different method calls
@GetMapping("/users/edit/{id}")
public String example2(@PathVariable("id") Long id, ModelMap modelMap)
{
modelMap.put("users", service.readUserById(id));
modelMap.put("disabled", true);
return "editUsers";
}
if your directly referring the model or entity object have a field declared disabled and make that variable transient so it will be not be picked by hibernate.
public User {
@Id
private Long id;
// others eliminated
//
@Transient
private boolean disabled = true;
// getter and setter are elimnated
}
in controller
@GetMapping("/users/edit/{id}")
public String example(@PathVariable("id") Long id, Model model) {
User user = service.readUserById(id);
user.setDisabled(false);
model.addAttribute("users", service.readUserById(id));
return "editUsers";
}
same follows for command object and its convertion.
In UI
在用户界面
for example one and two
例如一和二
for example3
例如3