分享

Spring MVC 入门指南(一):HelloWorld

 别无分店 2017-06-19

Spring MVC 为展现层提供了基于MVC的设计理念,它通过一套MVC注解,让普通的Java类即可成为处理请求的控制器(Servlet做的事情),而无需实现任何接口,也支持REST风格的URL请求。下面提供一个基于Spring MVC的HelloWorld的实例:

一、加入必需的 jar 包

(一)直接拷贝 jar 包的形式

1. spring-aop-x.x.x.jar
2. spring-beans-x.x.x.jar
3. spring-context-x.x.x.jar
4. spring-core-x.x.x.jar
5. spring-expression-x.x.x.jar
6. spring-web-x.x.x.jar
7. spring-webmvc-x.x.x.jar
8. commons-logging-x.x.x.jar

(二)使用Maven的方式

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.2.1.RELEASE</version>
</dependency>

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.1.RELEASE</version>
</dependency>

二、在web.xml文件中配置Spring的DispatcherServlet用来拦截请求

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www./2001/XMLSchema-instance"
	xmlns="http://java./xml/ns/javaee" xmlns:jsp="http://java./xml/ns/javaee/jsp"
	xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	
	<!-- 配置DispatcherServlet -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<!-- 配置DispatcherServlet的一个初始化参数,作用是配置SpringMVC配置文件的位置和名称 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- classpath表示类路径下的spring-mvc.xml文件 -->
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<!-- load-on-startup 表示在web应用启动时,即加载该DispatcherServlet,而不是等到首次请求再中载 -->
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<!-- 可以应答所有请求,也就是将所有的请求都交给Spring的DispatcherServlet来处理 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

三、添加Spring MVC的配置文件取名为 spring-mvc.xml,用来加载web应用中的Spring Bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www./schema/beans"
	xmlns:xsi="http://www./2001/XMLSchema-instance"
	xmlns:context="http://www./schema/context"
	xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd
		http://www./schema/context http://www./schema/context/spring-context-4.2.xsd">
	
	<!-- 自动扫描 cn.kolbe.spring.mvc 下的带有Spring组件注解的类 -->
	<context:component-scan base-package="cn.kolbe.spring.mvc"/>

	<!-- 配置视图解析器:将控制器方法返回值解析为实际物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 假设控制器返回 success 字符串,该视图解析器将自动进行装配,具体的视图将为:
			prefix + 返回的字符串 + suffix = /WEB-INF/views/success.jsp
		 -->
		<!-- prefix表示前缀 -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<!-- suffix表示后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

四、编写类HelloWorld,并通过添加注解 @Controller 将其标识为控制器,这里可以看出HelloWorld仅仅是一个普通的Java类,通过简单的注解,就可以将其解析为Servlet类来响应用户的请求

package cn.kolbe.spring.mvc.controller;

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

@Controller
public class HelloWorld {
	
	/**
	 * 1. 使用 @RequestMapping 注解来映射请求的URL(相当于web.xml中的servlet-mapping元素的url-pattern)
	 * 2. 返回值会通过视图解析器解析为实际的物理视图,具体看第三步中的InternalResourceViewResolver配置信息
	 */
	@RequestMapping("/helloworld")
	public String success() {
		return "success";
	}
}

五、编写视图层的页面,这里就两个简单的页面,一个是index.jsp页面,一个是success.jsp页面

(一)index.jsp页面,路径是 /index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC</title>
</head>
<body>
	<h1>
		<a href="helloworld">HelloWorld</a>
	</h1>
</body>
</html>

(二)success.jsp页面,路径是 /WEB-INF/views/success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC</title>
</head>
<body>
	<h1>Welcome</h1>
</body>
</html>

六、启动 web 应用,访问 index.jsp 页面,点击页面上的链接,可以看到,已经成功实现跳转


附:该 web 应用的结构







    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多