Thymeleaf 和 Spring MVC 的表单参数为空

编程入门 行业动态 更新时间:2024-10-24 14:26:30
本文介绍了Thymeleaf 和 Spring MVC 的表单参数为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用 Thymeleaf 和 Spring MVC 时遇到了问题.

i'm having a problem with Thymeleaf and Spring MVC.

我正在学习 spring.io 网站 spring 的教程.io/guides/gs/handling-form-submission/ 当我试图扩展本教程时,我遇到了一个问题.

I'm following a tutorial from spring.io website spring.io/guides/gs/handling-form-submission/ and when I've tried to expand this tutorial, I ran into a problem.

如果我将另一个参数添加到我的模型类中(在我的示例中,我添加了一个日期参数和一个长参数),并且没有将它们放在我的视图中(想象一下这个日期参数是我的最后修改日期和这个长参数是一个随机值),当我提交这个值时,它使我的方法中的这两个参数为空.

If I add another parameter to my model class (on my example, I've added a Date parameter and a long parameter), and not put them on my view (just imagine that this date parameter is my last modification date and this long parameter is a random value), when I submit this value, it makes this 2 parameters null on my method.

这是我的一些代码.

我的模型类

public class Greeting { private long id; private String content; private Date hour; private long longNumber; .... getters and setters .... }

我的控制器

@Controller public class GreetingController { @RequestMapping(value="/greeting", method=RequestMethod.GET) public String greetingForm(Model model) { Greeting greeting = new Greeting(); greeting.setHour(new Date()); greeting.setLongNumber(1234L); System.out.println(greeting.toString()); model.addAttribute("greeting", greeting); return "greeting"; } @RequestMapping(value="/greeting", method=RequestMethod.POST) public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) { System.out.println(greeting.toString()); model.addAttribute("greeting", greeting); return "result"; } }

我的表格

<!DOCTYPE HTML> <html xmlns:th="www.thymeleaf"> <head> <title>Getting Started: Handing Form Submission</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Form</h1> <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post"> <p>Id: <input type="text" th:field="*{id}" /></p> <p>Message: <input type="text" th:field="*{content}" /></p> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html>

结果页面

<!DOCTYPE HTML> <html xmlns:th="www.thymeleaf"> <head> <title>Getting Started: Handing Form Submission</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Result</h1> <p th:text="'id: ' + ${greeting.id}" /> <p th:text="'content: ' + ${greeting.content}" /> <p th:text="'Date: ' + ${greeting.hour}" /> <a href="/greeting">Submit another message</a> </body> </html>

这是我的模型类的控制台打印

This are my console prints of my model class

Greeting [id=0, content=null, hour=Sun Apr 26 22:29:25 GMT-03:00 2015, longNumber=1234] Greeting [id=0, content=aaaa, hour=null, longNumber=0]

如您所见,我的两个参数不在我的视野中,在发布后(第二行)变为空值,并且在第一行中,它们都有值.我不明白为什么,因为我来自另一个好的框架(JSF)并且我在那里没有这样的问题.只是想知道,我正在使用 spring 4,就像在教程中一样,为什么我有一个不在我看来的参数?因为其中一些参数会保存一些数据,就像上次注册更新日期、更新注册的最后用户和我的应用程序 ID 一样.

As you can see, both of my parameters that are not in my view, become null after the post (the second line) and on the first line, they have values. I don't understand why, because I came from another good framework (JSF) and I don't have such a problem there. Just to know, I'm using spring 4, just like on the tutorial, and why I have a parameter that is not on my view? Because some of this parameters will hold some data just like last register updated date, last user who updated the register and my application ID.

谁能帮我找到答案?

经过一段时间和实验,我找到了解决问题的方法.按照我的代码运行,就像它的意思一样:

After some time and experimentation, I found a solution to my problem. Follow my piece of code that works just as it meant to be:

@Controller @SessionAttributes({"obj"}) public class MyController { @RequestMapping(value = "/path", method = RequestMethod.GET) public ModelAndView myGet() { ModelAndView modelAndView = new ModelAndView("view"); MyClass object = new MyClass(); modelAndView.addObject("obj", object ); return modelAndView; } @RequestMapping(value = "/path", method = RequestMethod.POST) public ModelAndView myPost(@ModelAttribute("obj") @Validated MyClass object, BindingResult result){ ModelAndView modelAndView = new ModelAndView("view"); if(result.hasErrors()) modelAndView.addObject("obj",object); else service.save(object); return modelAndView; } }

推荐答案

这是预期的行为.在获得问候 view 时,您在 response 中有 model,但只要 http request/response 已完成,它不再存在.您将在 <form> 标记中保存这些值,并且您需要在 form submit 上提供这些值.

This is the expected behavior. You have the model in the response while getting to your greetings view, but as long as that http request/response is done, it doesn't exist anymore. You will hold the values in the <form> tag and you will need to provide these values upon form submit.

意思是,如果你想发回这些值,你需要添加 2 个隐藏的 input 来保存这两个值.

Meaning, that if you want to send these values back you will need to add 2 hidden inputs to hold these two values.

<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post"> <p>Id: <input type="text" th:field="*{id}" /></p> <p>Message: <input type="text" th:field="*{content}" /></p> <input type="hidden" th:field="*{hour}" /> <input type="hidden" th:field="*{longNumber}" /> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form>

更多推荐

Thymeleaf 和 Spring MVC 的表单参数为空

本文发布于:2023-11-24 21:16:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   为空   参数   Thymeleaf   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!