目录
MyDateConverter.java
package org.javaboy.springmvc02.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 邓雪松 (づ ̄ 3 ̄)づ)
* @create 2021-11-03-14-59
* 日期类型转换器-第一步 第二部是在spring-servlet配置文件中去配置
*/
@Component
public class MyDateConverter implements Converter<String,Date>{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public Date convert(String source) {
try {
return sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
addbook.jsp
<%--
Created by IntelliJ IDEA.
User: 管 理 员
Date: 2021年11月03日 0003
Time: 13:12:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>addbook</title>
</head>
<body>
<h1>添加图书</h1>
<form action="/book/addbook2" method="post">
<table>
<tr>
<td>图书名称</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>图书作者</td>
<td><input type="text" name="author.name"></td>
</tr>
<tr>
<td>作者年龄</td>
<td><input type="text" name="author.age"></td>
</tr>
<tr>
<td>出版时间</td>
<td><input type="date" name="publishDate"></td>
</tr>
<tr>
<td>图书价格</td>
<td><input type="text" name="price"></td>
</tr>
<tr>
<td><input type="submit" value="添加"></td>
</tr>
</table>
</form>
</body>
</html>
Book.java
package org.javaboy.springmvc02.model;
import java.util.Date;
/**
* @author 邓雪松 (づ ̄ 3 ̄)づ)
* @create 2021-11-03-14-15
*/
public class Book {
private String name;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", author=" + author +
", publishDate=" + publishDate +
'}';
}
private Double price;
private Author author;
private Date publishDate;//出版时间
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public void setAuthor(Author author) {
this.author = author;
}
public Author getAuthor() {
return author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
BookController.java
package org.javaboy.springmvc02.controller;
import org.javaboy.springmvc02.model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.naming.Name;
/**
* @author 邓雪松 (づ ̄ 3 ̄)づ)
* @create 2021-11-02-19-13
*/
@Controller
@RequestMapping("/book")
public class BookController {
//http://localhost:8080/book/getbook
@RequestMapping("/getbook")
public ModelAndView getBook(){
ModelAndView mv = new ModelAndView("hello");
mv.addObject("name","BookController");
return mv;
}
@GetMapping("/book")
public String book(){
return "addbook";
}
@PostMapping(value = "/addbook",produces = "text/html;charset=utf-8")
@ResponseBody//为了防止找视图
public String addBook(@RequestParam("bookname") String name, String author, Double price){
System.out.println("name = "+name);
return name+">>>"+author+">>>"+price;
}
@PostMapping(value = "/addbook2",produces = "text/html;charset=utf-8")
@ResponseBody//
public String addBook2(Book book){
return book.toString();
}
}
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--springmvc上来也是包扫描 false一个也不扫但是又要把controller加进来-->
<context:component-scan base-package="org.javaboy.springmvc02" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置日期类型转换器-->
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService">
<property name="converters">
<set>
<ref bean="myDateConverter"/>
</set>
</property>
</bean>
<!--让在MyController中的注解@RequestMapping(/hello)访问到方法hello-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"-->
<!--id="requestMappingHandlerMapping"/>-->
<!--找到hello方法后还需要有人执行它-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"-->
<!--id="requestMappingHandlerAdapter"/>-->
<!--上面的2句可以用下面这一句代替-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--有视图modelandView就要有视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
运行图
结束