数据处理(传值& 乱码)
处理前端提交的数据
1.提交的域名称和处理方法的参数名一致时 /hello?name=akagi
@RequestMapping("/hello") public String hello666(String name){ //封装数据 System.out.println(name); return "jojohello"; }
2.不一样的时候 @RequestParam /hello?username=akagi
@RequestMapping("/hello") public String hello666(@RequestParam("username") String name){ //封装数据 System.out.println(name); return "jojohello"; }
3.是一个对象的时候, 参数使用对象( 属性名必须一致)即可 /hello?name=akagi & id=1 &age=15
@RequestMapping("/hello") public String hello666(User user){ //封装数据 System.out.println(user); return "jojohello"; }
提交给前端数据
1.通过 Model
@RequestMapping("/hello") //真实访问地址 项目名/*/hello public String hello666(Model model){ //封装数据 model.addAttribute("msg","hello anno"); return "jojohello"; //会被视图解析器处理 jsp目录下的jsp文件 }
2.通过ModelMap (继承了LinkMap 除了实现自身方法,同样继承了一些方法和特性)
@RequestMapping("/hello") //真实访问地址 项目名/*/hello public String hello666(ModelMap model){ model.addAttribute("msg","hello anno"); return "jojohello"; //会被视图解析器处理 jsp目录下的jsp文件 }
处理乱码
form表单
<form action="${pageContext.request.contextPath }/e/t" method="post"> <input type="text" name="name"> <input type="submit"> </form>
接收
@RequestMapping("/e/t")
public String text(Model model,String name){
//封装数据
model.addAttribute("msg",name);
System.out.println(name);
return "jojohello";
}
过滤器处理乱码 (web.xml)
1.自定义过滤器

<filter> <filter-name>encoding</filter-name> <filter-class>com.ljm.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

import javax.servlet.*; import java.io.IOException; public class EncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request,response); } @Override public void destroy() { } }
2.调用过滤器

<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>
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术