无法在Spring MVC中提交表单

时间:2019-03-03 08:24:22

标签: java html spring-mvc

我是MVC和HTML等的新手,在从表单提交数据时遇到问题,该项目在控制器中显示为null:

我的JSP是:

<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>

    <head>
        <link rel="stylesheet" type="text/css" href="<c:url value='/resources/css/styles.css' />" />
        <title>Car Makes and Models</title>
    </head>
    <body>
        <jsp:include page="../header.jsp" />
        <c:url var="saveMake" value="/car/saveMake" />
        <c:url var="saveModel" value="/car/saveModel" />

        <div class="container">
            <div class="left40">
                <h1>Available Makes</h1>
                <br/>

                <table>
                    <c:forEach items="${listCarMakes}" var="make" >
                        <tr>
                            <td class="tableData">${make.description}</td>
                        </tr>
                    </c:forEach>
                </table>
            </div>
            <div class="right60">
                <h1>Add Make</h1>
                <br/>
                <form action="${saveMake}" method="post">
                    <label for="makeInput" >Description</label>
                    <input id="makeInput" type="text" value="${carMake.description}" /> 
                    <input type="submit" value="Add Make" />            
                </form>
            </div>

        </div>
    </body>
</html>

而控制器是:

package com.store.controller;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.store.service.CarService;
import com.store.vo.CarMake;
import com.store.vo.CarModel;



@Controller
@RequestMapping("/car")
public class CarController implements Serializable {

    private static final long serialVersionUID = 21L;
    private transient Logger log = Logger.getLogger( "CarController" );


    private CarService      carService;


    /*
     * Catalog
     */

    @RequestMapping("/catalog")
    public ModelAndView catalog( Model model ) {
        log.info("CarController: [catalog]");

        checkService();

        ModelAndView modelAndView = new ModelAndView( "car/catalog", "command", model);

        return modelAndView;
    }

    /*
     * Makes and Models
     */

    @RequestMapping("/makesAndModels")
    public ModelAndView makeAndModels( Model model ) {
        log.info("CarController: [makesAndModels]");

        checkService();

        List<CarMake> makes = carService.getAllMakes();
        model.addAttribute( "carMake", new CarMake());
        model.addAttribute( "listCarMakes" , makes );

        ModelAndView modelAndView = new ModelAndView( "car/makesAndModels", "command", model);

        return modelAndView;
    }
    /*
     * Save Make
     */
    @PostMapping("/saveMake")
    public String   saveMake( @ModelAttribute("carMake") CarMake make ) {

        log.info("CarController: [saveMake] [" + make.getDescription() + "]");

        carService.saveMake(make);

        return "redirect:/makesAndModels";
    }

    /*
     * Save Model
     */
    @GetMapping("/saveModel")
    public String   saveModel( @ModelAttribute("carModel") CarModel carModel ) {

        log.info("CarController: [saveModel] [" + carModel.getDescription() + "]");

        carService.saveModel(carModel);

        return "redirect:/makesAndModels";
    }

    /*
     * Spring
     */


    public void setCarService(CarService carService) {
        this.carService = carService;
    }


    /*
     * PRIVATE
     */
    private void checkService() {
        if ( carService == null ) {
            log.error("CarController: carService has NOT been initialised");
            throw new RuntimeException("CarController: carService has NOT been initialised");
        }
    }

}

显示汽车制造商列表的第一个div工作正常,显示输入和提交按钮的第二个div工作正常,并且在Controller中选择Submit调用正确的方法,但是CarMake对象的decription属性为null。

tomcat日志为:

INFO [http-nio-8088-exec-75] (?:?) CarMartController [navigatetoCarMart]
 INFO [http-nio-8088-exec-80] (?:?) CarController: [makesAndModels]
 INFO [http-nio-8088-exec-80] (?:?) CarDAO: [getAllMakes]
Hibernate: select carmake0_.id as id1_3_, carmake0_.description as descript2_3_ from make_catalog carmake0_
 INFO [http-nio-8088-exec-80] (?:?) CarDAOImpl: Make: [1][AUDI]
 INFO [http-nio-8088-exec-80] (?:?) CarDAOImpl: Make: [2][BMW]
 INFO [http-nio-8088-exec-78] (?:?) CarController: [saveMake] [null]
 INFO [http-nio-8088-exec-78] (?:?) CarDAO: [saveMake]
Hibernate: insert into make_catalog (description) values (?)
 WARN [http-nio-8088-exec-78] (SqlExceptionHelper.java:137) SQL Error: 1048, SQLState: 23000
ERROR [http-nio-8088-exec-78] (SqlExceptionHelper.java:142) (conn=29) Column 'description' cannot be null
 INFO [http-nio-8088-exec-82] (?:?) CarMartController [navigatetoCarMart]

0 个答案:

没有答案