mercredi 16 décembre 2020

Rendering output on same page in Spring Boot Thymeleaf

I've created a Spring Boot web application that takes in a filepath into the search bar and outputs the files at that filepath. The output is rendered on the same page, just below the search bar. I would like the input to the search bar (the filepath) to be kept as is on the output page, but instead it is replaced with a string of the files contained at that filepath. How can I fix this?

MainController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
class MainController
{
    
    @GetMapping("/")
    public String showForm(Model model) {
        Filename filename = new Filename();
        model.addAttribute("filename", filename);
        return "register_form";
    }

    @PostMapping("/register")
    public ModelAndView submitForm(@ModelAttribute("filename") Filename filename, @ModelAttribute("original") Filename original) { 
        ModelAndView view = new ModelAndView("register_form");
        view.addObject("filename", filename);
        view.addObject("original", original); // here
        return view;
    }
    
}

Register_Form.html

<!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">
    <h2></h2>
    <span th:text="${filename.filename}"></span><br/>
</div>
</div>
</body>
</html>

Filewalk.java

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

Filename.java

public class Filename {

    private String filename; 
    private String original;
    
    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = Filewalk.filewalk(filename);
    }

    public String getOriginal() {
        return original;
    }

    public void setOriginal(String original) {
        this.original = original;
    }
    
}



Aucun commentaire:

Enregistrer un commentaire