SpringMVC(二)--------控制器、结果跳转与数据处理、乱码问题
4、RestFul和控制器
4.1 控制器Controller
- 控制器负责提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现(建议使用注解定义)
- 控制器负责解析用户的请求,并将其转换为一个模型
- 在SpringMVC中一个控制器类可以包含多个方法
- 在SpringMVC中,对于Controller的配置方式有很多种
4.2 实现Controller接口
- Controller是一个接口,在在org.springframework.web.servlet.mvc包下,接口中只有一个方法
//实现该接口的类获得控制器功能
public interface Controller {
//处理请求且返回一个模型与视图对象
ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
}
- 编写Controller类
//定义控制器
//只要实现了Controller接口的类,说明这就是一个控制器
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
//添加数据
mv.addObject("msg","ControllerTest1");
//跳转到哪个页面
mv.setViewName("test");
return mv;
}
}
- 在spring配置文件中注册请求的bean,且只留下视图解析器
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--用于拼接路径-->
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/t1" class="com.zmt.controller.ControllerTest1"/>
</beans>
- 编写test.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
说明
- 实现接口Controller定义控制器是比较老的方法
- 缺点:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller,定义的方式比较麻烦
4.3 使用注解@Controller
- @Controller注解类型用于声明Spring类的实例是一个控制器(在学习IOC时还提到了另外3个注解)
@Component :组件
@Service :service层
@Controller :controller层
@Respository :Dao层
- Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类,为了保证Spring能找到你的控制器,需要在配置文件中声明组件扫描
- 编写spring.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描包,加注解-->
<context:component-scan base-package="com.zmt.controller"/>
<!--视图解析器:之后会学习一些模板引擎 Thymeleaf,Freemarker-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--用于拼接路径-->
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/t1" class="com.zmt.controller.ControllerTest1"/>
</beans>
- 编写ControllerTest2类,使用注解实现
@Controller //代表这个类会被Spring接管
// 被这个注解的类中的所有方法,如果返回值是String,并且有具体页面可以跳转,那么就会被视图解析器解析
public class ControllerTest2 {
@RequestMapping("/t2")
public String test2(Model model){
model.addAttribute("msg","ControllerTest2");
//拼接视图
return "test";
}
}
-
运行tomcat,测试
-
可以发现,我们的两个请求都可以指向一个视图(test),但是页面结果的结果是不一样的,从这里可以看出视图是被复用的,而控制器与视图之间是弱偶合关系。
-
注解方式是平时使用的最多的方式!
4.4 RequestMapping
-
@RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
-
为了测试结论更加准确,我们可以加上一个项目名测试 myweb
-
只在方法上加注解
@Controller
public class ControllerTest3 {
@RequestMapping("/t2")
public String test1(Model model){
model.addAttribute("msg","ControllerTest3");
return "test";
}
}
- 访问路径:/t2
- 同时在方法和类上加注解
@Controller
@RequestMapping("/c3")
public class ControllerTest3 {
@RequestMapping("/t2")
public String test1(Model model){
model.addAttribute("msg","ControllerTest3");
return "test";
}
}
- 访问路径:/c3/t2,在t2有重名的情况下,如果不加类的路径会跳转到该同名页面
4.5 RestFul风格
概念
- Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能
- 资源:互联网所有的事情都可以被抽象为资源
- 资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作
- 分别对应添加、删除、修改、查询
传统方式操作资源
-
通过不同的参数来实现不同的效果,方法单一,post和get
-
http://127.0.0.1/item/queryItem.action?id=1 查询,GET
http://127.0.0.1/item/saveItem.action 新增,POST
http://127.0.0.1/item/updateItem.action 更新,POST
http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST
使用RESTful操作资源
-
可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!
-
http://127.0.0.1/item/1 查询,GET
http://127.0.0.1/item 新增,POST
http://127.0.0.1/item 更新,PUT
http://127.0.0.1/item/1 删除,DELETE
测试
- 新建一个类RestFulController
@Controller
public class RestFulController {
//原来的:http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
//RestFul:http://localhost:8080/springmvc_04_controller_war_exploded/add/a/b
@RequestMapping("/add")
public String test1(int a, int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
- 在SpringMVC中可以使用@PathVariable注解,让方法参数的值对应绑定到一个URL模板变量上
@Controller
public class RestFulController {
//原来的:http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
//RestFul:http://localhost:8080/springmvc_04_controller_war_exploded/add/1/2
@RequestMapping("/add/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
-
这种情况下,原来的方式无法使用了,只能用这种方法
再次测试
- 修改对应参数类型
@Controller
public class RestFulController {
//原来的:http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
//RestFul:http://localhost:8080/springmvc_04_controller_war_exploded/add/a/b
@RequestMapping("/add/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable String b, Model model){
String res = a + b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
- 输出结果为字符串拼接结果
使用method属性指定请求类型
- 用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型,如:GET、POST、HEAD、OPTIONS、PUT、DELETE、TRACE等
- 测试一下,增加一个方法
@Controller
public class RestFulController {
//原来的:http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
//RestFul:http://localhost:8080/springmvc_04_controller_war_exploded/add/a/b
//使用value和path结果相同
@RequestMapping(value="/add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a,@PathVariable String b, Model model){
String res = a + b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
- 另一种方法
@Controller
public class RestFulController {
//原来的:http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
//RestFul:http://localhost:8080/springmvc_04_controller_war_exploded/add/a/b
@GetMapping("add/{a}/{b}")
public String test1(@PathVariable int a,@PathVariable String b, Model model){
String res = a + b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
使用路径变量的好处
- 获得参数更加方便,框架会自动进行类型转换
- 通过路径变量的类型可以约束访问参数,如果类型不一样,则访问不到对应的请求方法,例如:这里访问的路径是/add/1/ssss,则路径与方法不匹配,而不会是参数转换失败
4.6 小结
- Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
- 所有的地址栏请求默认都会是 HTTP GET 类型的。
- 方法级别的注解变体有如下几个:组合注解
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
- @GetMapping 是一个组合注解,平时使用的会比较多!
- 它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。
拓展:小黄鸭调试法
-
场景一:我们都有过向别人(甚至可能向完全不会编程的人)提问及解释编程问题的经历,但是很多时候就在我们解释的过程中自己却想到了问题的解决方案,然后对方却一脸茫然。
-
场景二:你的同行跑来问你一个问题,但是当他自己把问题说完,或说到一半的时候就想出答案走了,留下一脸茫然的你。
-
其实上面两种场景现象就是所谓的小黄鸭调试法(Rubber Duck Debuging),又称橡皮鸭调试法,它是我们软件工程中最常使用调试方法之一。
-
此概念据说来自《程序员修炼之道》书中的一个故事,传说程序大师随身携带一只小黄鸭,在调试代码的时候会在桌上放上这只小黄鸭,然后详细地向鸭子解释每行代码,然后很快就将问题定位修复了。
5、结果跳转方式
5.1 ModelAndView
-
设置ModelAndView对象,根据view的名称和视图解析器跳转到指定页面
-
页面真正位置:{视图解析器前缀}+viewName+{视图解析器后缀}
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--用于拼接路径-->
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
- 对应的Controller类
//定义控制器
//只要实现了Controller接口的类,说明这就是一个控制器
public class ControllerTest1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
//添加数据
mv.addObject("msg","ControllerTest1");
//跳转到哪个页面
mv.setViewName("test");
return mv;
}
}
5.2 ServletAPI
- 通过设置ServletAPI,不需要视图解析器
- 通过HttpServletResponse进行输出
- 通过HttpServletResponse实现重定向
- 通过HttpServletRequest实现转发
@Controller
public class ResultGo {
//进行输出
@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.getWriter().println("Hello,Spring BY servlet API");
}
//实现重定向
@RequestMapping("/result/t2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.sendRedirect("/index.jsp");
}
@RequestMapping("/result/t3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//实现请求转发
req.setAttribute("msg","/result/t3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
5.3 SpringMVC
-
通过SpringMVC来实现转发和重定向 - 无需视图解析器;
-
测试前,需要将视图解析器注释掉
-
默认为forward转发(也可以加上)
-
redirect转发需特别加
@Controller
public class ResultSpringMVC {
@RequestMapping("/rsm/t1")
public String test1(){
//转发
return "/index.jsp";
}
@RequestMapping("/rsm/t2")
public String test2(){
//转发二
return "forward:/index.jsp";
}
@RequestMapping("/rsm/t3")
public String test3(){
//重定向
return "redirect:/index.jsp";
}
}
-
通过SpringMVC来实现转发和重定向 - 有视图解析器
-
重定向 , 不需要视图解析器 , 本质就是重新请求一个新地方嘛 , 所以注意路径问题
-
可以重定向到另外一个请求实现
-
默认为forward转发(不可以加上)
-
redirect转发需特别加
@Controller
public class ResultSpringMVC2 {
@RequestMapping("/rsm2/t1")
public String test1(){
//转发
return "test";
}
@RequestMapping("/rsm2/t2")
public String test2(){
//重定向
return "redirect:/index.jsp";
//return "redirect:hello.do"; //hello.do为另一个请求/
}
}
6、数据处理
6.1 处理提交数据
6.1.1 提交的域名称和处理方法的参数名称一致
- 提交数据:/user/t1?name=zzz
- 处理方法
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/t1")
public String test1(String name, Model model){
//1.接收前端参数
System.out.println("接收到前端的参数为:"+name);
//2.将返回的结果传输给前端
model.addAttribute("msg",name);
//3.跳转视图
return "test";
}
}
- 后台输出:接收到前端的参数为:zzz
6.1.2 提交的域名称和处理方法的参数名不一致
-
提交数据:/user/t1?name1=zzz
-
处理方法
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/t1")
//@RequestParam("name1") : name1提交的域的名称 .
public String test1(@RequestParam("name1") String name, Model model){
//1.接收前端参数
System.out.println("接收到前端的参数为:"+name);
//2.将返回的结果传输给前端
model.addAttribute("msg",name);
//3.跳转视图
return "test";
}
}
- 后台输出:zzz
6.1.3 提交的是一个对象
- 要求表单的表单域和对象的属性名一致,参数使用对象即可
- User实体类
public class User {
private int id;
private String name;
private int age;
}
-
提交数据:/user/t2?id=1&name=zzz&age=18
-
处理方法
//前端接收的是一个对象:id name age
@GetMapping("/t2")
public String test2(User user){
System.out.println(user);
return "test";
}
- 后台输出:User{id=1, name=‘zzz’, age=18}
- 如果使用对象的话,前端传递参数和对象名必须一致,否则是null
说明
-
接收前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用
-
假设传递的是一个对象User,匹配User对象中的字段名,如果名字一致可以匹配,否则匹配不到
6.2 数据显示到前端
6.2.1 第一种 : 通过ModelAndView
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
6.2.2 第二种 : 通过ModelMap
@RequestMapping("/hello")
public String hello(String name, ModelMap modelMap){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
modelMap.addAttribute("name",name);
System.out.println(name);
return "hello";
}
6.2.3 第三种 : 通过Model
@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("msg",name);
System.out.println(name);
return "test";
}
6.3 对比
-
Model 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解
-
ModelMap 继承了 LinkedMap ,除了实现了自身的一些方法,同样的继承 LinkedMap 的方法和特性
-
ModelAndView 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转
-
当然更多的以后开发考虑的更多的是性能和优化,就不能单单仅限于此的了解
-
请使用80%的时间打好扎实的基础,剩下18%的时间研究框架,2%的时间去学点英文,框架的官方文档永远是最好的教程
7、乱码问题
7.1 测试步骤
- 我们可以在首页编写一个提交的表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/e/t1" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
- 后台编写对应的处理类
@Controller
public class EncodingController {
//过滤器解决乱码
@PostMapping("/e/t1")
public String test1(String name, Model model){
System.out.println(name);
model.addAttribute("msg",name); //获取表单提交的值
return "test"; //跳转表单
}
}
- 输入中文测试,发现乱码,添加过滤器
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
- 修改web.xml文件
<?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">
<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>
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.zmt.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- SpringMVC给我们提供了一个过滤器,在web.xml中直接配置
<?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">
<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>
<!--配置SpringMVC的乱码过滤器-->
<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>
</web-app>
- 注意这里写/无法过滤jsp页面,不能解决乱码问题,在url-pattern中写/*可以解决乱码问题。但是一些极端情况下,这个过滤器对get的支持不好
- 修改方法:修改tomcat配置文件,在tomcat文件夹下—conf—server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8" />
- 自定义过滤器(万能解决方式)
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* 解决get和post请求 全部乱码的过滤器
*/
public class GenericEncodingFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//处理response的字符编码
HttpServletResponse myResponse=(HttpServletResponse) response;
myResponse.setContentType("text/html;charset=UTF-8");
// 转型为与协议相关对象
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// 对request包装增强
HttpServletRequest myrequest = new MyRequest(httpServletRequest);
chain.doFilter(myrequest, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
//自定义request对象,HttpServletRequest的包装类
class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
//是否编码的标记
private boolean hasEncode;
//定义一个可以传入HttpServletRequest对象的构造函数,以便对其进行装饰
public MyRequest(HttpServletRequest request) {
super(request);// super必须写
this.request = request;
}
// 对需要增强方法 进行覆盖
@Override
public Map getParameterMap() {
// 先获得请求方式
String method = request.getMethod();
if (method.equalsIgnoreCase("post")) {
// post请求
try {
// 处理post乱码
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else if (method.equalsIgnoreCase("get")) {
// get请求
Map<String, String[]> parameterMap = request.getParameterMap();
if (!hasEncode) { // 确保get手动编码逻辑只运行一次
for (String parameterName : parameterMap.keySet()) {
String[] values = parameterMap.get(parameterName);
if (values != null) {
for (int i = 0; i < values.length; i++) {
try {
// 处理get乱码
values[i] = new String(values[i]
.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
hasEncode = true;
}
return parameterMap;
}
return super.getParameterMap();
}
//取一个值
@Override
public String getParameter(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
if (values == null) {
return null;
}
return values[0]; // 取回参数的第一个值
}
//取所有值
@Override
public String[] getParameterValues(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
return values;
}
}
- 一般情况下,SpringMVC默认的乱码处理就已经能够很好的解决了
- 平时需要多注意乱码问题,在尽可能能设置编码的地方,都设置为统一编码UTF-8