GET請求中文亂碼
如果我們傳遞的參數中有中文,你會發現接收到的參數會出現中文亂碼問題。
發送請求:
出現亂碼的原因相信大家都清楚,Tomcat8.5以後的版本已經處理了中文亂碼的問題,但是IDEA中的Tomcat插件目前隻到Tomcat7,是以需要修改pom.xml來解決GET請求中文亂碼問題
http://localhost/commonParam?name=張三&age=1
POST發送參數請求中文亂碼
發送請求與參數:
接收參數:
和GET一緻,不用做任何修改
發送方式;
解決中文亂碼方案:配置過濾器
建立SpringMVC項目
目錄
補全目錄結構 :添加java項結構
導入jar包
将pom.xml中多餘的内容删除掉,再添加SpringMVC需要的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>SpringMVC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMVC Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
</dependencies>
<!--導入插件-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
添加tomcat運作快捷鍵
建立配置類 SpringMvcConfig.class
package com.itheima.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringmvcConfig {
}
建立Controller類
package com.itheima.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//@Controller:Springmvc的bean标志
@Controller
//@RequestMapping("/user")
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("郭浩康第一個Sprintmvc項目建立成功");
return "{spring1save()...}";
}
@RequestMapping("/delete")
@ResponseBody
public String delete(){
System.out.println("");
return "{detete删除運作成功}";
}
@RequestMapping("/kk")
@ResponseBody
public String kk(String name,int age){
System.out.println("收到參數:"+name);
System.out.println("收到參數:"+age);
return "{kk成功}";
}
}
使用配置類ServletConfig替換web.xml
将web.xml删除,換成ServletContainersInitConfig
package com.itheima.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class ServletConfig extends AbstractDispatcherServletInitializer {
//加載Springmvc容器
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
ctx.register(SpringmvcConfig.class);
return ctx;
}
//設定請求處理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
//加載Spring容器
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
運作結果
控制台接收到參數