SpringMVC学习02

获取请求

@RequestParam("userid") String userid;                    //获取请求参数
@RequestHeader("User-Agent")String userAgent;            //获取请求头
@CookieValue(value = "JSESSIONID", required = false);    //获取Cookie

解决乱码

web.xml

<filter>
    <filter-name>characterEncodingFilter</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

注意:要求字符编码Filter在其他filter之前

数据输出

** Map

@RequestMapping("/handle01")
public String handle01(Map map) {
    System.out.println("访问了handle01");
    map.put("msg","你好");
    return "success";
}

** Model

@RequestMapping("/handle01")
public String handle01(Model model) {
    System.out.println("访问了handle01");
    model.addAttribute("msg", "modelsss");
    return "success";
}

** ModelMap

@RequestMapping("/handle01")
public String handle01(ModelMap modelMap) {
    System.out.println("访问了handle01");
    modelMap.addAttribute("msg", "modelsss");
    return "success";
}

返回类型使用ModelAndView

@RequestMapping("/handle01")
public ModelAndView handle01(ModelMap modelMap) {
    System.out.println("访问了handle01");
    modelMap.addAttribute("msg", "modelsss");
    ModelAndView modelAndView = new ModelAndView("success");    //success指定目标地址
    return modelAndView;
}