zoukankan      html  css  js  c++  java
  • springmvc(5)数据的处理

    1.提交数据的处理

    a)提交的域名称和提交的参数一致即可:

    http://localhost:8080/hello?name=zhangsan

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

    b)如果域名称和提交的参数名不一致:

    http://localhost:8080/hello?uname=zhangsan

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

    如果uname没有被转过来,将会报错

    c)如果提交的是一个对象:

    提交的域名和对象的属性名一致,处理器的参数使用对应的对象即可(这样写的前提条件是User必须有一个默认构造器,就是无参构造器,否则会报错):

    http://localhost:8080/user?name=zhangsan&pws=123

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

    实体类:
    public class User {
    private String name;
    private String pws;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getPws() {
    return pws;
    }

    public void setPws(String pws) {
    this.pws = pws;
    }
    }
     

    2.将数据显示到ui层:

    a)通过ModelAndView,有没有视图解析器都一样,但是需要将ModelAndView返回。

      @RequestMapping("/hello")
        public ModelAndView hello(HttpServletRequest request,HttpServletResponse response){
            ModelAndView modelAndView = new ModelAndView();
            //相当于request.setAttribute("msg","modelAndView");
            modelAndView.addObject("msg","modelAndView");
            modelAndView.setViewName("hello");
            return modelAndView;
        }

    b)通过ModelMap,有没有视图解析器都一样,但是需要将ModelMap作为处理器的参数。

    @RequestMapping("/hello")
    public String hello(ModelMap modelMap){
    //相当于request.setAttribute("msg","modelMap");
    modelMap.addAttribute("msg","modelMap");
    return "index.jsp";
    }
  • 相关阅读:
    最大子数组的求解(包括首尾相接成环)
    学习进度条第五周
    学习进度条第四周
    学习进度条第三周
    软件工程概论作业二
    软件工程个人作业01
    软件工程概论总结
    梦断代码阅读笔记01
    构建之法阅读笔记04
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/yuby/p/11019847.html
Copyright © 2011-2022 走看看