学习大数据——Spring Web MVC及实现SpringMVC的HelloWorld

SpringMVC 概述

  1. 一种轻量级的、基于MVC的Web层应用框架。偏前端而不是基于业务逻辑层。Spring框架的一个后续产品。
  2. Spring 为展现层提供的基于 MVC 设计理念的优秀的 Web 框架,是目前最主流的MVC 框架之一。
  3. Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口。

SpringMVC的HelloWorld实例

1) 创建Maven版的Web工程
2) 在Spring时导入的jar包依赖的基础上,加入对web相关jar包的依赖

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.learn.ss</groupId>
	<artifactId>SpringMVC</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<!-- context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<!-- webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>

	</dependencies>
</project>

Tips: 实际需要加入spring-web与spring-webmvc的jar包,因为spring-webmvc 依赖了spring-web, Maven会自动维护此依赖,因此只需加入对spring-webmvc的依赖.

3) 在web.xml中配置DispatcherServlet

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 配置前端控制器:DispatcherServlet
			快捷键:alt + / 选中倒数第二项
	-->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 配置SpringMVC配置文件的路径 -->
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<!-- 配置映射的请求地址:"/" -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

4) 加入SpringMVC的配置文件 springmvc.xml

springmvc.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"
	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-4.0.xsd">

	<!-- 设置自动扫描的包,别忘了添加命名空间context -->
	<context:component-scan base-package="com.learn.springmvc"></context:component-scan>
	
	<!-- 配置视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置前缀 -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<!-- 配置后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

5) 创建一个入口页面,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>Insert title here</title>
</head>
<body>
	<a href="${pageContext.request.contextPath }/hello">Hello SpringMVC</a>
</body>
</html>

6) 编写请求处理器

HelloWorld.java:

package com.learn.springmvc.helloworld;

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

//@RequestMapping注解可以添加到类上,也可以添加到方法上
//@RequestMapping("HelloWorld")
@Controller
public class HelloWorld {
	/*
	 * 方法的返回值会被SpringMVC配置文件中配置的InternalResourceViewResolver这个视图解析为真是的物理视图,
	 *  然后自动进行请求转发
	 *  真是的物理视图 = 前缀 + 方法返回值 + 后缀
	 *  即:/WEB-INF/views/success.jsp
	 */
	@RequestMapping("/hello")
	public String testHelloWorld() {
		System.out.println("Hello SpringMVC!");
		return "success";
	}
}

7) 编写视图页面
在/WEB-INF/views下新建success.jsp页面

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>成功页面!</h1>
</body>
</html>

8) 部署测试
演示页面1
演示页面2
9) 流程解析
流程解析
项目结构:
项目结构

发布了37 篇原创文章 · 获赞 7 · 访问量 678

猜你喜欢

转载自blog.csdn.net/qq_40394792/article/details/104398100