SpringMVC learning (three) incoming and outgoing request data

SpringMVC learning (three) incoming and outgoing request data

1. Data transfer

Front-end data is transferred to the back-end

1.1, @RequestParam annotation

Useless @ResquestParame annotation method

@RequestMapping("/hello")
public String hello(String name){
    
    
    System.out.println(name);
    return "hello";
}

The data input method in the web page: http://localhost:8080/hello? name =song needs to be consistent with the parameter name in the method

If @ResqusetParmae ​​annotation is used

@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
    
    
    System.out.println(name);
    return "hello";
}

The data input method in the web page: http://localhost:8080/hello? username =song can be defined by yourself can be inconsistent with the parameter name in the method

@Controller
public class HelloController {
    
    
    /**
     * @RequestParam 获取请求的参数,参数默认必须要带
     *      value 指定的key
     *      required 参数是否必须带
     *      defaultValue 默认参数的值
     * @param username
     * @return
     */
    @RequestMapping("/handle01")
    public String helloHandle(
            @RequestParam(value = "user",required = false,defaultValue = "no no ")String username
            , @RequestHeader("User-Agent") String userAgent){
    
    
        System.out.println("这个变量值:"+username);
        System.out.println("请求头中浏览器的信息:"+userAgent);
        return "success";
    }
}
  • Use @RequestParam at the input parameter of the processing method to pass the request parameters to the request method
  • value: parameter name
  • required: Is it necessary? The default is true, which means that the request parameter must contain the corresponding parameter, if it does not exist, an exception will be thrown
  • defaultValue: The default value, which is used when no parameters are passed

Use pojo as a parameter

@RequestMapping("/user")
public String user(User user){
    
    
    System.out.println(user);
    return "hello";
}

Data input method in the web page: http://localhost:8080/user? name=song&age=19

2. Data output

Data display to the front end

2.1, the description of SpringMVC output model data

  • ModelAndView : When the processing method return value type is ModelAndView, the method body can add model data through the object
  • Map and Model : When the input parameters are org.springframework.ui.Model, org.springframework.ui.ModelMap or java.uti.Map, when the processing method returns, the data in the Map will be automatically added to the model.
  • @SessionAttributes : Temporarily store an attribute in the model to HttpSession so that this attribute can be shared between multiple requests
  • @ModelAttribute : After the annotation is marked on the method input parameter, the object of the input parameter will be placed in the data model

ModelAndView use

@RequestMapping("/hello")
public ModelAndView testModelAndView(){
    
    
    System.out.println("testModelAndView");
    String viewName = "success";
    ModelAndView mv = new ModelAndView(viewName );
    mv.addObject("time",new Date().toString()); //实质上存放到request域中 
    return mv;
}

Use of Model and Model

@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, Model model,ModelMap modelmap){
    
    
    //封装要显示到视图中的数据
    //相当于req.setAttribute("name",name);
    model.addAttribute("msg",msg);
    
    model.addAttribute("name",name);
    
    System.out.println(msg);
    
    System.out.println(name);
    return "test";
}

3. Solve the problem of Chinese garbled characters

method one

Write your own filter Filter

public class CharacterEndoingFilter implements Filter {
    
    
    public void init(FilterConfig filterConfig) throws ServletException {
    
    
    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    
    
        //解决乱码问题
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }
    public void destroy() {
    
    
    }
}

Register in web.xml

 	<filter>
        <filter-name>ShowFilter</filter-name>
        <filter-class>com.song.filter.CharacterEndoingFilter</filter-class>
    </filter>
   
    <filter-mapping>
        <filter-name>ShowFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Method Two

Use the filters provided in 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>

Way Three

You can find ways to solve most of the garbled problems on the Internet

You can check the official SpringMVC documentation yourself

Recommended learning SpringMVC video B station mad God said java or still in Silicon Valley

Thank you all for reading! If there is a mistake in the above, please correct it

Guess you like

Origin blog.csdn.net/qq_44763720/article/details/107909631