springmvc----参数绑定

springmvc学习记录第二篇----参数绑定


记录下来可以让自己记得更牢,忘了也可以再看看。

springmvc参数绑定

1 类似servlet的直接用request接收参数

2 简单类型

3 pojo类型

4  数组类型

5 map类型

6 list类型

大概要说的是以上五种类型的参数绑定。

进入正题

准备工作:

所有工作是在第一篇springmvc框架搭建的基础上进行。

1 request接收参数

用HttpServletRequest接收参数,需要servlet-api jar包

在pom.xml中添加即可

<dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>

以下为对应的测试类,方法

package com.soft.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class TestParamController {

    @RequestMapping(value="/testParam01")
    public ModelAndView testParam01(HttpServletRequest request){
        // 通过HttpServletRequest进行参数绑定。
        String requestParam = request.getParameter("name");
        ModelAndView model = new ModelAndView();
        model.addObject("name",requestParam);
        model.setViewName("test/testParam");
        return model;
    }
}

testParam.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>

${name}
</body>
</html>
输入请求进行测试

http://localhost:8080/testParam01?name=testParam

测试结果

idea  debug结果


页面显示


通过HttpServletRequest参数绑定完毕。

2 简单类型参数绑定

2..1 传入参数名跟方法参数列表名一致,就可以进行绑定

java代码

   /**
     * 简单类型参数绑定,只需要页面传入参数名跟参数列表中的名称一致就可以绑定成功
     * @param paramInt   int类型参数
     * @param paramStr  String类型参数
     * @param paramChar  char类型参数
     * @return ModelAndView
     */
    @RequestMapping(value="/testParam02")
    public ModelAndView testParam02(int paramInt,String paramStr,char paramChar){
        ModelAndView model = new ModelAndView();
        // 将传入绑定成功的参数在页面显示。
        model.addObject("paramInt",paramInt);
        model.addObject("paramStr",paramStr);
        model.addObject("paramChar",paramChar);
        model.setViewName("test/testParam");
        return model;
    }
jsp页面接收参数代码

int类型参数${paramInt}<br/>
String类型参数${paramStr}<br/>
char类型参数${paramChar}<br/>

测试结果:

idea dubug结果


页面显示结果:


测试链接:http://localhost:8080/testParam02?paramInt=1&paramStr=testStrParam&paramChar=A

简单参数绑定测试完毕。关于测试链接,如果不想用这么长的链接,可以自己用表单传递。效果一样。

3 pojo类型

要点:pojo的属性名跟传入参数名一致就可以绑定。

3.1先定义一个pojo用户参数绑定。

package com.soft.po;

/**
 * Created by xuweiwei on 2017/8/19.
 */
public class ParamVo {

    private String name;
    private String password;

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
测试pojo参数绑定方法。
    /**
     * pojo参数绑定,只需要页面传入参数名跟pojo中的属性名一致就可以绑定成功
     * @return ModelAndView
     */
    @RequestMapping(value="/testParam03")
    public ModelAndView testParam03(ParamVo paramVo){
        ModelAndView model = new ModelAndView();
        // 将传入绑定成功的参数在页面显示。
        model.addObject("paramVo",paramVo);
        model.setViewName("test/testParam");
        return model;
    }
jsp页面显示信息
<%-- pojo绑定参数显示 --%>
ParamVo name  :${paramVo.name}<br/>
ParamVo password  :${paramVo.password}<br/>
测试参数地址:http://localhost:8080/testParam03?name=tom&password=123456

测试结果:

idea debug结果:



浏览器显示结果:


4 数组类型参数绑定。

要点:页面要绑定的属性的name属性,跟方法参数名一样。

例如:

<form action="${pageContext.request.contextPath }/testParam04">
    <input type="text" name="arr" value="123"><br/>
    <input type="text" name="arr" value="321"><br/>
    <input type="text" name="arr" value="456"><br/>
    <input type="text" name="arr" value="567"><br/>
    <input type="submit" value="提交">
</form>
上面的name属性为arr,则接收参数的属性名为arr才能接收到页面参数。

    /**
     * 数组参数绑定:页面有一组name属性为arr。
     * @return ModelAndView
     */
    @RequestMapping(value="/testParam04")
    public ModelAndView testParam04(String[] arr){
        for(String str:arr){
            System.out.println(str);
        }
        ModelAndView model = new ModelAndView();
        // 将传入绑定成功的参数在页面显示。
        model.addObject("arr",arr);
        model.setViewName("test/testParam");
        return model;
    }
注意上面的参数跟jsp form中input 的name属性必须对上才能绑定成功

绑定成功后页面回显。jsp显示用

<%-- 数组参数绑定后页面显示结果 --%>
<c:forEach items="${arr}" var="str">
    ${str}<br/>
</c:forEach>


jsp页面输入参数


点击提交后,进入后台action。

idea  debug


可以看到数组参数已经传进来了。

然后页面显示。


好了数组参数绑定完毕。要点就是,页面name属性必须跟后台接收参数的数组的名称一致。


5 map参数绑定。

要点:map是一组组键值对。即  key/value。

map参数绑定,页面name属性要与map的可以对上就可以绑定。

页面属性写法:

<form action="${pageContext.request.contextPath }/testParam05" method="post">
    姓名:<input type="text" name="mapParam['name']"><br/>
    年龄:<input type="text" name="mapParam['age']" ><br/>
    班级:<input type="text" name="mapParam['class']" ><br/>
    分数:<input type="text" name="mapParam['grade']" ><br/>
    <input type="submit" value="提交">
</form>
主要看name属性的写法。

mapParam对应vo中的map属性名,['name']即对应着map中的一个name key,

vo

package com.soft.po;

import java.util.Map;

/**
 * Created by xuweiwei on 2017/8/19.
 */
public class ParamVo {

    private String name;
    private String password;

    public Map<String, Object> getMapParam() {
        return mapParam;
    }

    public void setMapParam(Map<String, Object> mapParam) {
        this.mapParam = mapParam;
    }

    Map<String,Object> mapParam;

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
action接收map参数

    /**
     * map参数绑定
     * @return ModelAndView
     */
    @RequestMapping(value="/testParam05")
    public ModelAndView testParam05(ParamVo paramVo){
        for(String str : paramVo.getMapParam().keySet()){
            System.out.println(paramVo.getMapParam().get(str));
        }
        ModelAndView model = new ModelAndView();
        // 将传入绑定成功的参数在页面显示。
        model.addObject("mapParam",paramVo.getMapParam());
        model.setViewName("test/testParam");
        return model;
    }
页面显示map代码:

<%-- 显示map数据 --%>
<c:forEach items="${mapParam}" var="mapPar">
    ${mapPar.key}:${mapPar.value}<br/>
</c:forEach>
运行:

点击提交进入后台。

运行完后页面展示:



好了map参数绑定完毕。主要注意写法问题。

6 list参数绑定。

 jsp   list参数绑定写法,因为list本身也是个数组,所以写法跟数组显示。

<form action="${pageContext.request.contextPath }/testParam06" method="post">
    <c:forEach items="${students}" var="student" varStatus="sta">
        姓名:<input type="text" name="students[${sta.index}].name"><br/>
    </c:forEach>
    <input type="submit" value="提交">
</form>
需要注意的是name属性的写法。

students对应vo属性[${sta.index}]对应list的下标,.name对应属性。

vo定义

package com.soft.po;

import java.util.List;
import java.util.Map;

/**
 * Created by xuweiwei on 2017/8/19.
 */
public class ParamVo {

    private String name;
    private String password;

    List<Student> students;

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public Map<String, Object> getMapParam() {
        return mapParam;
    }

    public void setMapParam(Map<String, Object> mapParam) {
        this.mapParam = mapParam;
    }

    Map<String,Object> mapParam;

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
Student  vo

package com.soft.po;

/**
 * Created by xuweiwei on 2017/8/19.
 */
public class Student {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
跳转到list参数绑定页面 action

    /**
     *
     * @return ModelAndView
     */
    @RequestMapping(value="/toListJsp")
    public ModelAndView toListJsp(){
        List<Student> students = new ArrayList<Student>();
        Student student = new Student();
        student.setName("xiaoming");
        Student student1 = new Student();
        student1.setName("xiaohong");
        Student student3 = new Student();
        student3.setName("xiaolan");
        students.add(student);
        students.add(student1);
        students.add(student3);
        ModelAndView model = new ModelAndView();
        model.addObject("students",students);
        model.setViewName("test/testListParam");
        return model;
    }
list参数绑定定义页面代码:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/testParam06" method="post">
    <c:forEach items="${students}" var="student" varStatus="sta">
        姓名:<input type="text" name="students[${sta.index}].name"><br/>
    </c:forEach>
    <input type="submit" value="提交">
</form>
</body>
</html>
获取参数绑定action

    /**
     * map参数绑定
     * @return ModelAndView
     */
    @RequestMapping(value="/testParam06")
    public ModelAndView testParam06(ParamVo paramVo){
        ModelAndView model = new ModelAndView();
        // 将传入绑定成功的参数在页面显示。
        model.addObject("listParam",paramVo.getStudents());
        model.setViewName("test/testParam");
        return model;
    }
显示list数据jsp代码

<%-- 显示list数据 --%>
<c:forEach items="${listParam}" var="mapPar">
${mapPar.name}<br/>
</c:forEach>
页面操作流程及结果:

提交到后台后:

后台执行完毕,前台显示:

到此list参数绑定完毕。主要就是name属性的写法以及跟vo参数的属性名对应。

参数绑定到此告一段落。

弄这些也挺不容易。慢慢提高,慢慢进步。

下一步,传递json数据,以及源码分析。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值