Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC and Thymeleaf Button disabled update

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.

like image 904
Anton Styopin Avatar asked Nov 02 '16 10:11

Anton Styopin


People also ask

How do I disable Thymeleaf button?

The value of disabled is set through model attribute isEnabled. You can set this model attribute on controller and you will be good to go for active or disabled drop downs. For HTML input controls the same can be achieved by setting ThymeLeaf th:readonly attribute. An example of same is given below.

Is Thymeleaf supported by Spring MVC?

Thymeleaf offers a set of Spring integrations that allow you to use it as a fully-featured substitute for JSP in Spring MVC applications.

How do I enable Thymeleaf in Spring Boot?

Spring Boot will provide auto-configuration for Thymeleaf. Add spring-boot-starter-thymeleaf dependency in pom. xml to enable this auto-configuration. No other configurations required, Spring Boot will inject all required configuration to work with Thymeleaf.

Is Thymeleaf fully integrated with Spring?

It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.


1 Answers

This implementation should be done with Javascript and not Thymeleaf.

It has not sense do it with Thymeleaf and reload the page for such thing.

But if you are too much interested in it you could do it adding an action url to the form with something liket this:

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

@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>
like image 138
Pau Avatar answered Oct 26 '22 06:10

Pau