How to pass random number from Java class to html file

HenrikasB :

I am trying to learn more spring framework with thymeleaf, but right now I want to generate in one class a random number and then pass to html file to print it out. Am I even doing it right? So I have a class numberController where I generate my random number:

public class numberController {
    public int randomSkaicius() {
        Random rand = new Random();
        int skaicius = (int) (Math.random() * 50 + 1);
        return skaicius;
    }
}

And here's my HTML code:

<div class="container">
    <h5>Privaloma informacija</h5>
    <div>
        <form action="#" th:action="@{/home}" th:object="${galerija}" method="GET">
            <label>Nuotraukos ID: + ${numberController.randomSkaicius}</label>
        </form>
    </div>
</div>
g00glen00b :

First of all, you have to make sure that your Thymeleaf template is in the right location (src/main/resources/templates), and remember its name (eg. my-page.html).

The next step is that you have to properly tell Spring that NumberController is actually a controller, by using the @Controller annotation:

@Controller
public class NumberController {
    public int randomSkaicius() {
        Random rand = new Random();
        int skaicius = (int) (Math.random() * 50 + 1);
        return skaicius;
    }
}

After that, you have to tell Spring which URL should be resolved to a specific controller method, which returns a model + view. You can do that by using the @GetMapping annotation, for example:

@Controller
public class NumberController {
    @GetMapping("/my/page")
    public ModelAndView getMyPage() {
        // TODO: Implementation
    }

    public int randomSkaicius() {
        Random rand = new Random();
        int skaicius = (int) (Math.random() * 50 + 1);
        return skaicius;
    }
}

In this case, we're telling Spring that as soon as someone requests http://localhost:8080/my/page, the getMyPage() method is triggered.

Within that method, we have to return a ModelAndView object, which defines a few things:

  • The name of the view, in your case, the name of the Thymeleaf template, which means my-page (you have to remove the extension-part).
  • The name of the model, which you can freely choose, for example randomSkaicius.
  • The actual value for the model, which is what randomSkaicius() returns.

For example:

@Controller
public class NumberController {
    @GetMapping("/my/page")
    public ModelAndView getMyPage() {
        return new ModelAndView("my-page", "randomSkaicius", randomSkaicius());
    }

    public int randomSkaicius() {
        Random rand = new Random();
        int skaicius = (int) (Math.random() * 50 + 1);
        return skaicius;
    }
}

Now that you defined this, you have to go back to your Thymeleaf template, and wherever you want to use that model, you have to reference it properly. We named it "randomSkaicius" in the controller, so we can put something like this within our Thymeleaf template:

<span th:text="${randomSkaicius}"></span>

We're using th:text to tell Thymeleaf to resolve that part as the inner text of that element. We then used ${randomSkaicius} to refer to the model we defined earlier on in the controller.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152630&siteId=1