Spring MVC解决出现乱码

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请求都是有效的

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值