springmvc--请求参数的绑定(基本类型,string类型,实体类作为参数以及实体中包含引用数据类型或list集合类型或map集合类型)

注意:前端的请求参数需要和控制器里面的形参名字一致
代码展示
先将三个配置文件配置好
applicationConext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <context:component-scan base-package="com.requestparam.dao"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driver}"/>
        <property name="jdbcUrl" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="user" value="${db.username}"/>
    </bean>
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.requestparam.dao"/>
    </bean>
</beans>

spring-mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.requestparam.web"/>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

实体类的创建
user.java、user2.java、user3.java、role2.java、

package com.requestparam.pojo;

import java.io.Serializable;

public class User implements Serializable {
    private int id;
    private String name;
    private String pwd;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + 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;
    }
}

package com.requestparam.pojo;

import java.io.Serializable;
import java.util.List;

public class User2 implements Serializable {
    private int id;
    private String name;
    private String pwd;
    private List<Role2> role2List;

    @Override
    public String toString() {
        return "User2{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", role2List=" + role2List +
                '}';
    }

    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;
    }

    public List<Role2> getRole2List() {
        return role2List;
    }

    public void setRole2List(List<Role2> role2List) {
        this.role2List = role2List;
    }
}

package com.requestparam.pojo;

import java.io.Serializable;
import java.util.Map;

public class User3 implements Serializable {
    private int id;
    private String name;
    private String pwd;
    private Map<String,Role2> role2Map;

    @Override
    public String toString() {
        return "User3{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", role2Map=" + role2Map +
                '}';
    }

    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;
    }

    public Map<String, Role2> getRole2Map() {
        return role2Map;
    }

    public void setRole2Map(Map<String, Role2> role2Map) {
        this.role2Map = role2Map;
    }
}

package com.requestparam.pojo;

import java.io.Serializable;

public class Role2 implements Serializable {
    private int id;
    private String rolename;

    @Override
    public String toString() {
        return "Role2{" +
                "id=" + id +
                ", rolename='" + rolename + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }
}

编写控制类
UserController 核心

package com.requestparam.web;

import com.requestparam.pojo.User;
import com.requestparam.pojo.User2;
import com.requestparam.pojo.User3;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping("drump")
    public String dump(){
        return "table";
    }

    @RequestMapping("base_param")
    public String base(@RequestParam(value = "id") int uid, Model model){
        model.addAttribute("id",uid);
        System.out.println("基本类型");
        System.out.println("id==>"+uid);
        return "ok";
    }

    @RequestMapping("quote_param")
    public ModelAndView quote(@RequestParam(value = "name") String uname, String pwd){
        ModelAndView mv = new ModelAndView();
        System.out.println("引用类型");
        System.out.println("name-->"+uname+",pwd-->"+pwd);
        mv.addObject("name",uname);
        mv.addObject("pwd",pwd);
        mv.setViewName("ok");
        return mv;
    }

    @RequestMapping("pojo_param")
    public ModelAndView pojo(User users){
        ModelAndView mv = new ModelAndView();
        mv.addObject("users",users);
        mv.setViewName("ok");
        System.out.println("user->"+users);
        return mv;
    }

    @RequestMapping("pojo_list_param")
    public ModelAndView pojo_list(User2 user2){
        ModelAndView mv = new ModelAndView();
        mv.addObject("user2",user2);
        mv.setViewName("ok");
        System.out.println("user->"+user2);
        return mv;
    }

    @RequestMapping("pojo_map_param")
    public ModelAndView pojo_map(User3 user3){
        ModelAndView mv = new ModelAndView();
        mv.addObject("user3",user3);
        mv.setViewName("ok");
        System.out.println("user->"+user3);
        return mv;
    }
}

3个jsp页面
首页index.jsp

<%--
  Created by IntelliJ IDEA.
  User: miao
  Date: 2019/11/15
  Time: 21:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h4>你好</h4>
<a href="/user/drump">点击跳转</a>
</body>
</html>

表单jsp table.jsp

<%--
  Created by IntelliJ IDEA.
  User: miao
  Date: 2019/11/15
  Time: 21:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>表单</title>
</head>
<body>
<h4>基本类型请求参数的绑定</h4>
<form method="post" action="/user/base_param">
    用户id:<input type="text" name="id"><br>
    <input type="submit" value="基本类型提交">
</form>
<h4>---------------------------------------------------------------------------------</h4>
<br>

<h4>引用类型请求参数的绑定</h4>
<form method="post" action="/user/quote_param">
    用户名:  <input type="text" name="name"><br>
    用户密码:<input type="text" name="pwd"><br>
    <input type="submit" value="引用类型提交">
</form>
<h4>---------------------------------------------------------------------------------</h4>
<br>

<h4>对象类型请求参数的绑定</h4>
<form method="post" action="/user/pojo_param">
    用户id:  <input type="text" name="id"><br>
    用户名:  <input type="text" name="name"><br>
    用户密码:<input type="text" name="pwd"><br>
    <input type="submit" value="对象类型提交">
</form>
<h4>---------------------------------------------------------------------------------</h4>
<br>

<h4>对象类型(实体中包含list)请求参数的绑定</h4>
<form method="post" action="/user/pojo_list_param">
    用户id:  <input type="text" name="id"><br>
    用户名:  <input type="text" name="name"><br>
    用户密码:<input type="text" name="pwd"><br>
    角色id:  <input type="text" name="role2List[0].id"><br>
    角色名:  <input type="text" name="role2List[0].rolename"><br>
    <input type="submit" value="对象类型提交">
</form>
<h4>---------------------------------------------------------------------------------</h4>
<br>

<h4>对象类型(实体中包含map)请求参数的绑定</h4>
<form method="post" action="/user/pojo_map_param">
    用户id:  <input type="text" name="id"><br>
    用户名:  <input type="text" name="name"><br>
    用户密码:<input type="text" name="pwd"><br>
    角色1id:  <input type="text" name="role2Map['one'].id"><br>
    角色1名:  <input type="text" name="role2Map['one'].rolename"><br>
    角色2id:  <input type="text" name="role2Map['two'].id"><br>
    角色2名:  <input type="text" name="role2Map['two'].rolename"><br>
    <input type="submit" value="对象类型提交">
</form>
</body>
</html>

表单提交成功页面 ok.jsp

<%--
  Created by IntelliJ IDEA.
  User: miao
  Date: 2019/11/15
  Time: 21:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>成功页面</title>
</head>
<body>
<h4>基本类型请求参数的绑定--》成功</h4>
${id}传入成功
<h4>---------------------------------------------------------------------------------</h4>
<br>

<h4>引用类型请求参数的绑定--》成功</h4>
${name} ${pwd}传入成功
<h4>---------------------------------------------------------------------------------</h4>
<br>


<h4>对象类型请求参数的绑定--》成功</h4>
${user.id}   <br>
${user.name} <br>
${user.pwd}  <br>
传入成功
<h4>---------------------------------------------------------------------------------</h4>
<br>


<h4>对象类型-list集合请求参数的绑定--》成功</h4>
${user2.id}   <br>
${user2.name} <br>
${user2.pwd}  <br>
${user2.role2List[0].id}  <br>
${user2.role2List[0].rolename}  <br>
传入成功
<h4>---------------------------------------------------------------------------------</h4>
<br>


<h4>对象类型-map集合请求参数的绑定--》成功</h4>
${user3.id}   <br>
${user3.name} <br>
${user3.pwd}  <br>
${user3.role2Map["one"].id}  <br>
${user3.role2Map["one"].rolename}  <br>
${user3.role2Map["two"].id}  <br>
${user3.role2Map["two"].rolename}  <br>
传入成功
<h4>---------------------------------------------------------------------------------</h4>
<br>

</body>
</html>

代码链接(git网址)
https://github.com/cxpcxpcxp/csdn_case_springmvc_requestparam_bind

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值