其他分享
首页 > 其他分享> > IDEA中控制台post回传中文乱码的问题

IDEA中控制台post回传中文乱码的问题

作者:互联网

解决IDEA中post回传控制台中文乱码

解决控制台post回传中文乱码的问题

** Tomcat服务器启动后中文在控制台显示乱码 **
在这里插入图片描述

request对象回传参数时,控制台出现中文乱码现象
处理post中文乱码
在web.xml文件中添加以下代码

<!--中文编码过滤器-->
<filter>
  <filter-name>characterEncodingFilter</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>
  <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

** 注意添加的位置在servlet之前不然会报错 **
项目结构:
在这里插入图片描述
springmvc.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"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">

    <!--配置组件扫描器  直接写成所有组件的父包,会扫描所有父包下的子包-->
    <context:component-scan base-package="com.demo"/>

    <!--配置mvc注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>

    </bean>

</beans>		

jsp页面内容自己随便写
登录页面login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录界面</h1>

<form action="login1" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登录">
</form>
</body>
</html>

UserController

package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    //通过requerst对象来取值
    @RequestMapping("/login1")
    public String login1(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("用户名:"+username+"密码:"+password);
        return "list";
    }
}

设置编码格式为UTF-8
点击File->setting->File Encoding
在这里插入图片描述
将字符编码全部换成UTF-8
在这里插入图片描述
添加以下代码并保存
-Dfile.encoding=UTF-8
在tomcat配置中的VM options中也添加同样的代码
在这里插入图片描述
更改完成后重新将项目部署到服务器上,并重新启动Tomcat服务器,访问login.jsp页面,输入账号密码提交,回传控制台中文显示正常
在这里插入图片描述
提交后控制台显示如下:
在这里插入图片描述
此时中文显示正常

标签:中文,UTF,IDEA,乱码,回传,控制台,post
来源: https://blog.csdn.net/qq_46600445/article/details/116198192