简体   繁体   中英

ModelAttribute object fields are null in the controller

When i submit my form, the modelAttribute remain null. I looked for my mistake but i am stuck somewhere in the code and asking for your assistance... please check the code below

Spring version 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: Get:

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

Post:

@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";
}

error trace:

 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)

Since i am not getting any data from the form, thats why the object cant be saved in DB. How to bind the form data to controller? i have tried all the solutions as of now and I am not able to figure out where i am wrong, what i have missed out. Kindly help

Thanks in advance

Remove the @ModelAttribute from controller part. Ensure that the path attribute on jsp matching with the properties of your model bean (ie Book).

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

you have java.lang.NullPointerException on this line: com.adminportal.service.impl.BookServiceImpl.save(BookServiceImpl.java:18) maybe you send empty form? It is better to use validation with BindingResult on attribute and check validation form http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/validation/BindingResult.html or you have com.adminportal.service.impl.BookServiceImpl = null

Make sure you do have setters defined in the Book object. Because when the JSP is submitted, the details are mapped into the Java Book object through the setters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM