Spring MVC和Thymeleaf Button禁用更新

Ant*_*pin 3 java spring thymeleaf

我的表单中有两个按钮(第一个和第二个)。现在,我想在单击第一个按钮时更新第二个按钮的禁用功能。

<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>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

private Boolean disabled = false;

public Boolean getDisabled() {
    return disabled;
}

public void setDisabled(Boolean disabled) {
    this.disabled = disabled;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将布尔变量与我的第二个按钮绑定,以便该按钮在第一个按钮上启用/禁用onclick?我知道我必须用PostMapping(“ updateButton”)编写一个方法。但是我不知道如何绑定变量。

Pau*_*Pau 5

此实现应使用Javascript而不是Thymeleaf

用Thymeleaf这样做并重新加载该页面是没有意义的。

但是,如果您对此不太感兴趣,则可以通过以下操作向表单添加操作网址:

<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>
Run Code Online (Sandbox Code Playgroud)

然后,您将需要控制器中的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";
} 
Run Code Online (Sandbox Code Playgroud)

最后添加条件:

<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>
Run Code Online (Sandbox Code Playgroud)


0ga*_*gam 0

尝试这样。

控制器

@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;
}    
Run Code Online (Sandbox Code Playgroud)

html

<div th:if="${model.disabled}">
     // put your second button
     <input type="button" value="Second" /> 
<div>
Run Code Online (Sandbox Code Playgroud)

模型

public class Model {

    private Boolean disabled;

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }
}
Run Code Online (Sandbox Code Playgroud)