Python 官方文档:入门教程 => 点击学习
目录一、数据处理1. 处理提交数据2. 数据显示到前端二、乱码问题一、数据处理 1. 处理提交数据 1.提交的名称和方法的参数名一致 //localhost:8080/user/
1.提交的名称和方法的参数名一致
//localhost:8080/user/t1?name=xxx;
@GetMapping("/t1")
public String test1(String name, Model model){
//1.接收前端参数
System.out.println("接收到前端的参数为:" + name);
//2.将返回的结果传递给前端
model.addAttribute("msg", name);
//3.跳转视图
return "test";
}
2.提交的名称和方法的参数名不一致
//加上@RequestParam("username")就知道是从前端接收过来localhost:8080/user/t1?username=xxx;。此时必须要通过username识别
@GetMapping("/t1")
public String test1(@RequestParam("username") String name, Model model){
//1.接收前端参数
System.out.println("接收到前端的参数为:" + name);
//2.将返回的结果传递给前端
model.addAttribute("msg", name);
//3.跳转视图
return "test";
}
3.提交一个对象
//前端接收的是一个对象:id, name, age
//localhost:8080/user/t1?id=1&name=xxx&age=2;
@GetMapping("/t2")
public String test2(User user){
System.out.println(user);
//3.跳转视图
return "test";
}
使用对象,前端传递的参数名和对象名必须一致,否则为空。
2.1 ModelAndView
public ModelAndView handleRequest(httpservletRequest HttpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","ControllerTest1");
modelAndView.setViewName("test");
return modelAndView;
}
2.2 Model
@RequestMapping("/t2")
public String test(Model model){
model.addAttribute("msg", "ControllerTest2");
return "test";
}
2.3 ModelMap
@GetMapping("/t3")
public String test3(@RequestParam("username") String name,Model map){
map.addAttribute("name",name);
return "test";
}
对比:
1.fORM表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/encoding/t1" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
2.写一个controller
@Controller
public class EncodinGController {
@PostMapping("/encoding/t1")
public String test(String name, Model model){
model.addAttribute("msg",name);
return "test";
}
}
3.测试结果
前面弄得好好的,结果出了个这
解决办法:
看一下自己Tomcat中的是不是设置的UTF-8。在自己下载路径下-conf-server.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URLEncoding="UTF-8"/>
1)修改提交方式
把提交方式post方法改成get方法
2)spring提供了过滤器,可以在WEB.xml中直接配置。(这个基本够用)
<!--2.配置 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>
public class EncodingFilter 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;
}
}
同样需要在web.xml中进行配置
到此这篇关于Java解决前端数据处理及乱码问题的文章就介绍到这了,更多相关Java数据处理乱码内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java解决前端数据处理及乱码问题
本文链接: https://lsjlt.com/news/160835.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0