gpt4 book ai didi

java - ModelAttribute 对象字段在 Controller 中为空

转载 作者:行者123 更新时间:2023-11-30 06:43:10 25 4
gpt4 key购买 nike

当我提交表单时,modelAttribute 保持为空。我查找了我的错误,但我被困在代码中的某个地方并请求您的帮助...请检查下面的代码

Spring版本4.2.0

<form:form class="form-horizontal" modelAttribute="book"
action="${pageContext.request.contextPath}/book/add" method="post">
<fieldset>
<legend class="center-block">
New Book Information<span style="font-size: small"> * is a
required field</span>
</legend>


<!-- title -->
<div class="form-group">
<label class="col-md-2 control-label" for="title">* Title</label>

<div class="col-md-8">
<form:input type="text" path="title" class="form-control" id="title"
required="required" placeholder="Title" />
<span class="help-block">Title of the book</span>
</div>
</div>

<!-- author -->
<div class="form-group">
<label class="col-md-2 control-label" for="author">*
Author</label>
<div class="col-md-8">
<form:input type="text" path="author" class="form-control" id="author"
required="required" placeholder="Author" />
<span class="help-block">Author of the book</span>
</div>
</div></form:form>

Controller :获取:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String addBook(Model model) {
Book book = new Book();
model.addAttribute("book", book);
return "addBook";
}

帖子:

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addBookPost(@ModelAttribute("book") Book book, HttpServletRequest request) {
System.out.println(book.getTitle());
bookService.save(book);

MultipartFile bookImage = book.getBookImage();
System.out.println(book);
try {
byte[] bytes = bookImage.getBytes();
String name = book.getId() + ".png";
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File("src/main/resources/static/image/book/" + name)));
stream.write(bytes);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}

return "redirect:bookList";
}

错误跟踪:

 java.lang.NullPointerException
com.adminportal.service.impl.BookServiceImpl.save(BookServiceImpl.java:18)
com.adminportal.controller.BookController.addBookPost(BookController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

由于我没有从表单中获取任何数据,这就是对象无法保存在数据库中的原因。如何将表单数据绑定(bind)到 Controller ?到目前为止,我已经尝试了所有解决方案,但我无法弄清楚我错在哪里,我错过了什么。请帮忙!

提前致谢

最佳答案

从 Controller 部分删除@ModelAttribute。确保 jsp 上的路径属性与模型 bean(即 Book)的属性匹配。

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addBookPost(Book book, BindingResult result, ModelMap model) {

关于java - ModelAttribute 对象字段在 Controller 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44069310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com