---
title: "Spring MVC 练习001"
author: "wuyechun"
date: "2012-10-23T15:12:41Z"
tags: []
categories: ["编程语言"]
slug: "spring-mvc-001"
draft: false
lastmod: "2012-10-23T15:14:07Z"
id: 1703725
url: "https://wuyechun.iteye.com/blog/1703725"
---

**本文讲述SpringMVC开发环境快速搭建以及值传递的几种方式,**

**只包含操作，不包含讲解。**

 

**1.New-Dynamic Web Project-springmvc**

**2.WEB-INF路径下添加依赖包：**

   1）commons-logging-1.1.1.jar

   2）org.springframework.asm-3.1.1.RELEASE.jar

   3)org.springframework.beans-3.1.1.RELEASE.jar

   4)org.springframework.context-3.1.1.RELEASE.jar

   5)org.springframework.core-3.1.1.RELEASE.jar

   6)org.springframework.expression-3.1.1.RELEASE.jar

   7)org.springframework.web-3.1.1.RELEASE.jar

   8)org.springframework.web.servlet-3.1.1.RELEASE.jar

**3.修改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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!--如果不配置 init-param,会默认到 WEB-INF 路径下 DispatcherServlet-servlet.xml 文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/DispatcherServlet-servlet.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>characterEncoding</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> </filter> -->
</web-app>
```

 

**4.WEB-INF下添加配置文件 DispatcherServlet-servlet.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<mvc:annotation-driven />

	<!-- 搜索的控制类路径(C) -->
	<context:component-scan base-package="com.wuyechun.springmvc.controller"></context:component-scan>

	<!-- 配置视图路径(V),说明逻辑视图转换成物理视图的前缀和后缀. -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
```

 

**5. 在src下新建 com.wuyechun.springmvc.controller 包，并在包中新建类为HelloController.java**

```
package com.wuyechun.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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

/***
 * @author wuyechun
 * 演示了springmvc中传递参数的方法
 *
 */

@Controller
@RequestMapping("/welcome")  
public class HelloController {
	
	
	/***
	 * http://localhost:8080/springmvc/
	 * http://localhost:8080/springmvc/welcome/
	 * http://localhost:8080/springmvc/welcome/?name=wuyechun
	 * 这些请求方式都是可以的 
	 * 
	 * respMap 为返回值。
	 */
	@RequestMapping(value = { "/", "/hello" })
	public String sayHello(String name,Map<String,Object> respMap){
		String helloStr="Hello,"+name;
		respMap.put("helloStr",helloStr);
		return "welcome";
	}
	
	/**
	 * http://localhost:8080/springmvc/welcome/sayok  这种请求时不允许的
	 * http://localhost:8080/springmvc/welcome/sayok?name=wuyechun 
	 *  添加了 @RequestParam，必须要加上参数。
	 */
	@RequestMapping(value = "/sayok")
	public String sayOk(@RequestParam String name, Model resModel) {
	
		String helloStr="Hello,"+name;
		resModel.addAttribute("helloStr", helloStr);
		// 使用Object的类型作为key,String-->string,即jsp中通过 ${string} 获取。
		resModel.addAttribute(",Are you ok?");
		return "welcome";
	}

	/*
	 * http://localhost:8080/springmvc/welcome/req?user=wuyechun
	 * 获取HttpServletRequest，从中获取传递的参数。
	 */
	@RequestMapping("/req")
	public String req(HttpServletRequest req,Model resModel) {
		String name=req.getParameter("user");
		String helloStr="Hello,"+name;
		resModel.addAttribute("helloStr", helloStr);
		return "welcome";
	}
	

}
```

 **6. 在Tomcat下运行**

    浏览器地址栏分别输入：

```
 http://localhost:8080/springmvc/
 http://localhost:8080/springmvc/welcome/
 http://localhost:8080/springmvc/welcome/?name=wuyechun

 http://localhost:8080/springmvc/welcome/sayok
 http://localhost:8080/springmvc/welcome/sayok?name=wuyechun 

http://localhost:8080/springmvc/welcome/req?user=wuyechun
```

        观察了解spring mvc中值的传递方法。