zoukankan      html  css  js  c++  java
  • SpringMVC(三)-- springmvc的系统学习之数据的处理,乱码及restful

    资源:尚学堂 邹波 springmvc框架视频

    一、提交数据的处理

    1、提交的域名称和处理方法的参数一致

    (1)提交的数据:http://localhost:8080/data/hello.do?name=zhangsan

          此处的name即为域名称。

    (2)后台方法

    @RequestMapping("/hello")
    public String hello(String name) {}

    此时后台则可接收到前台传递的值:zhangsan

    2、提交的域名称和处理方法的参数不一致

    (1)提交的数据:http://localhost:8080/data/hello.do?uname=zhangsan

          此处的name即为域名称。

    (2)后台方法

    @RequestMapping("/hello")
    public String hello(@RequestPara("uname") String name) {}
     

    3、提交是一个对象

    若提交的表单域名和对象的属性一致,则方法参数使用对象即可。

    (1)提交的数据:http://localhost:8080/data/hello.do?name=zhangsan&&pwd=123456

    (2)后台方法

    @RequestMapping("/hello")
    public String hello(User user) {}

    二、将数据显示到UI层

    (1)通过ModelAndView  —— 需要视图解析器

    (2)通过ModelMap —— 不需要视图解析器

    @RequestMapping("/hello")
    public String hello(ModelMap map) {
           map.addAttribute("name","zhangsan");
           return "index.jsp";
    }
    这两种方法都相当于request.setAttribute("name","zhangsan");
    三、乱码
    1、post乱码
    用springmvc 中提供的CharacterEncodingFilter过滤器解决
    在web.xml中配置如下

    2、get方式乱码

    方法一:修改tomcat的配置

    方法二:自己写过滤器

    四、restful风格的url

    1、前台:http://localhost:8080/data/delete/1.do

    后台:

    //相当于delete?id=1
    @RequestMapping("/delete/${id}")
    public String hello(@PathVariable int id){}

    此时接收到前台页面传递过来的参数:1

    2、前台:http://localhost:8080/data/1/123/delete.do

    后台:

    @RequestMapping("${id}/${uid}/delete/")
    public String hello(@PathVariable int uid, @PathVariable int id){}

    此时,uid=1, id= 123

    若是写成以下方式,则uid=123, id=1

    @RequestMapping("${id}/${uid}/delete/")
    public String hello(@PathVariable("id")  int uid, @PathVariable("uid") int id){}
     
  • 相关阅读:
    智器SmartQ T7实体店试用体验
    BI笔记之SSAS库Process的几种方案
    PowerTip of the Day from powershell.com上周汇总(八)
    PowerTip of the Day2010071420100716 summary
    PowerTip of the Day from powershell.com上周汇总(十)
    PowerTip of the Day from powershell.com上周汇总(六)
    重新整理Cellset转Datatable
    自动加密web.config配置节批处理
    与DotNet数据对象结合的自定义数据对象设计 (二) 数据集合与DataTable
    在VS2003中以ClassLibrary工程的方式管理Web工程.
  • 原文地址:https://www.cnblogs.com/OuZeBo/p/5949674.html
Copyright © 2011-2022 走看看