1、编写的jsp页面
<%--添加的页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/electronic/save" method="post">
型号:<input type="text" name="type">
出厂价格:<input type="text" name="price">
出厂日期:<input type="date" name="createTime">
<input type="submit" value="添加">
</form>
</body>
</html>
<%--显示页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>显示新增数据页面</title>
</head>
<body>
<table border="1">
<tr>
<td> 型号:</td>
<td> ${home.type}</td>
</tr>
<tr>
<td> 出厂价格:</td>
<td> ${home.price}</td>
</tr>
<tr>
<td> 出厂时间:</td>
<td> ${home.createTime}</td>
</tr>
</table>
</body>
</html>
2、搭建环境
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring MVC的核心是一个Servlet,称为前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义控制器,访问路径为hello-->
<!-- demo1-->
<!-- 启动注解方式配置-->
<mvc:annotation-driven/>
<!-- 扫描注解的包-->
<context:component-scan base-package="cn.cvs.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4、编写的controller
使用的是MVC注解引用
@Controller
@RequestMapping("/electronic")
public class HomeController {
@RequestMapping("/add")
public String add() throws Exception{
return "/add";
}
@RequestMapping("/save")
public String save(Model model, String type, Double price, String createTime) throws Exception {
System.out.println("type:"+type);
Home home = new Home();
home.setType(type);
home.setPrice(price);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(createTime);
home.setCreateTime(date);
model.addAttribute("home",home);
return "/save";
}
}
5、用字母是可以正常运行的
那么换成中文就变成乱码了
6、解决方案 给web.xml中添加一个字符编码过滤器
<!-- 设置过滤器防止字符乱码-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置之后重启服务器再次请求
那么中文乱码就解决了post请求跟get请求都是有效的