SpringMVC其他说明(六)

2020-12-13 14:45

阅读:340

标签:问题   通过   nbsp   ppi   ice   --   XML   work   lse   

1. 编码问题

在web.xml中配置过滤器:


filter>
    filter-name>encodingFilterfilter-name>
    filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    init-param>
        param-name>encodingparam-name>
        param-value>UTF-8param-value>
    init-param>
    init-param>
        param-name>forceEncodingparam-name>
        param-value>trueparam-value>
    init-param>
filter>
filter-mapping>
    filter-name>encodingFilterfilter-name>
    url-pattern>/*url-pattern>
filter-mapping>

2. Controller的返回类型

  • ModelAndView
  • Model 
  • ModelMap 
  • Map 
  • View 
  • String 
  • Void

(1) ModelAndView

controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。

@RequestMapping("/test")
public ModelAndView test(){
     //通过ModelAndView构造方法可以指定返回的页面名称,也可以通过setViewName()方法跳转到指定的页面
    ModelAndView mav=new ModelAndView("hello");
    mav.addObject("time", new Date());
    mav.getModel().put("name", "caoyc");
    return mav;
}

(2) Map

@RequestMapping("/demo2/show") 
public Map getMap() { 
    Map map = new HashMap(); 
    map.put("key1", "value-1"); 
    map.put("key2", "value-2"); 
    return map; 
}

在jsp页面中可直通过${key1}获得到值, map.put()相当于request.setAttribute方法。

(3) View

可以返回pdf excel等。

(4) String

(4.1) 逻辑视图名:

@RequestMapping(value="/showdog")
public String hello1(){
    return "hello";
}

(4.2) 直接将返回值输出到页面(添加@ResponseBody注解):

@RequestMapping(value="/print")
@ResponseBody
public String print(){
    String message = "Hello World, Spring MVC!";
    return message;
}

(4.3) redirect重定向:

@RequestMapping(value="/updateitem.action")
public String updateItem(Items item, Model model) {    
    ItemService.updateItem(item);    
    //修改item后跳转到列表页面
    return "redirect:/item/itemlist.action";
}

(4.4) forward转发:

@RequestMapping(value="/updateitem.action")
public String updateItem(Items item, Model model) {    
    ItemService.updateItem(item);
    //修改item后跳转到列表页面
    return "forward:/item/itemlist.action";
}

(4.5) void:

如果返回值为空,则响应的视图页面对应为访问地址

@RequestMapping("/index")
public void index() {
    return;
}

对应的逻辑视图名为"index"。

 

SpringMVC其他说明(六)

标签:问题   通过   nbsp   ppi   ice   --   XML   work   lse   

原文地址:https://www.cnblogs.com/myitnews/p/11569149.html


评论


亲,登录后才可以留言!