1. 新建项目,在pom.xml中添加build标签
<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>
2. 将项目添加web支持
如图
image.png
3. 给将添加lib包
image.png
4. 在web.xml中添加springmvc的配置
image.png
<!-- 1. 注册DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个springmvc的配置文件:【servlet-name】-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动级别 1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- / 匹配所有的请求;(不包含.jsp)-->
<!-- /* 匹配所有的请求;(包括 .jsp)-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5. 添加springmvc-servlet.xml,添加配置
image.png
<?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">
<!-- 自动扫描包,让指定包下的注解生效,有IOC容器统一管理 -->
<context:component-scan base-package="com.springmvc.controller"/>
<!-- 让Spring MVC 不处理静态资源 -->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
在spring中一般采用@RequestMapping 注解来完成映射关系
要想使@RequestMapping 注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理。
而annotation-driven配置帮助我们自动完成上述两个实例的注入。
-->
<mvc:annotation-driven/>
<!-- 视图解析器:DispatcherServlet 给他的ModelAndView -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6. 添加controller, 通过注解方式映射请求路径
image.png
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/helloController")
public class HelloController {
// 真实访问地址: 项目名/helloController/hello
@RequestMapping("/hello")
public String sayHello(Model model){
// 向模型中添加属性msg与值,可以再jsp页面中取出并渲染
model.addAttribute("msg","hello,springMVC");
// 返回值就是试图的名字,即 /WEB-INF/jsp/hello.jsp
return "hello";
}
}
7. 添加jsp页面
image.png
<%@ page contentType="text/html; UTF-8" language="java" %>
<html>
<head><title>hellomvc</title></head>
<body>
${msg}
</body>
</html>
8. 将项目添加到tomcat容器中,然后运行项目
image.png
image.png
9. 访问程序
http://localhost:8080/helloController/hello
访问成功
image.png