在 Thymeleaf 的同一页面上显示输入参数

发布于 2025-01-17 10:49:36 字数 1291 浏览 1 评论 0 原文

我试图在 Thymleaf 和 Spring 中的表单所在的同一页面上显示结果,这是我到目前为止所尝试过的:

控制器:

   @Controller
public class ThymeleafController {
    
    @GetMapping("/hello")
    public String getHello() {
        return "hello";
    }
    
    
    @PostMapping("/test")
    public String test(@RequestParam("text1")String str, Model model) {
        
        model.addAttribute("sample", str);
        
        return "test";
    }
    
}

test.html 页面:

    <!DOCTYPE html>
<html xmlns:th ="http://www.thymeleaf.org" >
<head>
<meta charset ="UTF-8" ></meta>
<title> Hello World</title>
</head>
<body>
<h1> Hello World</h1>
<form method ="post" action ="/test" >
Enter: <input type ="text" name ="text1" />
<input type ="submit" value ="click" />
</form>
<span th:text ="${sample == null} ? 'null' : ${sample}" ></span>
</body>
</html>

当我进入测试页面时,我看到此错误:

  There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

有人能给我吗一个想法或一个示例,如何在表单/输入的同一页面上显示值?谢谢

I am trying to show the results on the same page as the form in Thymleaf and Spring and this is what I have tried till now:

Controller:

   @Controller
public class ThymeleafController {
    
    @GetMapping("/hello")
    public String getHello() {
        return "hello";
    }
    
    
    @PostMapping("/test")
    public String test(@RequestParam("text1")String str, Model model) {
        
        model.addAttribute("sample", str);
        
        return "test";
    }
    
}

test.html page:

    <!DOCTYPE html>
<html xmlns:th ="http://www.thymeleaf.org" >
<head>
<meta charset ="UTF-8" ></meta>
<title> Hello World</title>
</head>
<body>
<h1> Hello World</h1>
<form method ="post" action ="/test" >
Enter: <input type ="text" name ="text1" />
<input type ="submit" value ="click" />
</form>
<span th:text ="${sample == null} ? 'null' : ${sample}" ></span>
</body>
</html>

When i go on test page i see this error:

  There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

Can someone give me an ideea or an example how I could display a value on the same page as the form / input ? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2025-01-24 10:49:36

参考:

添加get,因此可以解决此问题。

@Controller
public class ThymeleafController {
    
    @GetMapping("/hello")
    public String getHello() {
        return "hello";
    }
    
    @GetMapping("/test")
    public String testfrm() {
        return "test";
    }

    @PostMapping("/test")
    public String test(@RequestParam("text1")String str, Model model) {
        
        return "test";
    }
    
}

Ref : https://hellokoding.com/handling-form-submission-example-with-java-spring-boot-and-freemarker/

Add for get , so this issue will get solved.

@Controller
public class ThymeleafController {
    
    @GetMapping("/hello")
    public String getHello() {
        return "hello";
    }
    
    @GetMapping("/test")
    public String testfrm() {
        return "test";
    }

    @PostMapping("/test")
    public String test(@RequestParam("text1")String str, Model model) {
        
        return "test";
    }
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文