在同一页面上处理表单提交spring boot

jqjz2hbq  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(361)

我正在制作一个thymeleafspringbootweb应用程序,它接收一个文件路径作为搜索栏的输入,并输出该文件路径上的目录/文件。目前,结果呈现在一个单独的thymeleaf页面上,但我想知道如何更改它,使结果呈现在同一个页面上,就在搜索栏下面?
主控制器

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输入/登录页

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

自动对焦输出页

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

文件名.java

public class Filename {

    private String filename; 

    public String getFilename() {
        return filename;
    }

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

}

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

chhqkbe11#

这就是你可以改变的事情:
控制器:

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

登录页:

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

这个应该适合你:)保重!

相关问题