第一步:创建maven的web工程;
第二步:由于Maven可能存在资源过滤的问题,我们将其完善,在pom.xml在添加以下代码:
<!--解决配置文件不能导入问题,没问题不加,加上也没大碍-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
第三步:pom.xml导入相关资源包:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!---->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!---->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
第四步:在web.xml中配置注册DispatcherServlet,这段基本上固定,除了spring配置文件的名字:
<!-- 注册前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置springmvc配置文件路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<!--项目创建时启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
第五步:创建spring-servlet.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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!--指定扫描包,让包下的注解生效-->
<context:component-scan base-package="com.it"/>
<!--让mvc不在处理静态资源 -->
<mvc:default-servlet-handler/>
<!--开启mvc的注解驱动 -->
<mvc:annotation-driven/>
<!--视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀 -->
<property name="prefix" value="WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
第六步:创建视图目录,在WEN-INF下创建jsp文件夹(房子WEB-INF下可以保证安全,浏览器端不能直接访问),在jsp文件夹下创建一个jsp文件hello.jsp(名称自定):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success page </title>
</head>
<body>
${msg}
</body>
</html>
第七步:创建控制器HelloController,并添加注解:
package com.it;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*视图
*/
@Controller
public class HelloController {
@RequestMapping("/hello")//访问该视图的路径
public String hello(Model model){
model.addAttribute("msg","hello,springmvc annotation!");
return "hello";//返回的值就是视图的名字 对应 /WEB-INF/jsp/hello.jsp
}
}
第八步:启动服务器,访问,观察结果。出现hello,springmvc annotation!则成功