实验六 Spring MVC注解与数据绑定
- 实验目的
1、了解Spring MVC核心类的作用;
2、掌握Spring MVC常用注解的使用;
3、了解Spring MVC 数据绑定的概念;
4、掌握Spring MVC的常用数据绑定类型的使用
- 实验内容
1、有如下一个订单信息页面order.jsp(置于/WEB-INF/jsp目录下),按以下步骤实现一个使用POJO类型完成表单数据传输的SpringMVC数据绑定项目。
(1) 创建一个Order类来封装上述订单信息,其中各个属性的名称和数据类型是:产品编号(ProductID,Integer),用户编号(UserID,Integer),交易日期(TransactionDate,Date),价格(Price,Double),数量(Quantity,Long),交易金额(TransactionAmount,Double),用户邮件(Email,String),备注(Comment,String)。
(2) 编写控制器类OrderController,在类中实现向order.jsp跳转的方法和接收order.jsp页面提交的订单信息的方法(该方法执行完后会将订单信息显示在result.jsp页上,见步骤(6))。
(3)创建日期转换类DateConverter,实现String类型到Date类型的转换。
(4)创建order.jsp页面。
(5)配置springmvc-config.xml文件。
(6)创建一个result.jsp结果页面(置于/WEB-INF/jsp目录下),用于显示接收到订单信息。
- 实验结果
- 实验代码
package com.zym;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConverter implements Converter<String, Date> {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
public DateConverter(String s) {
}
public DateConverter() {
}
public Date convert(String source) {
try {
return DATE_FORMAT.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
package com.zym;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class Order {
private Integer productID;
private Integer userID;
private String transactionDate;
private Double price;
private Long quantity;
private Double transactionAmount;
private String email;
private String comment;
public Order() {
this.productID = productID;
this.userID = userID;
this.transactionDate = transactionDate;
this.price = price;
this.quantity = quantity;
this.transactionAmount = transactionAmount;
this.email = email;
this.comment = comment;
}
public Integer getProductID() {
return productID;
}
public Integer getUserID() {
return userID;
}
public String getTransactionDate() {
return transactionDate;
}
public Double getPrice() {
return price;
}
public Long getQuantity() {
return quantity;
}
public Double getTransactionAmount() {
return transactionAmount;
}
public String getEmail() {
return email;
}
public String getComment() {
return comment;
}
public void setProductID(Integer productID) {
this.productID = productID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public void setTransactionDate(String transactionDate) {
this.transactionDate = transactionDate;
}
public void setPrice(Double price) {
this.price = price;
}
public void setQuantity(Long quantity) {
this.quantity = quantity;
}
public void setTransactionAmount(Double transactionAmount) {
this.transactionAmount = transactionAmount;
}
public void setEmail(String email) {
this.email = email;
}
public void setComment(String comment) {
this.comment = comment;
}
@Override
public String toString() {
return "Order{" +
"productID=" + productID +
", userID=" + userID +
", transactionDate=" + transactionDate +
", price=" + price +
", quantity=" + quantity +
", transactionAmount=" + transactionAmount +
", email='" + email + '\'' +
", comment='" + comment + '\'' +
'}';
}
}
package com.zym;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class OrderController {
@RequestMapping(value = "/order")
public String showOrderForm(ModelMap model) {
// 创建Order对象,并将其添加到模型中
model.addAttribute("order", new Order());
// 跳转到订单表单页面
return "order";
}
@RequestMapping(value = "/result", method = RequestMethod.POST)
public String submitOrder(@ModelAttribute("order") Order order, ModelMap model) {
// 将订单信息添加到模型中
model.addAttribute("order", order);
System.out.println(order);
// 跳转到订单结果页面
return "result";
}
}
package com.zym;
import com.zym.DateConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描控制器所在的包 -->
<context:component-scan base-package="com.zym" />
<!-- 视图解析器配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 数据绑定配置 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.zym.DateConverter" >
<constructor-arg value=""/>
</bean>
</set>
</property>
</bean>
<!-- 启用注解驱动 -->
<mvc:annotation-driven />
</beans>
<%--
Created by IntelliJ IDEA.
User: 111
Date: 2023/5/6
Time: 21:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>订单信息</title>
</head>
<body>
<h1>订单信息</h1>
<form action="result" method="post">
<label for="productID">产品编号:</label>
<input type="text" id="productID" name="productID"><br>
<label for="user_id">用户编号:</label>
<input type="text" id="user_id" name="userID"><br>
<label for="transaction_date">交易日期:</label>
<input type="text" id="transaction_date" name="transactionDate" pattern="\d{4}-\d{2}-\d{2}"><br>
<label for="price">价格:</label>
<input type="number" id="price" name="price" min="0" step="0.01"><br>
<label for="quantity">数量:</label>
<input type="number" id="quantity" name="quantity" min="1"><br>
<label for="transaction_amount">交易金额:</label>
<input type="number" id="transaction_amount" name="transactionAmount" min="0" step="0.01"><br>
<label for="user_email">用户邮件:</label>
<input type="email" id="user_email" name="email"><br>
<label for="remark">备注:</label>
<textarea id="remark" name="comment"></textarea><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: 111
Date: 2023/5/7
Time: 18:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>订单信息</title>
</head>
<body>
<h1>订单信息</h1>
<p>产品编号:${order.productID}</p>
<p>用户编号:${order.userID}</p>
<p>交易日期:${order.transactionDate}</p>
<p>价格:${order.price}</p>
<p>数量:${order.quantity}</p>
<p>交易金额:${order.transactionAmount}</p>
<p>用户邮件:${order.email}</p>
<p>备注:${order.comment}</p>
</body>
</html>
- 实验心得
在本次实验中,我通过实践掌握了Spring MVC的数据绑定和常用注解的使用。通过实现一个使用POJO类型完成表单数据传输的SpringMVC数据绑定项目,我深入理解了Spring MVC的数据绑定机制和流程。创建了一个Order类,用于封装订单信息。在该类中,我定义了各个属性的名称和数据类型,例如交易日期使用了Date类型,这样可以方便地将表单提交的字符串类型转换为Date类型。编写了一个控制器类OrderController,该类中实现了向order.jsp跳转的方法和接收order.jsp页面提交的订单信息的方法。通过@ModelAttribute注解和@RequestParam注解,我可以将前端页面提交的数据绑定到Order类的实例中,并且通过ModelAndView对象将订单信息传递到result.jsp页面进行显示。
为了实现字符串类型到Date类型的转换,我创建了一个日期转换类DateConverter,通过实现Converter接口,实现了String类型到Date类型的转换。并且通过在springmvc-config.xml文件中进行配置,将该类注册到Spring MVC的转换器中。创建了一个result.jsp页面,用于显示接收到的订单信息。通过在页面中使用EL表达式,可以方便地将订单信息进行展示。
通过本次实验,我深入理解了Spring MVC的数据绑定机制和常用注解的使用,特别是对于日期类型的转换,需要注意格式化的方法和注解的使用。同时,我也认识到了Spring MVC的优势,它通过注解和数据绑定,可以方便地将前端页面的数据传递到后端进行处理,并且可以通过配置简化开发流程。