SpringMVC系列三之传参数


本节介绍前台html页面向后台java程序传递参数的三种方法

同名传参

同名传参是指前台页面中传递的参数名称和后台用来接受这个值的参数名称是一样的 。具体操作如下。适用的情况是当前台要传递的参数个数较少时。

工程目录

在这里插入图片描述

项目创建过程

  1. 创建动态网页工程(Dynamic web project)
  2. 将所需的jar包复制到lib目录下
  3. 编写web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVC05</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- SpringMVC的servlet,用来处理各种业务请求 -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
    
  <!--修改请求方式,用于put和delete提交  -->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
  1. 编写spring配置文件。在Java Resource下创建名为conf的source floder
    在conf内创建一个xml文件,命名为SpringMVC.xml(与web.xml中init-param中指定的名称一致)
<?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 
        http://www.springframework.org/schema/context/spring-context.xsd">
     
     <!--1、配置为找到注解需要扫描的包-->   
	<context:component-scan base-package="com.edu.tjdz.geng"></context:component-scan>
	
	<!--2、配置视图管理器,用于和controller中方法的返回值进行前后缀拼接,构成返回页面的路径-->   
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver ">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
  1. 编写index.jsp。
    在测试页面中,<a href=“student?id=20180101&name=A” 这个标签,href属性就包括了前台参数的形式。更为通常的形式是:ip:端口号/项目名?参数名1=具体值&参数名2=具体值…
    即在请求路径后使用问号来拼接若干个参数,参数与参数之间使用&。
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试页</title>
</head>
<body>
	<a href="student?id=20180101&name=A">查询</a> 
	<br/>
	<br/>
	<form action="student" method="post">
		<input type="submit" value="添加">
	</form>
	<br/>
	<form action="student" method="post">
		<input type="hidden" name="_method" value="put">
		<input type="submit" value="修改">
	</form>
	<br/>
	<form action="student" method="post">
		<input type="hidden" name="_method" value="delete">
		<input type="submit" value="删除">
	</form>
</body>
</html>
  1. 创建返回视图界面
    在WEB-INF下创建view文件夹,在该文件夹下创建4个返回页面:addSuccess.jsp、deleteSuccess.jsp、getSuccess.jsp、updateSuccess.jsp

addSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加结果</title>
</head>
<body>
	<h>添加成功</h>
</body>
</html>

deleteSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除结果</title>
</head>
<body>
	<h>删除成功</h>
</body>
</html>

getSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询结果</title>
</head>
<body>
	<h>查询成功</h>
</body>
</html>

updateSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新结果</title>
</head>
<body>
	<h>更新成功</h>
</body>
</html>
  1. 创建com.edu.tjdz.geng.controller包,编写cotroller,接受前端参数
package com.edu.tjdz.geng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class MyController {
	
	/* 方法一:同名传递
	*  观察get函数列表中的参数个数和名称与index.jsp中查询链接的href
	* 可以发现两者参数名称一模一样
	*/
	@RequestMapping(value="/student", method = RequestMethod.GET )
	public String get(int id, String name) {
		System.out.println("查询学生id为:" + id + ",姓名为: " + name);
		return "getSuccess";
	}
	
	@RequestMapping(value="/student", method = RequestMethod.POST )
	public String add() {
		System.out.println("添加学生信息:");
		return "addSuccess";
	}
	
	@RequestMapping(value="/student", method = RequestMethod.PUT)
	public String update() {
		System.out.println("更新学生");
		return "updateSuccess";
	}
	
	@RequestMapping(value="/student", method = RequestMethod.DELETE)
	public String delete() {
		System.out.println("删除学生");
		return "deleteSuccess";
	}	
}
  1. 部署项目,访问localhost:8080/SpringMVC04,点击查询链接,观察打印信息

注解传参

注解传参是指使用注解来为后台处理函数的某个参数指定它所负责接受的是前台传过来的哪一个参数。适用的情况是当前台要传递的参数个数较少且名字和后台的参数名字不一致时。

项目创建过程

  1. 修改controller类
package com.edu.tjdz.geng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class MyController {
	
	/* 方法二:注解传递
	*  使用@RequestParam注解,该注解位置放置在函数参数列表中的参数前
	* 注解有三个常用属性,name属性指定前台参数名称,required指定是否请求url必须携带该参数
	* defaultValue为设置默认值,它通常用于数值类型,和 required = false搭配使用
	* 此例中,get函数的stuId被注解指定接收前台参数id的参数值,stuName被注解指定接收前台参数name的参数值
	*/
	@RequestMapping(value="/student", method = RequestMethod.GET )
	public String get(@RequestParam(name = "id", required = false, defaultValue = "0")int stuId, @RequestParam(name = "name", required = false)String stuName) {
		System.out.println("查询学生id为:" + stuId + ",姓名为: " + stuName);
		return "getSuccess";
	}
	/* 方法一:同名传递
	*  观察get函数列表中的参数个数和名称与index.jsp中查询链接的href
	* 可以发现两者参数名称一模一样
	*/
	/*
	@RequestMapping(value="/student", method = RequestMethod.GET )
	public String get(int id, String name) {
		System.out.println("查询学生id为:" + id + ",姓名为: " + name);
		return "getSuccess";
	}*/
	
	@RequestMapping(value="/student", method = RequestMethod.POST )
	public String add() {
		System.out.println("添加学生信息:");
		return "addSuccess";
	}
	
	@RequestMapping(value="/student", method = RequestMethod.PUT)
	public String update() {
		System.out.println("更新学生");
		return "updateSuccess";
	}
	
	@RequestMapping(value="/student", method = RequestMethod.DELETE)
	public String delete() {
		System.out.println("删除学生");
		return "deleteSuccess";
	}	
}
  1. 部署项目,访问localhost:8080/SpringMVC04,点击查询链接,观察打印信息

POJO传参

POJO传参是指使用JAVA的实体类对象接受前台页面传递过来的参数。适用的情况是当前台要传递的参数个数较多时,例如添加或创建页面,一个表单中有好多信息。

项目创建过程

  1. 修改测试页面
    form是一种更常用的向后台传递参数的方式,form表单中的input或其他控件用来填写参数。在传递时,input的name属性作为参数的参数名,input中输入的值作为对应参数名的具体值。
<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试页</title>
</head>
<body>
   <a href="student?id=20180101&name=A">查询</a> 
   <br/>
   <br/>
   <form action="student" method="post">
   	学校:<input type="text" name="school" /><br/>
   	系部:<input type="text" name="department" /><br/>
   	专业:<input type="text" name="speciality" /><br/>
   	班级:<input type="text" name="className" /><br/>
   	姓名:<input type="text" name="name" /><br/>
   	性别:<input type="text" name="gender" /><br/>
   	年龄:<input type="text" name="age" /><br/>
   	
   	<input type="submit" value="添加">
   </form>
   <br/>
   <form action="student" method="post">
   	<input type="hidden" name="_method" value="put">
   	<input type="submit" value="修改">
   </form>
   <br/>
   <form action="student" method="post">
   	<input type="hidden" name="_method" value="delete">
   	<input type="submit" value="删除">
   </form>
</body>
</html>
  1. 修改controller类
package com.edu.tjdz.geng.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.edu.tjdz.geng.model.Student;

@Controller
public class MyController {

   /* 方法三:POJO传递
   *  使用Student对象来进行统一的接收
   * 类比于mybatis中,java实体类接收数据库信息的方式
   */
   @RequestMapping(value="/student", method = RequestMethod.POST )
   public String add(Student s) {
   	System.out.println("添加学生信息:" + s);
   	return "addSuccess";
   }

   /* 方法二:注解传递
   	*  使用@RequestParam注解,该注解位置放置在函数参数列表中的参数前
   	* 注解有三个常用属性,name属性指定前台参数名称,required指定是否请求url必须携带该参数
   	* defaultValue为设置默认值,它通常用于数值类型,和 required = false搭配使用
   	* 此例中,get函数的stuId被注解指定接收前台参数id的参数值,stuName被注解指定接收前台参数name的参数值
   	*/
   	@RequestMapping(value="/student", method = RequestMethod.GET )
   	public String get(@RequestParam(name = "id", required = false, defaultValue = "0")int stuId, @RequestParam(name = "name", required = false)String stuName) {
   		System.out.println("查询学生id为:" + stuId + ",姓名为: " + stuName);
   		return "getSuccess";
   	}
   	/* 方法一:同名传递
   	*  观察get函数列表中的参数个数和名称与index.jsp中查询链接的href
   	* 可以发现两者参数名称一模一样
   	*/
   	/*
   	@RequestMapping(value="/student", method = RequestMethod.GET )
   	public String get(int id, String name) {
   		System.out.println("查询学生id为:" + id + ",姓名为: " + name);
   		return "getSuccess";
   	}*/
   
   @RequestMapping(value="/student", method = RequestMethod.PUT)
   public String update() {
   	System.out.println("更新学生");
   	return "updateSuccess";
   }
   
   @RequestMapping(value="/student", method = RequestMethod.DELETE)
   public String delete() {
   	System.out.println("删除学生");
   	return "deleteSuccess";
   }	
}
  1. 创建com.edu.tjdz.geng.model包,编写Student类
    属性名与form表单中各个input的name属性值保持一致。
package com.edu.tjdz.geng.model;

public class Student {
	private String school;
	private String department;
	private String speciality;
	private String className;
	private String name;
	private String gender;
	private int age;
	public String getSchool() {
		return school;
	}
	public void setSchool(String school) {
		this.school = school;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public String getSpeciality() {
		return speciality;
	}
	public void setSpeciality(String speciality) {
		this.speciality = speciality;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [school=" + school + ", department=" + department + ", speciality=" + speciality
				+ ", className=" + className + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";
	}
}
  1. 部署项目,访问localhost:8080/SpringMVC04,填写input,点击添加按钮,观察打印信息‘
    在控制台中我们发现,打印的信息存在乱码,这是由于解析中文的编码方式不合适导致的
  2. 在web.xml中配置解析中文的过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVC05</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- SpringMVC的servlet,用来处理各种业务请求 -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</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>
  
  <!--修改请求方式,用于put和delete提交  -->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
  1. 部署项目,访问localhost:8080/SpringMVC04,填写input,点击添加按钮,观察打印信息
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值