文章目录
HelloController.java
java
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//@Controller:在类上添加该注解,指定该类为一个请求处理器,不需要实现任何接口或者继承任何类。
@Controller
public class HelloController {
//@RequestMapping:在方法上或者类上添加该注解,指定请求的`url`由该方法处理。
@RequestMapping("showView")
public String showView() {
return "hello";
}
@RequestMapping("showData")
@ResponseBody
public String showData() {
return "showData";
}
}
springmvc.xml
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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置处理器Bean的读取 -->
<!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->
<context:component-scan base-package="com.springmvc.controller"/>
<!-- 配置三大组件之处理器适配器和处理器映射器 -->
<!-- 内置了RequestMappingHandlerMapping和RequestMappingHandlerAdapter等组件注
册-->
<mvc:annotation-driven />
<!-- 配置三大组件之视图解析器 -->
<!-- InternalResourceViewResolver:默认支持JSP视图解析-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
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 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_2_5.xsd"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- 学习前置条件 -->
<!-- 问题1:web.xml中servelet、filter、listener、context-param加载顺序 -->
<!-- 问题2:load-on-startup标签的作用,影响了servlet对象创建的时机 -->
<!-- 问题3:url-pattern标签的配置方式有四种:/dispatcherServlet、 /servlet/* 、*
、/ ,以上四种配置,优先级是如何的-->
<!-- 问题4:url-pattern标签的配置为/*报错,原因是它拦截了JSP请求,但是又不能处理JSP请
求。为什么配置/就不拦截JSP请求,而配置/*,就会拦截JSP请求-->
<!-- 问题5:配置了springmvc去读取spring配置文件之后,就产生了spring父子容器的问题 -->
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置spring配置文件路径 -->
<!-- 如果不设置初始化参数,那么DispatcherServlet会读取默认路径下的配置文件 -->
<!-- 默认配置文件路径:/WEB-INF/springmvc-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 指定初始化时机,设置为2,表示Tomcat启动时,DispatcherServlet会跟随着初始化
-->
<!-- 如果不指定初始化时机,DispatcherServlet就会在第一次被请求的时候,才会初始化,
而且只会被初始化一次 -->
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- URL-PATTERN的设置 -->
<!-- 不要配置为/*,否则报错 -->
<!-- 通俗解释:/*,会拦截整个项目中的资源访问,包含JSP和静态资源的访问,对于静态资源
的访问springMVC提供了默认的Handler处理器 -->
<!-- 但是对于JSP来讲,springmvc没有提供默认的处理器,我们也没有手动编写对于的处理
器,此时按照springmvc的处理流程分析得知,它短路了 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <servlet> -->
<!-- <servlet-name>sss</servlet-name> -->
<!-- <servlet-class>sss</servlet-class> -->
<!-- </servlet> -->
<!-- <servlet-mapping> -->
<!-- <servlet-name>sss</servlet-name> -->
<!-- <url-pattern>/sss</url-pattern> -->
<!-- </servlet-mapping> -->
</web-app>
pom.xml
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>org.example</groupId>
<artifactId>springmvcdemo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvcdemo 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>
<!-- spring MVC依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvcdemo</finalName>
<plugins>
<!-- 配置Maven的JDK编译级别 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
hello.jsp
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello
</body>
</html>