• SSM46:Get和post发送请求数据


     

    GET请求中文乱码

    如果我们传递的参数中有中文,你会发现接收到的参数会出现中文乱码问题。

    发送请求:http://localhost/commonParam?name=张三&age=1

    出现乱码的原因相信大家都清楚,Tomcat8.5以后的版本已经处理了中文乱码的问题,但是IDEA中的Tomcat插件目前只到Tomcat7,所以需要修改pom.xml来解决GET请求中文乱码问题

     

    POST发送参数请求中文乱码

    发送请求与参数:

    接收参数:

    和GET一致,不用做任何修改

    发送方式;

    解决中文乱码方案:配置过滤器

     

     
    

     

    创建SpringMVC项目

     目录

    补全目录结构 :添加java项结构

     

    导入jar包

    将pom.xml中多余的内容删除掉,再添加SpringMVC需要的依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.itheimagroupId>
    6. <artifactId>SpringMVCartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>SpringMVC Maven Webappname>
    10. <url>http://www.example.comurl>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <maven.compiler.source>1.8maven.compiler.source>
    14. <maven.compiler.target>1.8maven.compiler.target>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>junitgroupId>
    19. <artifactId>junitartifactId>
    20. <version>4.11version>
    21. <scope>testscope>
    22. dependency>
    23. <dependency>
    24. <groupId>javax.servletgroupId>
    25. <artifactId>javax.servlet-apiartifactId>
    26. <version>3.1.0version>
    27. <scope>providedscope>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframeworkgroupId>
    31. <artifactId>spring-webmvcartifactId>
    32. <version>5.2.1.RELEASEversion>
    33. dependency>
    34. dependencies>
    35. <build>
    36. <plugins>
    37. <plugin>
    38. <groupId>org.apache.tomcat.mavengroupId>
    39. <artifactId>tomcat7-maven-pluginartifactId>
    40. <version>2.1version>
    41. <configuration>
    42. <port>80port>
    43. <path>/path>
    44. configuration>
    45. plugin>
    46. plugins>
    47. build>
    48. project>

    添加tomcat运行快捷键

    创建配置类 SpringMvcConfig.class

    1. package com.itheima.config;
    2. import org.springframework.context.annotation.ComponentScan;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. @ComponentScan("com.itheima.controller")
    6. public class SpringmvcConfig {
    7. }

    创建Controller类

    1. package com.itheima.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.ResponseBody;
    5. //@Controller:Springmvc的bean标志
    6. @Controller
    7. //@RequestMapping("/user")
    8. public class UserController {
    9. @RequestMapping("/save")
    10. @ResponseBody
    11. public String save(){
    12. System.out.println("郭浩康第一个Sprintmvc项目创建成功");
    13. return "{spring1save()...}";
    14. }
    15. @RequestMapping("/delete")
    16. @ResponseBody
    17. public String delete(){
    18. System.out.println("");
    19. return "{detete删除运行成功}";
    20. }
    21. @RequestMapping("/kk")
    22. @ResponseBody
    23. public String kk(String name,int age){
    24. System.out.println("收到参数:"+name);
    25. System.out.println("收到参数:"+age);
    26. return "{kk成功}";
    27. }
    28. }

    使用配置类ServletConfig替换web.xml

    将web.xml删除,换成ServletContainersInitConfig

    1. package com.itheima.config;
    2. import org.springframework.web.context.WebApplicationContext;
    3. import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    4. import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
    5. public class ServletConfig extends AbstractDispatcherServletInitializer {
    6. //加载Springmvc容器
    7. @Override
    8. protected WebApplicationContext createServletApplicationContext() {
    9. AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
    10. ctx.register(SpringmvcConfig.class);
    11. return ctx;
    12. }
    13. //设置请求处理
    14. @Override
    15. protected String[] getServletMappings() {
    16. return new String[]{"/"};
    17. }
    18. //加载Spring容器
    19. @Override
    20. protected WebApplicationContext createRootApplicationContext() {
    21. return null;
    22. }
    23. }

    运行结果

    控制台接收到参数

     

     

  • 相关阅读:
    [数据分析与可视化] 基于Python绘制简单动图
    【C语言】转圈报数问题(三种方法--指针,数组)
    MyBatis 如何进行一对一关联查询呢?
    luffy-(13)
    机器学习笔记之概率图模型(五)马尔可夫随机场的结构表示
    低代码招投标应用:全程不见面,一次都不跑,快速优化招投标流程
    [StartingPoint][Tier1]Bike
    低代码PaaS开发平台
    三次握手四次挥手
    数据结构(c语言版) 顺序表
  • 原文地址:https://blog.csdn.net/weixin_51330376/article/details/127673277