I am making a thymeleaf Spring Boot web application that takes in a filepath as input to a search bar and outputs the directories/files at that filepath. Currently the result are rendered on a separate thymeleaf page, but I am wondering how to change it so that the result is rendered on the same page, just below the search bar?
Main Controller
package net.javavatutorial.tutorials;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/")
class MainController
{
@GetMapping("/")
public String showForm(Model model) {
Filename filename = new Filename();
model.addAttribute("filename", filename);
return "register_form";
}
@PostMapping("/register")
public String submitForm(@ModelAttribute("filename") Filename filename) {
System.out.println(filename);
return "register_success";
}
}
Thymeleaf Input/Landing Page with Search Bar
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>User Registration</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<div class="container">
<div align="center">
<h1>File Finder</h1>
<form action="#" th:action="@{/register}" method="post" th:object="${filename}">
<input type="text" th:field="*{filename}" placeholder="Enter Filepath..."/>
<button type="submit">Search</button>
</form>
</div>
</div>
</body>
</html>
Thymeleaf Output Page
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>File Finder</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<div class="container">
<div align="center">
<h1>Files Found:</h1>
<span th:text="${filename.filename}"></span><br/>
</div>
</div>
</body>
</html>
Filename.java
public class Filename {
private String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = Filewalk.filewalk(filename);
}
}
Filewalk.java program takes in file path and outputs files/directories contained at that file path
import java.io.File;
public class Filewalk {
public static void main(String[] args) {
System.out.println(filewalk("H:\\jdk-15"));
}
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;
}
}
That's what you can change to make it happen:
Controller:
@PostMapping("/register")
public String submitForm(@ModelAttribute("filename") Filename filename) {
ModelAndView view = new ModelAndView("register_form");
view.addObject("filename", filename);
return view;
}
Landing Page:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>User Registration</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<div class="container">
<div align="center">
<h1>File Finder</h1>
<form action="#" th:action="@{/register}" method="post" th:object="${filename}">
<input type="text" th:field="*{filename}" placeholder="Enter Filepath..."/>
<button type="submit">Search</button>
</form>
</div>
</div>
<div class="container" th:if="${filename.filename != null}">
<div align="center">
<h1>Files Found:</h1>
<span th:text="${filename.filename}"></span><br/>
</div>
</div>
</body>
</html>
This one should work for you :) Take care!