(一)SpringMvc简介以及第一个springmvc工程
一、SpringMVC是什么?
- springmvc是Spring的一个模块,提供web层解决方案(就与MVC设计架构)
- 如上图,
DispatcherServlet:前端控制器,由SpringMVC提供。HandlerMapping:处理器映射器,由SpringMVC提供。用于解析url,将该url映射到某一个Controller上。HandlerAdapter:处理器适配器,由SpringMVC提供。用于将url映射到某个Controller的某个方法上。Handler:处理器,需要程序员开发。ViewResolver:视图解析器,由SpringMVC提供。view:真正的视图页面,由程序员编写。
二、第一个springmvc项目
2.1 下载springmvc需要用到的包依赖
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.shyroke</groupId> <artifactId>firstSpringMvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>firstSpringMvc Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.0.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>firstSpringMvc</finalName> </build> </project>
2.2 在web.xml中配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载SpringMVC配置 --> <init-param> <!-- 配置文件的位置 --> <!-- 如果不配置contextConfigLocation, 默认查找的配置文件名称classpath下的: servlet名称+"servlet.xml"即springmvc-servlet.xml --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 可以配置/, 此工程 所有请求全部由springmvc解析,此种方式可以实现 RESTful方式,需要特殊处理对静态文本的解析不能由springmvc解析 可以配置*.do或*.action, 所有请求的url扩展名为.do或.action由springmvc解析,此种方法 不可以使用/*,如果配置/*,返回的jsp也由springmvc解析,这是不对的 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
3.3 创建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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 激活@Required @Autowired @Resource等标注--> <context:annotation-config></context:annotation-config> <!-- DispatcherServlet上下文,扫描base-package包中的类,并自动加载到spring容器中 --> <context:component-scan base-package="com.shyrolk.controller"> </context:component-scan> <!-- 启用@Component,@Controller,@Service,@Repository注解驱动 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
- <context:component-scan/>标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。
- <mvc:annotation-driven/>是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。
3.4 编写前端页面,设置跳转url
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <% String path=request.getContextPath(); %> </head> <body> <a href="<%=path%>/userAction/login.action">点我跳转</a> </body> </html>
3.5 编写Controller层
UserController.java
package com.shyrolk.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value="/userAction") public class UserController { @RequestMapping(value="/login.action") public String login(){ System.out.println("用户登录"); return "success"; } }
- 在springmvc.xml文件中我们配置了<context:component-scan base-package="com.shyrolk.controller"> </context:component-scan> 所以上述代码的类即UserController类必须位于com.shyrolk.controller包里,这样才会被spirng扫描到并注册到bean中
3.6 编写跳转之后的页面
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 登录成功!!! </body> </html>
- 在springmvc.xml文件中我们配置了:
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>
所以该 success.jsp 必须位于/WEB-INF/views/ 目录下
3.7 测试