SpringMVC入门 (一) 搭建环境

SpringMVC学习使用总共分几个步骤:
1.搭建环境
2.如何完成Controller和Viewer的映射
3.如果把值传递给Controller
4.Controller如何把值传递给Viewer
5.异常处理
6.页面标签
7.文件上传
8.深入一下源代码


这里就一步步开始学习。

1. 首先导入Spring
这里使用Maven来导入spring框架包。
关于maven搭建webapp环境,参考 http://alleni123.iteye.com/admin/blogs/1983774

<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-context</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-core</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-beans</artifactId>
				<version>${spring.version}</version>
			</dependency>



			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-test</artifactId>
				<version>${spring.version}</version>
				<scope>test</scope>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-orm</artifactId>
				<version>${spring.version}</version>
			</dependency>
			
			
			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aop</artifactId>
				<version>${spring.version}</version>
			</dependency>

			<dependency>
				<groupId>org.springframework</groupId>
				<artifactId>spring-webmvc</artifactId>
				<version>${spring.version}</version>
			</dependency>

  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  	
  	<spring.version>3.2.2.RELEASE</spring.version>
  </properties>


2.在web.xml中配置SpringMVC的DispatcherServlet
这样SpringMVC就可以捕获到所有的用户请求,将请求转给内部机制处理。
 <servlet>
		<servlet-name>hello</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>hello</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


3.创建一个SpringMVC的xml文件,里面是关于SpringMVC的配置。
这里要特别注意这个xml文件的名称,打开Spring的reference文件,
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
找到 17.Web MVC FrameworkThe Dispatcher
可以看到如下:
Upon initialization of a DispatcherServlet, SpringMVC looks for a file named
[servlet-name]-servlet in the WEB-INF directory of your web application and creates the beans defined there, overriding the definition of any beans defined with the same name in the gloable scope.

即是说, 这个配置文件的名字必须是web.xml里面的<servlet-name>里面的名字加上"-servlet.xml".

因此这里就是 hello-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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
		
		
			<!-- 将静态文件制定到某个特殊的文件夹中统一处理 -->
		<mvc:resources location="/resources/" mapping="/resources/**"/>
		<!-- InternalResourceViewResolver是我们要使用的视图解析器,它是UrlBasedViewResolver的子类。支持JstlView。 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			
<!-- 这个JstlView类是用来专门支持视图的JSTL标签。 根据教程,在Spring3.0.5之后,这个类就不用手动引入了。 -->
			 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
                        <!-- 前缀表示我们的jsp文件所在路径 -->
			<property name="prefix" value="/WEB-INF/jsp/"/>
			<!-- 后缀表示我们的View文件的后缀,这里是.jsp -->
			<property name="suffix" value=".jsp"/>
		</bean>
		
		
		<!-- Enable autodetection of annotated controllers, here we add component scanning to our configuration 通过component-scan来开启自动检测标识控制器。 这里把我们放置controller的包告诉Spring-->
		
		<context:component-scan base-package="com.lj.controller"></context:component-scan>
		
<!--这个是用于在classpath下开启使用支持JSR303标准的validator-->
<mvc:annotation-driven/>
		
		</beans>




4. 创建Controller。
在包com.lj.controller下,创建LoginController

@Controller()
@RequestMapping("/hellomvc")
public class LoginController {
	

	
	//通过注释RequestMapping告诉Spring这是一个请求处理方法(handler method)
	//RequestMapping里面可以包含具体的对应用户请求的url地址。
	@RequestMapping({"/hello","/"})
	public String hello(){
		System.out.println("hello mvc");
		//这里返回的"hello"就是我们的逻辑视图名称(logic view),在这里就是/WEB-INF/jsp/hello.jsp.
		return "hello";
	}
	
}


5.创建逻辑视图文件
在WEB-INF/jsp下创建hello.jsp


6.启动web容器
输入地址ContextPath+hellomvc/hello.
启动顺利的话,就能跳到WEB-INF/jsp/hello.jsp页面。

猜你喜欢

转载自alleni123.iteye.com/blog/1985571