我被要求学习java和spring mvc的工作,但似乎我并不擅长它:( 无论如何,我试图动态创建两个列表,第一个工作,第二个没有(我没有真正理解我担心的get和post方法)你能告诉我为什么吗?
@Controller
@RequestMapping(value =" /") 公共类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 "configDrivers";
}
@RequestMapping(value = "configDrivers", method = RequestMethod.POST)
public String createCars(@ModelAttribute Championship championship, Config config, Model model) {
List<Car> cars = new ArrayList<Car>();
for (int i = 0; i < championship.getDrivers().size(); i++) {
cars.add(new Car());
}
championship.setCars(cars);
model.addAttribute("championship", championship);
return "configCars";
}
@RequestMapping(value = "configCars", method = RequestMethod.GET)
public String saveTrackNum(@ModelAttribute Config config, Model model) {
model.addAttribute("configuration", config);
return "trackNum";
}
@RequestMapping(value = "trackNum", method = RequestMethod.POST)
public String saveTracks(@ModelAttribute(value = "configuration") Config config, Championship championship,
Model model) {
List<Track> tracks = new ArrayList<Track>();
for (int i = 0; i < config.getTracksNum(); i++) {
tracks.add(new Track());
}
championship.setTracks(tracks);
model.addAttribute("championship", championship);
return "configTracks";
}
}
主页视图
<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>
configDrivers视图
<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>
configCars视图
<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>
Car class
public class Car {
private String team;
private Driver driver;
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public Driver getDriver() {
return driver;
}
public void setDriver(Driver driver) {
this.driver = driver;
}
}
冠军级别(带名单的那个)
public class Championship {
private List<Driver> drivers;
private List<Car> cars;
private List<Track> tracks;
public List<Driver> getDrivers() {
return drivers;
}
public void setDrivers(List<Driver> drivers) {
this.drivers = drivers;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public List<Track> getTracks() {
return tracks;
}
public void setTracks(List<Track> tracks) {
this.tracks = tracks;
}
}
答案 0 :(得分:0)
可以粘贴此代码:
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);
在printDrivers()方法中并检查。
我认为应该这样做。