Ciao a tutti sto cercando di creare un programma che crei due liste dinamicamente, sfortunatamente la seconda non sta funzionando (probabilmente perch� non ho molto capito i metodi get e post)
Potreste spiegarmi perch�?
Ecco il mio controller
codice:
@Controller
@RequestMapping(value = "/")
public class ConfigController {
@RequestMapping(value = "home", method = RequestMethod.GET)
public String saveNum(Model model) {
model.addAttribute("configuration", new Config());
return "home";
}
@RequestMapping(value = "home", method = RequestMethod.POST)
public String saveNumPost(@ModelAttribute(value = "configuration") Config config, Model model) {
List<Driver> drivers = new ArrayList<Driver>();
for (int i = 0; i < config.getDriversNum(); i++) {
drivers.add(new Driver());
}
Championship championship = new Championship();
championship.setDrivers(drivers);
model.addAttribute("championship", championship);
return "configDrivers";
}
@RequestMapping(value = "configDrivers", method = RequestMethod.GET)
public String saveDriverList() {
return "redirect:/configDrivers";
}
@RequestMapping(value = "configDrivers", method = RequestMethod.POST)
public String printDrivers(@ModelAttribute Config config, Championship championship, Model model) {
for(int i = 0; i < config.getDriversNum(); i++){
System.out.println(championship.getDrivers().get(i));
}
return "configCars";
}
@RequestMapping(value = "configCars", method = RequestMethod.GET)
public String saveCars(/* Model model */) {
return "configCars";
}
@RequestMapping(value = "configCars", method = RequestMethod.POST)
public String saveCarsPost(@ModelAttribute(value = "configuration") Config config, Model model) {
List<Car> cars = new ArrayList<Car>();
for (int i= 0; i < config.getDriversNum(); i++) {
cars.add(new Car());
}
Championship championship = new Championship();
championship.setCars(cars);
model.addAttribute("championship",championship);
return "configTracks";
}
}
la view home
codice:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Welcome to Formula 1 simulator
</h1>
<c:url var="url" value="/home"/>
<form:form action="${url}" method="POST" modelAttribute="configuration">
<label>Insert number of drivers:</label> <br>
<form:input path="driversNum" placeholder="Type drivers number"/>
<br>
<input type="submit" />
<!--number races-->
</form:form>
</body>
</html>
la view configDrivers
codice:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false" %>
<html>
<head>
<title>Configuration</title>
</head>
<body>
<h1>
World championship Configuration
</h1>
<c:url var="url" value="/configDrivers"/>
<form:form action="${url}" method="POST" modelAttribute="championship">
<label>Insert drivers:</label>
<c:forEach items="${championship.drivers}" var="driver" varStatus="i">
<h3>Insert driver's name and surname of driver ${i.index} </h3>
<form:input path="drivers[${i.index}].name" value="${driver.name}" placeholder="Type name here" />
<br>
<form:input path="drivers[${i.index}].surname" value="${driver.surname}" placeholder="Type surname here" />
<br>
</c:forEach>
<input type="submit" />
<!--number races-->
</form:form>
</body>
</html>
e la view configCars
codice:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page session="false" %>
<html>
<head>
<title>Cars Configuration</title>
</head>
<body>
<h1>
Cars configuration
</h1>
<c:url var="url" value="/configCars"/>
<form:form action="${url}" method="POST" modelAttribute="championship">
<label>Insert cars:</label>
<c:forEach items="${championship.cars}" var="car" varStatus="i">
<h3>Insert driver's team ${i.index} </h3>
<form:input path="cars[${i.index}].team" value="${car.team}" placeholder="Type team here" />
<br>
</c:forEach>
<input type="submit" />
<!--number races-->
</form:form>
<select>
<c:forEach items="${championship.drivers}" var="car" varStatus="i">
<option value="${i.index}">${driver.name}</option>
</c:forEach>
</select>
</body>
</html>