SpringMVC 学习笔记(一) Hello World
springMVC概述:
Spring MVC 是眼下最主流的MVC 框架之中的一个
Spring MVC 通过一套 MVC 注解。让 POJO 成为处理请
求的控制器,而无须实现不论什么接口。
支持 REST 风格的 URL 请求
採用了松散耦合可插拔组件结构,比其它 MVC 框架更具
扩展性和灵活性
项目结构图:
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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ibigsea</groupId> <artifactId>springmvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring_version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring_version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring_version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-beans</artifactId> <version>${spring_version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring_version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring_version>4.1.2.RELEASE</spring_version> </properties> <build> <finalName>springmvc</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
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" version="3.0" > <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 载入spirng配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.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>
通过在web.xml配置DispatcherServlet前端控制器, web容器 启动时载入,而且进行初始化.
由于HttpServlet继承了HttpServlet 所以在web容器启动运行时将会调用 init() 方法
当中initServletBean() 是HttpServletBean 提供给子类扩展的
FrameworkServlet 继承了 HttpServletBean 实现了initServletBean()这种方法,对web上下文进行初始化 . 而且提供了onRefresh(wac) 方法给子类实现
DispatcherServlet继承了 FrameworkServlet 实现了onRefresh()这种方法
假设 web.xml中没有指定 contextConfigLocation . 则使用servlet-name 寻找xml文件.
HelloWorld.java
package com.ibigsea.springmvc.helloworld; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { /** * 配置@RequestMapping 拦截 localhost:8080/springmvc/hello 请求 * @return */ @RequestMapping("/hello") public String helloWorld(){ System.out.println("hello world"); return "helloworld"; } }
RequestMapping
详情请见第2章节
Spirng-mvc.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-4.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-4.1.xsd"> <!-- 配置自己主动扫描包 扫描com.ibigsea.springmvc 包下的类 --> <context:component-scan base-package="com.ibigsea.springmvc"/> <!-- 配置视图解析器 如返回helloworld 为 [/WEB-INF/pages/helloworld.jsp] --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/pages/"/> <!-- 后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>
当訪问 http://localhost:8080/springmvc/hello 后 DispatcherServlet 会拦截请求
InternalResourceViewResolver 继承了 UrlBasedViewResolver 终于通过buildView() 解析
重定向和转发
普通情况下,控制器方法返回字符串类型的值会被当成逻辑视图名处理
• 假设返回的字符串中带 forward: 或 redirect: 前缀时,SpringMVC 会对他们进行特殊处理:将 forward: 和redirect: 当成指示符,其后的字符串作为 URL 来处理
– redirect:helloworld.jsp:会完毕一个到 helloworld.jsp 的重定向的操作
– forward:helloworld.jsp:会完毕一个到 helloworld.jsp 的转发操作
在 UrlBasedViewResolver 类里面的 createView 方法 解析