java - Spring MVC 和 Thymeleaf Button 禁用更新

标签 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>

我的 Controller :

private Boolean disabled = false;

public Boolean getDisabled() {
    return disabled;
}

public void setDisabled(Boolean disabled) {
    this.disabled = disabled;
}

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

最佳答案

此实现应使用 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>

那么您的 Controller 中需要一个 Web 服务来处理这种情况并重定向到映射 View ,将属性设置为 true。

@RequestMapping(value = "/ws")
public ModelAndView disable(@RequestParam("second") String second, Model model) {
    Model model = new Model();        
    model.addAttribute("disableSecondButton", true);

    return "yourview";
} 

最后添加条件:

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

关于java - Spring MVC 和 Thymeleaf Button 禁用更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377341/

相关文章:

java - Spring 批处理 : Multithreaded step with AsyncItemProcessor doesn't run in parallel

java - 未定义名为 'persistence-unit' 的 bean

java - 使用 Spring MVC 和 Thymeleaf 上传文件

java - 如何防止 Thymeleaf 对我的模板进行 url 编码?

java - Jar 文件没有(图像加载相关问题)

java - Spring Boot @RestController,在 @RequestBody 中反序列化 Collection 时容忍 MismatchedInputException

java - 在内存中创建文本文件以供下载?

spring - @RequestParam,名称与值属性

java - 在 Thymeleaf 表中显示多行

java - 哪个线程获取哪个对象的锁?