1:創(chuàng)建一個maven的web項目參照上一篇文章
2:使用maven撇簿,在pom.xml中導入Spring框架毒租,下面部分框架實際沒有用到
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-context</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-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
3:修改web.xml文件掌腰,在web-app標簽中添加
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
說明:加載web.xml后會去查找一個<servlet-name>為dispatcher-servlet.xml的文件
4:手動增加dispatcher-servlet.xml文件
dispatcher-servlet.xml內(nèi)容如下:
<?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:p="http://www.springframework.org/schema/p"
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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自動掃描@Controller注入為bean -->
<context:component-scan base-package="com.springmvc.helloworld"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
說明:com.springmvc.helloworld是你的包名澈魄,此處還沒有創(chuàng)建吵瞻,所以為紅色
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
這里已經(jīng)指定jsp的路徑和后綴名惧笛,在之后的返回中無需制定路徑
5:創(chuàng)建java文件从媚,新建一個java文件夾,并修飾為Sources患整,接著創(chuàng)建包com.springmvc.helloworld和java文件
注解@Controller:聲明HiController類是一個 SpringMVC Controller類
注解@RequestMapping:添加請求url映射地址拜效,此處的url變成:http://localhost:8080/hi/say
6:創(chuàng)建一個say.jsp作為HiController類中say()方法的返回
7:直接啟動tomcat喷众,報錯了
絕大部分的錯誤都是缺少依賴庫,檢測pom.xml
這里的錯誤顯示:項目缺少org.springframework.web.servlet.DispatcherServlet
但是我們在maven中已經(jīng)加入了紧憾,此時我們應(yīng)該想到是否沒有引用到項目中到千,查看Configurations,點擊下圖中的位置
將1中的所有庫添加到2的位置赴穗,如下圖