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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章