SpringMvc的配置文件的配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.zjj" />
<mvc:annotation-driven conversion-service="conversionService">
mvc:annotation-driven>
<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.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.zjj.converter.GoodConverter"/>
list>
property>
<property name="formatters">
<list>
<bean class="com.zjj.converter.DateFormatter">
<property name="dateFormat" value="yyyy-MM-dd"/>
bean>
list>
property>
bean>
beans>
需要在原有的基础上加上
<mvc:annotation-driven conversion-service="conversionService">
mvc:annotation-driven>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.zjj.converter.GoodConverter"/>
list>
property>
bean>
beans>
UserController:
package com.zjj.controller;
import com.zjj.pojo.Goods;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GoodController {
@RequestMapping("/addGoods")
public String showGoods(@RequestParam("goods") Goods goods, Model model){
System.out.println(goods);
model.addAttribute("goods", goods);
return "goodList";
}
}
GoodConverter:
package com.zjj.converter;
import com.zjj.pojo.Goods;
import org.springframework.core.convert.converter.Converter;
public class GoodConverter implements Converter<String, Goods> {
@Override
public Goods convert(String s) {
if(s!=null &&s.length()>0){
/*
private Double price;
private Integer nums;*/
String[] arr = s.split(",");
Goods goods=new Goods(arr[0],Double.valueOf(arr[1]),Integer.valueOf(arr[2]));
return goods;
}
return null;
}
}
Goods类:
package com.zjj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
private String goodName;
private Double price;
private Integer nums;
}
Date时间格式的转换器的配置:Springmvc.xml转换器中加上
<property name="formatters">
<list>
<bean class="com.zjj.converter.DateFormatter">
<property name="dateFormat" value="yyyy-MM-dd"/>
bean>
list>
property>
创建Student类:一定要有Date数据类型
package com.zjj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer stuNo;
private String stuName;
private Date birthday;
}
创建showStu.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Titletitle>
head>
<body>
学号: ${stu.stuNo}
学生姓名: ${stu.stuName}
生日:<fmt:formatDate value="${stu.birthday}" pattern="yyyy年MM月dd日"/>
body>
html>
创建addStu.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<form action="addStu" method="post">
<table>
<tr>
<td>学号:td>
<td><input type="text" name="stuNo">td>
tr>
<tr>
<td>姓名:td>
<td><input type="text" name="stuName">td>
tr>
<tr>
<td>生日td>
<td><input type="date" name="birthday">td>
tr>
<tr>
<td>td>
<td><input type="submit" name="添加学生">td>
tr>
table>
form>
body>
html>
创建StudentController类:
package com.zjj.controller;
import com.zjj.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class StudentController {
@RequestMapping("/addStu")
public String addStudent(Student student, Model model){
System.out.println(student);
model.addAttribute("stu",student);
return "showStu";
}
}
创建DateFormatter类进行相关的转换:
package com.zjj.converter;
import org.springframework.format.Formatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormatter implements Formatter<Date> {
//日期格式化对象
private SimpleDateFormat dateFormat;
public SimpleDateFormat getDateFormat() {
return dateFormat;
}
public void setDateFormat(SimpleDateFormat dateFormat) {
this.dateFormat = dateFormat;
}
//解析
@Override
public Date parse(String s, Locale locale) throws ParseException {
return dateFormat.parse(s);
}
//显示
@Override
public String print(Date date, Locale locale) {
return dateFormat.format(date);
}
}