创建完成后, 讲spring 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.learn.javaee</groupId>
<artifactId>createspringmvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>createspringmvc Maven Webapp</name>
<url>http://maven.apache.org</url> <properties>
12 <spring.version>4.3.2.RELEASE</spring.version>
13
14 </properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- servlet api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency> <!-- Spring -->
<dependency>
41 <groupId>org.springframework</groupId>
42 <artifactId>spring-core</artifactId>
43 <version>${spring.version}</version>
44 </dependency>
45 <dependency>
46 <groupId>org.springframework</groupId>
47 <artifactId>spring-context</artifactId>
48 <version>${spring.version}</version>
49 </dependency>
50 <dependency>
51 <groupId>org.springframework</groupId>
52 <artifactId>spring-beans</artifactId>
53 <version>${spring.version}</version>
54 </dependency>
55 <dependency>
56 <groupId>org.springframework</groupId>
57 <artifactId>spring-aop</artifactId>
58 <version>${spring.version}</version>
59 </dependency>
60 <dependency>
61 <groupId>org.springframework</groupId>
62 <artifactId>spring-jdbc</artifactId>
63 <version>${spring.version}</version>
64 </dependency>
65 <dependency>
66 <groupId>org.springframework</groupId>
67 <artifactId>spring-web</artifactId>
68 <version>${spring.version}</version>
69 </dependency>
70 <dependency>
71 <groupId>org.springframework</groupId>
72 <artifactId>spring-webmvc</artifactId>
73 <version>${spring.version}</version>
74 </dependency>
75 <dependency>
76 <groupId>org.springframework</groupId>
77 <artifactId>spring-tx</artifactId>
78 <version>${spring.version}</version>
79 </dependency>
80 <dependency>
81 <groupId>org.springframework</groupId>
82 <artifactId>spring-context-support</artifactId>
83 <version>${spring.version}</version>
84 </dependency>
85 <dependency>
86 <groupId>org.springframework</groupId>
87 <artifactId>spring-orm</artifactId>
88 <version>${spring.version}</version>
89 </dependency>
90 <dependency>
91 <groupId>org.springframework</groupId>
92 <artifactId>spring-test</artifactId>
93 <version>${spring.version}</version>
94 </dependency>
95 <dependency>
96 <groupId>org.springframework</groupId>
97 <artifactId>spring-websocket</artifactId>
98 <version>${spring.version}</version>
99 </dependency>
<!-- Spring --> </dependencies>
<build>
<finalName>createspringmvc</finalName>
</build>
</project>
然后编辑web.xml文件
如果安装过STS插件的话, 直接按alt + 回车
选择后就出来了, 然后改成如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1"> <!-- 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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationcontext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
红色字体部分是需要修改的,
classpath:applicationcontext.xml 文件名可以修改, 无所谓
按照如图位置创建applicationcontext.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"> <!-- 设置自动扫描的包 -->
<context:component-scan base-package="cn.learn.*"></context:component-scan>
要在src/main/java 下创建包为cn.learn, 为了方便就这么写了, 大神别喷我
<!-- 开启自动注解支持 -->
<mvc:annotation-driven />
<!-- 定义静态资源位置 -->
<mvc:resources location="/WEB-INF/static/" mapping="/static/**"></mvc:resources> <!-- jsp视图处理 start -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 视图后缀 -->
<property name="suffix" value=".jsp"></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.InternalResourceView" />
<property name="order" value="1" />
</bean>
<!-- jsp视图处理 end --> </beans>
然后变成这样的目录结构
在cn.learn包下新建一个controller包(这一步不是必须的, 但是一般都要分开, 这东西看项目)
然后在controller包下新建一个类 IndexController
package cn.learn.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/index")
public class IndexController { @RequestMapping("/helloword")
public ModelAndView hellowordAction(){ //直接返回到view目录下的 index目录下的helloword.jsp
return new ModelAndView("index/helloword");
}
}
新建文件 在 view/index/helloword.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>helloword</title>
</head>
<body>
this is a helloword page
</body>
</html>
启动tomcat浏览器访问
http://localhost:8080/createspringmvc/index/helloword
应该注意到在applicationcontext.xml文件中配置过一个静态文件访问
<mvc:resources location="/WEB-INF/static/" mapping="/static/**"></mvc:resources>
把web应用的所有 类似于js css images 等, 不需要走框架的都放到这里
例如
写了一行 alert(1);
在浏览器中访问http://localhost:8080/createspringmvc/static/test.js
如果把这个js在页面中引用的话
在helloword.jsp页面引用<script type="text/javascript" src="${pageContext.request.contextPath}/static/test.js"></script>
再次访问http://localhost:8080/createspringmvc/index/helloword
这样就完成springmvc的最基本的可以访问了