Spring控制器中的@ModelAttribute字段为null(@ModelAttribute fields are null in Spring controller)

编程入门 行业动态 更新时间:2024-10-26 06:34:52
Spring控制器中的@ModelAttribute字段为null(@ModelAttribute fields are null in Spring controller)

我遵循教程来处理Spring中的表单提交。 我想用表单填充我的TaskEntity bean,并在我的控制器的taskSubmit方法中使用这个bean的值。 但是,当我填写表单并点击提交时,此方法中task所有属性均为空。 相关代码如下:

TaskEntity.java

public class TaskEntity { private String title; private String description; private Date dueDate; private TaskPriority priority; public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setDescription(String description) { this.description = description; } public String getDescription() { return this.description; } public void setDueDate(Date dueDate) { this.dueDate = dueDate; } public Date getDueDate() { return this.dueDate; } public void setPriority(String priority) { this.priority = TaskPriority.valueOf(priority); } public TaskPriority getPriority() { return this.priority; } }

addTask.jsp

<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>To Do List - Add Task</title> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/stylesheet.css"> </head> <body> <form action="tasks" th:action="@{/tasks}" th:object="${taskEntity}" method="post"> <p>Title: <input type="text" th:field="*{title}" /></p> <p>Description: <input type="text" th:field="*{description}" /></p> <p>Due Date: <input type="text" th:field="*{dueDate}" /></p> <p>Priority: <input type="text" th:field="*{priority}" /></p> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html>

TaskController.java

@Controller public class TaskController { private static final String TASKS = "/tasks"; private static final String ADD_TASK = "/addTask"; @Autowired private TaskEntityDao taskEntityDao; ObjectMapper mapper = new ObjectMapper(); @RequestMapping(value = TASKS, method = RequestMethod.GET) public ModelAndView readTasks() { return new ModelAndView("tasks"); } @RequestMapping(value = ADD_TASK, method = RequestMethod.GET) public String addTask(Model model) { model.addAttribute("taskEntity", new TaskEntity()); return "addTask"; } @RequestMapping(value = TASKS, method = RequestMethod.POST) public void taskSubmit(@ModelAttribute TaskEntity task) { // Fields for 'task' are all null! taskEntityDao.createTask(task); readTasks(); } }

我期望通过model.addAttribute("taskEntity", new TaskEntity());将TaskEntity传递给视图model.addAttribute("taskEntity", new TaskEntity()); 将会把形式值映射到它的字段,但我一定错过了一些东西。

更新:

我从我的Spring配置中添加视图解析器代码:

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>

I followed a tutorial to handle form submission in Spring. I wanted to populate my TaskEntity bean with the form and have this bean's values available in my controller's taskSubmit method. However, all of the properties of task in this method are null when I fill out the form and hit submit. The relevant code is as follows:

TaskEntity.java

public class TaskEntity { private String title; private String description; private Date dueDate; private TaskPriority priority; public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setDescription(String description) { this.description = description; } public String getDescription() { return this.description; } public void setDueDate(Date dueDate) { this.dueDate = dueDate; } public Date getDueDate() { return this.dueDate; } public void setPriority(String priority) { this.priority = TaskPriority.valueOf(priority); } public TaskPriority getPriority() { return this.priority; } }

addTask.jsp

<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>To Do List - Add Task</title> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/stylesheet.css"> </head> <body> <form action="tasks" th:action="@{/tasks}" th:object="${taskEntity}" method="post"> <p>Title: <input type="text" th:field="*{title}" /></p> <p>Description: <input type="text" th:field="*{description}" /></p> <p>Due Date: <input type="text" th:field="*{dueDate}" /></p> <p>Priority: <input type="text" th:field="*{priority}" /></p> <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p> </form> </body> </html>

TaskController.java

@Controller public class TaskController { private static final String TASKS = "/tasks"; private static final String ADD_TASK = "/addTask"; @Autowired private TaskEntityDao taskEntityDao; ObjectMapper mapper = new ObjectMapper(); @RequestMapping(value = TASKS, method = RequestMethod.GET) public ModelAndView readTasks() { return new ModelAndView("tasks"); } @RequestMapping(value = ADD_TASK, method = RequestMethod.GET) public String addTask(Model model) { model.addAttribute("taskEntity", new TaskEntity()); return "addTask"; } @RequestMapping(value = TASKS, method = RequestMethod.POST) public void taskSubmit(@ModelAttribute TaskEntity task) { // Fields for 'task' are all null! taskEntityDao.createTask(task); readTasks(); } }

I expected the TaskEntity passed to the view with model.addAttribute("taskEntity", new TaskEntity()); was going to have the form values mapped to it's fields, but I must have missed something.

Update:

I am adding the view resolver code from my Spring config:

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>

最满意答案

通过编辑,您已确认您的视图解析适用于JSP。

您有一个包含<input>元素的JSP页面

<input type="text" th:field="*{description}" />

据推测, th:field是Thymeleaf元素 ,但是您的页面不是由Thymeleaf处理的,它是由JSP Servlet处理的。 JSP Servlet不关心th:field ,它在任何方面都不是特别的。 它只是从字面上复制它。 此外,您的input没有name属性。 因此,当您提交form ,您的浏览器不会发送相应的值。 (即使这样做,它也不会有Spring MVC认可的名称,并且可以绑定到您的模型属性。)

设置您的MVC配置,以正确呈现Thymeleaf视图而不是JSP视图。

或者,如果您真的想使用JSP,请使用相应的技术:EL,JSTL和Spring的标记库。 网上有大量的教程,就像这个 。

With your edit you've confirmed that your view resolution is for JSPs.

You have a JSP page with <input> elements that look like

<input type="text" th:field="*{description}" />

Presumably th:field is a Thymeleaf element, but your page isn't processed by Thymeleaf, it's processed by the JSP Servlet. The JSP Servlet doesn't care about th:field, it's not special to it in any way. It just copies it literally. Furthermore, your input doesn't have a name attribute. As such, when you submit the form, your browser doesn't send a corresponding value. (Even if it did, it wouldn't have a name that Spring MVC recognizes and can bind to your model attribute.)

Set up your MVC configuration to properly render Thymeleaf views instead of JSP views.

Or if you really want to use JSPs, use the corresponding technologies: EL, JSTL, and Spring's tag library. There are tons of tutorials online, like this one.

更多推荐

TaskEntity,public,value,电脑培训,计算机培训,IT培训"/> <meta name="descr

本文发布于:2023-08-05 17:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   器中   ModelAttribute   Spring   fields

发布评论

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

>www.elefans.com

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