首先新建一个Maven项目,这里我用的Idea,在下图所示啥也不用选,直接next
如图所示,创建完成maven项目
在新建的项目上右击,点击图中所示
选择图中所示
这样,项目就成了一个web项目,可以看到项目结构中多出了一个web包
在Pom文件中添加依赖
<!-- 导入依赖-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.9.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>
然后可以看到maven中导入了相关依赖,但是在web包下面没有lib文件夹,需要我们自己去创建,如果没有lib包,在项目运行后会找不到url,报404错误
可以看到下图,没有lib包
然后我们点击右侧的相应的项目,右击
可以看到,lib包出来了
至此,基于maven的web项目在集成springmvc的前置操作就完成了
,接下来要配置springmvc-servler.xml文件来完成有关springmvc的配置,以及配置web.xml,编写controller层的代码,pojo类
基于xml方式的配置
首先在java包下面新建com.tanjiu.controller包,然后新建HelloController类
@Controller
@RequestMapping("/hello")
public class HelloController {
//三个方法只需要写三个@RequestMapping即可,如果用servlet需要写三个servlet映射才可以,配置非常繁琐
@RequestMapping("/h1")
public String hello(Model model){
//封装数据
model.addAttribute("msg","Hello,SpringMVCAnnotation01");
//会被视图解析器处理
return "hello";
}
@RequestMapping("/h2")
public String hello2(Model model){
//封装数据
model.addAttribute("msg","Hello,SpringMVCAnnotation02");
//会被视图解析器处理
return "hello";
}
@RequestMapping("/h3")
public String hello3(Model model){
//封装数据
model.addAttribute("msg","Hello,SpringMVCAnnotation03");
//会被视图解析器处理
return "hello";
}
}
然后在web-inf包下面新建jsp包,新建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
接下来在resource包下新建springmvc-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: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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置自动扫描包,让指定包下面的注解生效,让ioc容器统一管理
springmvc只扫描controller层的注解,spring的配置中要把controller层排除扫描
-->
<context:component-scan base-package="com.tanjiu.controller"/>
<!--让springmvc不处理静态资源如.css .html .js文件,固定配置-->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
在springmvc中一般采用@RequestMapping注解来完成映射关系
要想使@RequestMapping注解生效,必须向上下文注册@DefaultAnnotationHandlerMappin
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理
而annotation-driver配置帮助我们自动完成上面两个实例的注入
-->
<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>
然后配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置DispatchServlet,这个是springmvc的核心,请求分发器,前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatchServlet要绑定Spring的配置文件 -->
<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>
<!-- 在springmvc中,/和/*是有区别的
/:会只匹配所有的请求,不会去匹配jsp页面
/*:会匹配所有的请求,包括jsp页面
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 以上为DispatchServlet的配置-->
</web-app>
然后添加tomcat
图中所示,只留一个/即可,然后点击Ok
然后启动tomcat,按照HelloController类的url访问即可