zoukankan      html  css  js  c++  java
  • SpringMVC其他说明(六)

    1. 编码问题

    在web.xml中配置过滤器:

    <!--
        源码:spring-web.jar 
        功能:字符集过滤器,设置编码集为UTF-8,解决POST的中文乱码问题。
        参数说明:
            encoding:request的编码集(request.setCharacterEncoding("UTF-8"))
            forceEncoding:默认为false设置为true,response的编码集也强制转变成UTF-8(response.setCharacterEncoding("UTF-8"))
    -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-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<String, String> getMap() { 
        Map<String, String> map = new HashMap<String, String>(); 
        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"。

    时刻与技术进步,每天一点滴,日久一大步!!! 本博客只为记录,用于学习,如有冒犯,请私信于我。
  • 相关阅读:
    memcached(三)--参数
    memcached(二)--安装
    memcached(一)--前言
    如何高性能的给UIImageView加个圆角?(不准说layer.cornerRadius!)
    ios打包ipa的四种实用方法
    怎么调试EXC_BAD_ACCESS错误
    iOS应用性能调优的25个建议和技巧
    3D Touch
    segment
    低版本Xcode 出现could not find developer disk image问题
  • 原文地址:https://www.cnblogs.com/myitnews/p/11569149.html
Copyright © 2011-2022 走看看