Я новичок в MVC, HTML и т. Д., И у меня возникают проблемы при отправке данных из формы, элемент отображается в контроллере как ноль:
Мой 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, который отображает кнопку ввода и отправки, отображает отлично, и выбор Submit вызывает правильный метод в Controllerно атрибут расшифровки объекта CarMake имеет значение 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]