zoukankan      html  css  js  c++  java
  • springmvc 梳理5--传参

    再也不用 getParamter 了

    1. 建一个User类

    package com.xinzhi.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.beans.factory.annotation.Autowired;
    
    /**
     * @author sr
     * @date 2021/1/25
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private int id;
        private String username;
        private String password;
    }
    View Code

    2. 修改FirstController.java

    @RequestMapping(value = "/add",method = RequestMethod.POST)
        public String addUser(User user) throws Exception {
        //模型里封装数据
            System.out.println(user);
            return "hellomvc";
        }

    要什么,加什么

        @RequestMapping(value = "/add",method = RequestMethod.POST)
        public String addUser(User user,HttpServletRequest request) throws Exception {
        //模型里封装数据
            request.setAttribute("user",user);
            System.out.println(user);
            return "add";
        }

    或这样

    public class FirstController {
        @RequestMapping(value = "/add")
        public String addUser(Integer id,String username,String password,HttpServletRequest request) throws Exception {
        //模型里封装数据
            request.setAttribute("user",new User(id,username,password));
            
            return "hellomvc";
        }

    但如果这个参数名跟user类的私有变量不一样,就传不过去

     解决方法:

    3. 在index.jsp里面做个表单

    <form action="/user/add" method="post">
        <input name="id" >
        <input name="username">
        <input name="password">
        <input type="submit" value="提交">
    </form>

    4.测试

    成功

  • 相关阅读:
    js正则 转载
    asp.net中打开新窗口的多种方法(转载)
    ajax有两种提交数据的方式,分别为get和post(转)
    DropDownList 绑定(转载)
    CentOS网络配置
    Java内存区域-- 运行时数据区域
    Spring Ioc--Bean装配
    Spring--Spring容器
    java正则表达式 --简单认识
    Leetcode 402:移除K个数字
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14327903.html
Copyright © 2011-2022 走看看