基本步骤

  • 新建Maven项目(Web)
  • 导入依赖
  • 配置web.xml
  • 配置springmvc配置文件
  • 编写Controller
  • 创建view页面
  • 部署并启动Tomcat

开始搭建

新建Maven项目(Web)

使用webapp骨架,创建一个web项目

导入依赖

<properties>
<spring.version>5.1.13.RELEASE</spring.version>
<junit.version>4.13.1</junit.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</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>

为了防止资源导出的问题,建议在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>

配置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"> <!--1.注册servlet-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 启动顺序,数字越小,启动越早 -->
<load-on-startup>1</load-on-startup>
</servlet> <!--所有请求都会被springmvc拦截 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

配置springmvc配置文件

在resources文件夹创建spring-mvc.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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
<context:component-scan base-package="com.tanyiqu.controller"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler/>
<!--
支持mvc注解驱动
在spring中一般采用@RequestMapping注解来完成映射关系
要想使@RequestMapping注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理。
而annotation-driven配置帮助我们自动完成上述两个实例的注入。
-->
<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>

编写Controller

创建controller包,创建HelloController类

package com.tanyiqu.controller;

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 sayHello(Model model) {
model.addAttribute("msg", "hello,SpringMVC");
return "hello";
}
}

创建view页面

WEB-INF文件夹创建jsp文件夹,并在里面创建hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>

部署并启动Tomcat

启动后在浏览器输入链接

http://127.0.0.1:8080/hello(根据你自己配置的项目路径输入链接)

最新文章

  1. retrofit2的get和post
  2. C语言语法分析器
  3. SQL——神奇代码1之Update
  4. XML 和 List 互转类
  5. [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接
  6. 关于spring mvc MaxUploadSizeExceededException 死循环解决方案
  7. online training
  8. RStudio Keyboard Shortcuts
  9. 将小度WiFi改造为无线网卡(小度WiFi能够接收WiFi信号)
  10. 推荐一本好书给即将走入工作的程序员and程序媴
  11. 佩特来项目经验小集合(2)___组合查询存储过程,报错 &amp;quot;varchar JBID=&amp;#39;&amp;#39; 转换成数据类型 int 时失败&amp;quot;
  12. C#静态成员静态类。
  13. C# chart控件基础使用
  14. crontab定时任务-干货案例
  15. HBase Cli相关操作
  16. JDBC入门(3)--- PrepareStatement
  17. [Angular-Scaled Web] 6. Navigating between states with ui-router
  18. build.prop文件介绍与用法举例
  19. Volley的post使用
  20. django的查询集

热门文章

  1. 35 异常机制 异常处理机制 异常处理五个关键字 try、catch、finally、throw、thorws 代码
  2. web服务器-nginx负载均衡
  3. bitsadmin windwos自带下载命令
  4. python练习册 每天一个小程序 第0011题
  5. 【Python 第0课】Why Python?
  6. 分库分表之后分布式如何保证ID全局唯一性
  7. 如何实现 Spring Boot 应用程序的安全性?
  8. Dubbo 服务降级,失败重试怎么做?
  9. Homebrew 卸载后重新安装mysql
  10. MyBatis 实现一对一有几种方式?具体怎么操作的?