ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据处理(传值& 乱码)

2022-04-28 08:35:22  阅读:146  来源: 互联网

标签:RequestMapping return name public 乱码 数据处理 hello 传值 String


处理前端提交的数据

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";
    }

过滤器处理乱码  

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>
View Code
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() {

    }
}
View Code

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>
View Code

 

标签:RequestMapping,return,name,public,乱码,数据处理,hello,传值,String
来源: https://www.cnblogs.com/liujinmeng/p/16201006.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有