在上一篇中,提交的表单都是简单的 数据类型。如果提交的表单中有其他对象怎么办?比如日期。
写道
package com.spring.mvn.domain;
import java.util.Date;
public class Reservation {
private String courtName;
private Date date;
private int hour;
private Player player;
private SportType sportType;
public String getCourtName() {
return courtName;
}
public void setCourtName(String courtName) {
this.courtName = courtName;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public SportType getSportType() {
return sportType;
}
public void setSportType(SportType sportType) {
this.sportType = sportType;
}
public Reservation(String courtName, Date date, int hour, Player player,
SportType sportType) {
super();
this.courtName = courtName;
this.date = date;
this.hour = hour;
this.player = player;
this.sportType = sportType;
}
public Reservation() {
super();
}
@Override
public String toString() {
return "Reservation [courtName=" + courtName + ", date=" + date
+ ", hour=" + hour + ", player=" + player + ", sportType="
+ sportType + "]";
}
}
import java.util.Date;
public class Reservation {
private String courtName;
private Date date;
private int hour;
private Player player;
private SportType sportType;
public String getCourtName() {
return courtName;
}
public void setCourtName(String courtName) {
this.courtName = courtName;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public SportType getSportType() {
return sportType;
}
public void setSportType(SportType sportType) {
this.sportType = sportType;
}
public Reservation(String courtName, Date date, int hour, Player player,
SportType sportType) {
super();
this.courtName = courtName;
this.date = date;
this.hour = hour;
this.player = player;
this.sportType = sportType;
}
public Reservation() {
super();
}
@Override
public String toString() {
return "Reservation [courtName=" + courtName + ", date=" + date
+ ", hour=" + hour + ", player=" + player + ", sportType="
+ sportType + "]";
}
}
写道
package com.spring.mvn.domain;
public class Player {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Player(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "Player [name=" + name + ", phone=" + phone + "]";
}
}
public class Player {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Player(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
@Override
public String toString() {
return "Player [name=" + name + ", phone=" + phone + "]";
}
}
写道
package com.spring.mvn.domain;
public class SportType {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SportType(int id, String name) {
super();
this.id = id;
this.name = name;
}
public SportType() {
}
@Override
public String toString() {
return "SportType [id=" + id + ", name=" + name + "]";
}
}
public class SportType {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SportType(int id, String name) {
super();
this.id = id;
this.name = name;
}
public SportType() {
}
@Override
public String toString() {
return "SportType [id=" + id + ", name=" + name + "]";
}
}
如果提交的表单是 reservation这个对象时应该如何将页面上的输入数据绑定到匹配的属性中呢?
写道
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Reservation Form</title>
<style type="text/css">
.error {
color: #ff0000;
font-weight: bold;
}
</style>
</head>
<body>
<form:form method="post" modelAttribute="reservation">
<table>
<tr>
<td>Court Name</td>
<td><form:input path="courtName" /></td>
<td><form:errors path="courtName" cssClass="error" /></td>
</tr>
<tr>
<td>Date</td>
<td><form:input path="date" /></td>
<td><form:errors path="date" cssClass="error" /></td>
</tr>
<tr>
<td>Hour</td>
<td><form:input path="hour" /></td>
<td><form:errors path="hour" /></td>
</tr>
<tr>
<td>Player Name</td>
<td><form:input path="player.name" /></td>
<td><form:errors path="player.name" cssClass="error" /></td>
</tr>
<tr>
<td>Player Phone</td>
<td><form:input path="player.phone" /></td>
</tr>
<tr>
<td>Sport Type</td>
<td><form:select path="sportType" items="${sportTypes}"
itemValue="id" itemLabel="name" /></td>
<td><form:errors path="sportType" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Reservation Form</title>
<style type="text/css">
.error {
color: #ff0000;
font-weight: bold;
}
</style>
</head>
<body>
<form:form method="post" modelAttribute="reservation">
<table>
<tr>
<td>Court Name</td>
<td><form:input path="courtName" /></td>
<td><form:errors path="courtName" cssClass="error" /></td>
</tr>
<tr>
<td>Date</td>
<td><form:input path="date" /></td>
<td><form:errors path="date" cssClass="error" /></td>
</tr>
<tr>
<td>Hour</td>
<td><form:input path="hour" /></td>
<td><form:errors path="hour" /></td>
</tr>
<tr>
<td>Player Name</td>
<td><form:input path="player.name" /></td>
<td><form:errors path="player.name" cssClass="error" /></td>
</tr>
<tr>
<td>Player Phone</td>
<td><form:input path="player.phone" /></td>
</tr>
<tr>
<td>Sport Type</td>
<td><form:select path="sportType" items="${sportTypes}"
itemValue="id" itemLabel="name" /></td>
<td><form:errors path="sportType" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
此时你会发下在页面上如如 2012-12-12 时,reservation.date=null.这应该如何解决?
这就需要使用到@InitBinder
写道
package com.spring.mvn.controller;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import com.spring.mvn.domain.Player;
import com.spring.mvn.domain.Reservation;
import com.spring.mvn.domain.SportType;
import com.spring.mvn.service.ReservationService;
import com.spring.mvn.validator.ReservationValidator;
@Controller
@RequestMapping("reservationForm")
@SessionAttributes("reservation")
public class ReservationFormController {
@Autowired
private ReservationService reservationService;
@Autowired
private ReservationValidator reservationValidator;
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, true));
binder.registerCustomEditor(SportType.class,
new PropertyEditorSupport() {
@Override
public void setAsText(String text)
throws IllegalArgumentException {
int sportTypeId = Integer.parseInt(text);
SportType sportType = reservationService
.getSportType(sportTypeId);
setValue(sportType);
}
});
}
@ModelAttribute("sportTypes")
public List<SportType> populateSportTypes() {
System.out.println("sportTypes");
return reservationService.getAllSportType();
}
@RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model,
@RequestParam("username") String username) {
Reservation reservation = new Reservation();
reservation.setPlayer(new Player(username, null));
model.addAttribute("reservation", reservation);
return "reservationForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submintForm(
@ModelAttribute("reservation") Reservation reservation,
BindingResult result, SessionStatus status, ModelMap model) {
reservationValidator.validate(reservation, result);
if (result.hasErrors()) {
model.addAttribute("reservation", reservation);
return "reservationForm";
}
reservationService.make(reservation);
return "reservationSuccess";
}
}
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import com.spring.mvn.domain.Player;
import com.spring.mvn.domain.Reservation;
import com.spring.mvn.domain.SportType;
import com.spring.mvn.service.ReservationService;
import com.spring.mvn.validator.ReservationValidator;
@Controller
@RequestMapping("reservationForm")
@SessionAttributes("reservation")
public class ReservationFormController {
@Autowired
private ReservationService reservationService;
@Autowired
private ReservationValidator reservationValidator;
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, true));
binder.registerCustomEditor(SportType.class,
new PropertyEditorSupport() {
@Override
public void setAsText(String text)
throws IllegalArgumentException {
int sportTypeId = Integer.parseInt(text);
SportType sportType = reservationService
.getSportType(sportTypeId);
setValue(sportType);
}
});
}
@ModelAttribute("sportTypes")
public List<SportType> populateSportTypes() {
System.out.println("sportTypes");
return reservationService.getAllSportType();
}
@RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model,
@RequestParam("username") String username) {
Reservation reservation = new Reservation();
reservation.setPlayer(new Player(username, null));
model.addAttribute("reservation", reservation);
return "reservationForm";
}
@RequestMapping(method = RequestMethod.POST)
public String submintForm(
@ModelAttribute("reservation") Reservation reservation,
BindingResult result, SessionStatus status, ModelMap model) {
reservationValidator.validate(reservation, result);
if (result.hasErrors()) {
model.addAttribute("reservation", reservation);
return "reservationForm";
}
reservationService.make(reservation);
return "reservationSuccess";
}
}