SpringMVC传值的五种方式

搭建SpringMVC的环境

1.把SpringMVC的jar包导入WebContent→WEB_INF的lib文件夹下。

springmvc传递date springmvc传值_mvc


2.在WebContent→WEB_INF文件夹下配置web.xml文件。(名字固定,必须为web.xml)。

springmvc传递date springmvc传值_mvc_02


web.xml文件里的具体内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<servlet>
	<!—<servlet>的<servlet-name>必须与<servlet-mapping>里的<servlet-name>相同-->
		<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-servlet.xml</param-value><!—参数值-->
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern> <!-- 把<url-pattern>元素的值改为 / ,表示要拦截所有的请求,并交由Spring MVC的后台控制器来处理 -->
	</servlet-mapping>
</web-app>

3.在src 的下面写springmvc的配置文件,文件格式为xml(命名自由,可为springmvc.xml,springMvc-servlet.xml,spring-servlet.xml等)。

springmvc传递date springmvc传值_mvc_03

<?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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
	<context:component-scan
		base-package="com.ch.springMvc"></context:component-scan><!—扫描的包的路径-->
	<mvc:annotation-driven></mvc:annotation-driven>
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="internalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

<!—如果在web.xml中servlet-mapping的url-pattern设置的是/,而不是如.do。表示将所有的文件,包含静态资源文件都交给spring mvc处理。就需要用到<mvc:annotation-driven />了。如果不加,DispatcherServlet则无法区分请求是资源文件还是mvc的注解,而导致controller的请求报404错误。–>
<!—Spring 3.0.x中使用了mvc:annotation-driven后,默认会帮我们注册默认处理请求,参数和返回值的类,其中最主要的两个类:DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter ,分别为HandlerMapping的实现类和HandlerAdapter的实现类,从3.1.x版本开始对应实现类改为了RequestMappingHandlerMapping和RequestMappingHandlerAdapter。HandlerMapping的实现类的作用
实现类RequestMappingHandlerMapping,它会处理@RequestMapping 注解,并将其注册到请求映射表中。
HandlerAdapter的实现类的作用
实现类RequestMappingHandlerAdapter,则是处理请求的适配器,确定调用哪个类的哪个方法,并且构造方法参数,返回值。
当配置了mvc:annotation-driven/后,Spring就知道了我们启用注解驱动。然后Spring通过context:component-scan/标签的配置,会自动为我们将扫描到的@Component,@Controller,@Service,@Repository等注解标记的组件注册到工厂中,来处理我们的请求。–>

4.写实体类

springmvc传递date springmvc传值_mvc_04

`

package com.ch.springMvc.bean;

import javax.validation.constraints.NotBlank;

public class User {
	//私有化定义的属性
	@NotBlank(message="name is not blank")
	private String name;
	private String password;
	
	//使用公有化方法get私有的属性
	public String getName() {
		return name;
	}
	//使用公有化方法set私有的属性
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

5.在WebContent→WEB_INF文件夹下新建文件夹jsp,在jsp文件夹新建jsp文件。

springmvc传递date springmvc传值_spring_05

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello world <!--跳转页面中显示的内容  -->
</body>
</html>

6.写控制类

springmvc传递date springmvc传值_java_06

package com.ch.springMvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ch.springMvc.bean.User;

@Controller //本类是一个控制器
public class HelloController {

	//@RequestMapping()里要写执行的方法名
	@RequestMapping("/hello")
	public void hello(String name,String password) {
		System.out.println(name);
		System.out.println(password);
		System.out.println("hello");
	}
	
 //@RequestMapping()里要写执行的方法名
	@RequestMapping("/hello2") 
	public String hello2(String name,String password) {
		System.out.println(name);
		System.out.println(password);
		System.out.println("perfect");
		return "ok"; 
}

 //实体类形式传参
	@RequestMapping("/hello3")
	public String hello3(User user) {
		System.out.println(user.getName());
		System.out.println(user.getPassword());
		System.out.println("hello3");
		return "hello3";
	}
	
    //restful形式
	//@RequestMapping()里要写"{参数名1}/{参数名2}/方法名"
	@RequestMapping("{name}/{password}/hello4")
	public String hello4(@PathVariable("name") String name,@PathVariable("password") String password) {
		System.out.println(name);
		System.out.println(password);
		System.out.println("hello4");
		return "hello4";
	}
	
//放到request作用域,jsp用el表达式拿数据:${key}
	@RequestMapping("hello5")
	public String hello5(HttpServletRequest request) {
		request.setAttribute("name", "hehe");
		request.setAttribute("password", "qwert");
		return "ok";
	}
}
// jsp页面用el表达式拿数据:${key}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
it is ok
<!-- EL表达式 -->
${name}
${password}
</body>
</html>

7.实现的结果

//@RequestMapping("/hello")
地址栏上输入:
“http://localhost:8080/springmvc0325/hello.do? name=zs&password=20”
后台控制台上会出现:
zs
20
Hello
前端页面会自动调转到return后面的hello .jsp页面

//@RequestMapping("/hello2")
地址栏上输入:
“http://localhost:8080/springmvc0325/hello2.do?name=yiyi&password=189”
后台控制台上会出现:
yiyi
189
perfect
前端页面会自动调转到return后面的ok .jsp页面:
it is ok

//@RequestMapping("/hello3")
地址栏上输入:
“http://localhost:8080/springmvc0325/hello3.do?name=yoyo&password=456”
后台控制台上会出现:
yoyo
456
hello3
前端页面会自动调转到return后面的hello3 .jsp页面:
hello 3

//@RequestMapping("{name}/{password}/hello4")
地址栏上输入:
“http://localhost:8080/springmvc0325/koko/33/hello4.do”
后台控制台上会出现:
koko
33
hello4
前端页面会自动调转到return后面的hello4 .jsp页面:
hello 4

//@RequestMapping(“hello5”)
地址栏上输入:
“http://localhost:8080/springmvc0325/hello5.do”
enter输入后,
前端页面会自动调转到return后面的ok .jsp页面的内容+方法里写的具体的参数
it is ok hehe qwert