一、解决中文乱码问题

1.请求中文乱码

1.1get请求乱码问题

在toncat8.0以前需要在tomcat/conf/server.xml新增URIEncoding=“UTF-8”,tomcat8.0之后则默认是UTF-8

    <Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443"URIEncoding="UTF-8"/>

1.2POST请求乱码

在Web.xml中进行过滤器配置

<!--对所有的请求进行过滤并将中文字符转为UTF-8编码--><filter><filter-name>characterFilter</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>characterFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

2.响应中文乱码问题

在applicationContext.xml中在<mvc:annotation-driven>标签中配置<mvc:message-converters>

!--    启用SpringMVC中的注解开发模式--><mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.StringHttpMessageConverter"><property name="supportedMediaTypes"><list>
<!--                       response.setContentType("text/html;charset=utf-8")--><value>text/html;charset=utf-8</value></list></property></bean></mvc:message-converters></mvc:annotation-driven>

二、响应是如何输出结果的

1.@ResponseBody使用方法

2.ModelAndView使用方法(建议)

在Controller方法中新增返回类型为ModelAndView的方法。

    @GetMapping("/view")public ModelAndView showView(Integer userId){ModelAndView mav = new ModelAndView("/view.jsp");User user = new User();if (userId == 1){user.setUsername("Lily");}else if(userId == 2){user.setUsername("luaks");}
//        在当前的请求中增加对象mav.addObject("u",user);return mav;}

新增view.jsp页面

<%--Created by IntelliJ IDEA.User: kiellDate: 2021/2/2Time: 11:04To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><h1>Iam View Page</h1><hr>
<h2>UserName:${u.username}</h2>
</body>
</html>

通过访问的不同请求参数来进行不同的数据展示。

也可以通过String和ModelMap来进行

    //String和ModelMap//Controller方法返回String的情况//1.方法被@Response描述,springMVC直接响应String字符串本身。//2.方法不存在@ResponseBody,则springMVC处理String指代的视图(页面)。public String showView1(Integer userId, ModelMap modelMap){String view ="/view.jsp";User user = new User();if (userId == 1){user.setUsername("Lily");}else if(userId == 2){user.setUsername("luaks");}modelMap.addAttribute("u",user);return view;}

三、SpringMVC整合Freemarker

1.引入依赖pom.xml

     <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.9.RELEASE</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.1.9.RELEASE</version></dependency>

2.applicationContext.xml配置freemarker和对freemarker本身进行配置

<!--配置freemarker--><bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<!--        产生最终结果后向客户端输出的字符集--><property name="contentType" value="text/html;charset=UTF-8"></property><property name="suffix" value=".ftl"></property></bean>
<!--对freemarker本身进行配置--><bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/ftl"></property><property name="freemarkerSettings"><props>
<!--         产生最终结果的字符集--><prop key="defaultEncoding">UTF-8</prop></props></property></bean>

3.在WEB-INF目录下新建ftl/test.ftl使用${u.username}

4.新建Controller类

@Controller
@RequestMapping("/fm")
public class freemarkerController {@GetMapping("/test")public ModelAndView showTest(){ModelAndView mav= new ModelAndView("/test");User user = new User();user.setUsername("andy");mav.addObject("u",user);return mav;}
}

5.结果

需要注意的是每次对pom.xml进行改动都需要对war包对依赖进行重新发布。

SpringMVC基础三相关推荐

  1. springmvc跳转html_SpringMVC基础(三)

    本篇文章主要整理了数据处理.乱码处理和Json的相关知识.参考的狂神说的公众号以及视频.所有代码亲测有效. 数据处理主要包括处理提交的数据以及将数据显示到前端. 处理提交的数据一般有三种情况: (1) ...

  2. SpringMVC基础配置及使用

    SpringMVC基础配置及使用 SpringMVC: 1.SpringMVC和Spring的关系:     软件开发的三层架构: web层[表示层.表现层]---->Service层----& ...

  3. javaweb实训第五天下午——SpringMVC基础

    SpringMVC基础 1.课程介绍 2.SpringMVC概述 3.SpringMVC入门 3.1.入门需知 3.1.1.Jar包管理 3.1.2.核心控制器(前端控制器) 3.2.加入相关Spri ...

  4. 瑞思拜 我儿豁 SpringMVC基础 兄弟们冲冲冲

    SpringMVC基础 课程介绍 1. SpringMVC概述: (了解) 2. SpringMVC入门: (掌握) 3. 前端控制器<url-pattern>配置: (掌握) 4. 业务 ...

  5. SpringMVC基础学习之Controller的两种实现方式和RequstMapping注解的使用

    前言: 小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java半年多时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师. 这个SpringM ...

  6. SpringMVC基础

    SpringMVC基础 1.SpringMVC概述 1.1 三层架构 三层架构: 表现层:负责数据展示 业务层:负责业务处理 数据层:负责数据操作 1.2 MVC MVC(Model View Con ...

  7. 一、SpringMVC基础入门,创建一个HelloWorld程序

    一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...

  8. SpringMVC基础学习之Restful风格的简单使用

    前言: 小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java半年多时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师. 这个SpringM ...

  9. [GO语言基础] 三.变量声明、数据类型、标识符及编程练习12题

    作为网络安全初学者,会遇到采用Go语言开发的恶意样本.因此从今天开始从零讲解Golang编程语言,一方面是督促自己不断前行且学习新知识:另一方面是分享与读者,希望大家一起进步.前文介绍了Go的编译运行 ...

最新文章

  1. 使用Go语言遇到的“坑”收集
  2. win10突然只剩下c盘和d盘了_电脑C盘爆满飘红?系统卡?试试这两种解决办法
  3. ​MEMS在未来面临的挑战
  4. 让该死的恶意软件去死吧!!!!!!!
  5. python pkl是什么类型的文件?怎么来打开它?(使用numpy和pickle都能打开)
  6. DDD领域驱动之干货 (一)
  7. 国开计算机专业英语章节测试答案,国开大201x理工英语1第七单元网上测试答案...
  8. 360 n6 linux内核版本,五年26个版本:Linux系统内核全程回顾
  9. React Natvie Fetch工具类
  10. linux 卸载theano,centos 安装theano
  11. pm2启动express项目
  12. Android WebView播放视频flash(判断是否安装flash插件)
  13. 在VMware WorkStation中安装Windows Server 2016
  14. 等保测评中web应用防火墙怎么选择?
  15. python人像录制加声音_Python自动化测试入门必读
  16. 最流行十大在线客服系统排行榜-市场常见客服系统软件排行-2023最新
  17. 计算机有哪些值得参加的比赛
  18. Leetcode:剑指 Offer 58 - II. 左旋转字符串(C++)
  19. 计算机课程计算奇数,计算机网络课设计算校验和讲解
  20. 计算机网络基础 — 网络设备的类型

热门文章

  1. 多少量级才算是高并发
  2. 手写迷你SpringMVC框架
  3. matlab内联函数怎么作图,第1讲:向量,函数和作图
  4. 有极性、无极性电容爆炸的原因
  5. SwitchyOmega插件
  6. 高权重网站外链如何建设和打造?
  7. 在Windows Mobile手机上运行Android
  8. R语言笔记7:functions——编写函数所需的基础知识
  9. matching wildcard is strict, but no declaration can be found for element forcontext:property-plac
  10. TMT: A Transformer-based Modal Translator for Improving Multimodal Sequence Representations in Audio