SpringMVC入门项目
依赖
<properties>
<spring>5.2.13.RELEASE</spring>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring}</version>
</dependency>
一、创建spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
二、配置web.xml
(一)配置spring配置文件的路径
<!-- spring配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applactionContext.xml</param-value>
</context-param>
(二)配置ContextLoaderListener
用来初始化ioc容器
<!-- 配置ContextLoaderListener用来初始化ioc容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(三)配置servlet
这里暂时配置为拦截所有请求
项目部署时tomcat会自动从/web-inf/目录中加载[servlet-name]-servlet.xml配置文件
<!-- 配置servlet映射-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、配置dispatcher-servlet.xml
ApplicationContext.xml 是servlet-name 全局配置文件,用来控制spring 特性的。
dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view。
使用applicationContext.xml文件时是需要在web.xml中添加listener
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 开启ioc容器扫描-->
<context:component-scan base-package="com.wfb"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开启mvc注解-->
<mvc:annotation-driven/>
</beans>
internalResourceViewResolver
中的prefix代表前缀,suffix代表后缀。如以上配置tomcat会找到**/web-inf/xxx.jsp**文件。
四、创建Controller
HelloController.java
@Controller
标记这个类为一个拦截器
@RequestMapping("/hello")
用在方法上拦截请求
package com.wfb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String sayHello() {
System.out.println("hello");
//根据internalResourceViewResolver的配置tomcat会加载/WEB-INF/say.jsp
return "say";
}
}
五、创建jsp文件
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
<br/>
来了
</body>
</html>
六、运行测试
启动tomcat将项目部署上去之后浏览器访问127.0.0.1:8080/项目名/hello
控制台将会打印hello
项目结构