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

    1 提交数据的处理

    a提交的域名城和处理方法的参数名一致即可

    提交的数据

    处理方法

    @RequestMapping("/hello")
        public String hello(String name){
            System.out.println(name);
            return "index.jsp";
            //这种方式不需要视图解析器    
        }

    b如果域名城和参数名不一致

    提交的数据

    处理方法

    @RequestMapping("/hello")
        public String hello(@RequestParam("uname")String name){
            System.out.println(name);
            return "index.jsp";
            //这种方式不需要视图解析器    
        }

    c提交一个对象

    要求提交的表单域名和对象的属性名一致,参数使用对象即可

    处理方法

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

    实体类

    package com.sgcc.entity;
    
    public class User {
        private int id;
        private String name;
        private String pwd;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
        }
        
        
        
    
    }

    2 将数据显示到ui层

    第一种通过ModelAndView 需要通过视图解析器

    第二种通过ModelMap来实现 不需要通过视图解析器

    ModelMap需要作为处理方法的参数

    @RequestMapping("/hello")
        public String hello(@RequestParam("uname")String name,ModelMap model){
            //相当于request.setAttribute("name",name);
            model.addAttribute("name", name);
            System.out.println(name);
            return "index.jsp";
            //这种方式不需要视图解析器    
        }

    ModelAndView 和ModelMap的区别

    相同点都可以将数据封装显示到表示层页面中

    不同ModelAndView 可以指定跳转到视图,而ModelMap不能

    ModelAndView 需要视图解析器 ModelMap不需要配置

  • 相关阅读:
    Spring、SpringMVC和Springboot的区别(网摘)
    scikit-learn中的主成分分析(PCA)的使用
    便捷的php操作mysql库MysqliDb
    Windows下单机安装Spark开发环境
    在windows上安装scikit-learn开发环境
    Code Igniter + PHP5.3 + SqlServer2008配置
    ubuntu下安装php memcache扩展
    排序——选择排序
    线性回归与梯度下降算法
    ubuntu 允许端口被连接
  • 原文地址:https://www.cnblogs.com/alloevil/p/6067796.html
Copyright © 2011-2022 走看看