1.通过maven创建一个webapp项目,并在IDEA 中增加smart tomcat的插件.

2.然后在pom文件中添加springmvc的依赖

<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring5和Thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency> <!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency> <!-- json 解析-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>

3.在web.xml文件夹中添加spring 的 filter 和 servlet

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
<!-- <init-param>-->
<!-- &lt;!&ndash; contextConfigLocation为固定值 &ndash;&gt;-->
<!-- <param-name>contextConfigLocation</param-name>-->
<!-- &lt;!&ndash; 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources &ndash;&gt;-->
<!-- <param-value>classpath:springMVC-servlet.xml</param-value>-->
<!-- </init-param>-->
<!--
作为框架的核心组件,在启动过程中有大量的初始化操作要做
而这些操作放在第一次请求时才执行会严重影响访问速度
因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时
-->
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springMVC</servlet-name>
<!--
设置springMVC的核心控制器所能处理的请求的请求路径
/所匹配的请求可以是/login或.html或.js或.css方式的请求路径
但是/不能匹配.jsp请求路径的请求
-->
<url-pattern>/</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>/assets/*"</url-pattern>
<url-pattern>/images/*</url-pattern>
</servlet-mapping> </web-app>

4.添加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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 配置controller包所在的位置,需要按项目调整 -->
<context:component-scan base-package="com.demo.controller"/> <!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean> <!-- 开启mvc注解驱动 -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 处理响应中文内容乱码 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8" />
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <mvc:view-controller path="/" view-name="index" />
<mvc:view-controller path="/adduser" view-name="adduser" /> <!--
处理静态资源,例如html、js、css、jpg
若只设置该标签,则只能访问静态资源,其他请求则无法访问
此时必须设置<mvc:annotation-driven/>解决问题
-->
<mvc:default-servlet-handler/> <!-- 开启MVC注解渠道-->
<mvc:annotation-driven /> </beans>

5.添加一个controller的包,并在springMVC-servlet.xml中配置component-scan标签,把包的位置填入。

写一个 controller和requestmap

package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class User {
@RequestMapping("/index")
public String getUser(){
System.out.print("this is get user!");
return "getUser";
}
}

然后对应增加一个webapp/WEB-INF/templates/getUser.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getuser</title>
</head>
<body>
this is getUser
</body>
</html>

6.最后的结构:

7.使用tomcat即可本地运行

http://localhost:8080/TestDrools/index

 

最新文章

  1. form表单 ----在路上(15)
  2. 友盟错误日志分析(转自:COCOACHINA shemy )
  3. Linux vim命令
  4. android基础开发之WebView
  5. PHP定时执行任务的实现
  6. JS网址正则验证
  7. 数据校验validator 与 DWZ
  8. DBSCAN算法
  9. linux 下ffmpeg和mencoder安装
  10. hibernate官方新手教程 (转载)
  11. 如何清除xcode里面的mobileprovision文件
  12. (一)原生JS实现 - 基本类方法
  13. Ubuntu apache 禁止目录浏览
  14. 快收藏!高手Linux运维管理必备工具大全,你会吗?
  15. poj2342 Anniversary party
  16. English trip V1 - B 22. Here,There and Everywhere 无处不在 Teacher:Taylor Key: Be + Ving
  17. JS保留小数 去尾法 进一法 四舍五入法
  18. CentOS 7 设置iptables防火墙开放proftpd端口
  19. Hadoop(1.2.1)安装
  20. ASP.NET 数据库缓存依赖

热门文章

  1. 爆肝200+小时,总结出的 Creator 3.x 入门修炼指南!全免费
  2. LeetCode-838 推多米诺
  3. 【Java-01-2】java基础-基本语法(2)(关系运算,if,循环)
  4. 文字icon
  5. Javascript高级程序设计(000)
  6. pgsql中行数据转json数组
  7. CVE-2020-1938 Tomcat AJP漏洞复现
  8. Unity 使用IO流获取PNG/JPG/GIF/BMP的宽高【转】
  9. 苹果App 上架 app store 提示 “构建版本错误”使用Application Loader发布App
  10. 配置tomcat 服务 启动模式