Search code examples
javaspringspring-bootspring-mvcthymeleaf

Call Java Class in Thymeleaf


How do you reference a Java class using Thymeleaf? I'm trying to display the result of my Java class on a website. When I use Spring Boot to display the output of the Java class it works, and when I use Spring Boot to display a Thymeleaf HTML template it also works, but I'm stuck now on how to call the java class from within the Thymeleaf HTML code.

Spring Boot Application File

package net.javavatutorial.tutorials;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootExampleApplication.class, args);
    }
}

Controller File

package net.javavatutorial.tutorials;

import org.springframework.stereotype.Controller;    
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
class MainController
{
    @RequestMapping(method = RequestMethod.GET)
    ModelAndView
    index()
    {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("version", "0.1");
        return mav;
    }
}

Filewalk.java that takes in a directory path and outputs the files contained in that directory

package net.javavatutorial.tutorials;

import java.io.File; 

public class Filewalk {
    
    public static String filewalk(String s) {
        String result = "";
        // creates a file object
        File file = new File(s);
        
        // returns an array of all files
        String[] fileList = file.list();
        
        for (String str : fileList) {
            result += str + "\n";
        }
        
        return result;
    }
}

Index.HTML file where I want to display the Filewalk.java output String

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "ISO-8859-1" />
      <link href = "css/styles.css" rel = "stylesheet"/>
      <title>Spring Boot Application</title>
   </head>
   <body>
      <h4>Thymeleaf Spring Boot web application</h4>
   </body>
</html>

Solution

  • The way you do this should be something like this:

    Controller:

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("index");
        mav.addObject("version", "0.1");
        mav.addObject("filewalk_results", Filewalk.filewalk("directory"));
        return mav;
    }
    

    HTML:

    <!DOCTYPE html>
    <html>
      <head>
          <meta charset = "ISO-8859-1" />
          <link href = "css/styles.css" rel = "stylesheet"/>
          <title>Spring Boot Application</title>
      </head>
    
      <body>
          <h4>Filewalk results:</h4>
          <pre th:text="${filewalk_results}" />
      </body>
    </html>
    

    You do all the actual calculation and create objects/information to put on your model, then output it in the HTML.