SpringMVC RESTFul实战案例访问首页
作者:把苹果咬哭的测试笔记 时间:2022-03-12 00:21:01
SpringMVC RESTFul访问首页实现
一、新建 index.html
在 webapp\WEB-INF\templates 下新建首页 index.html。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" >
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/employee}" rel="external nofollow" >查看员工信息</a>
</body>
</html>
二、配置视图控制器
在 springMVC.xml 配置文件里,配置首页的 view-controller。另外还要开启注解驱动。
<?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">
<!-- 自动扫描包 -->
<context:component-scan base-package="com.pingguo.rest"></context:component-scan>
<!-- 配置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>
<!--
path:设置处理的请求地址
view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<!--开启 mvc 的注解驱动-->
<mvc:annotation-driven />
</beans>
三、Idea 部署配置
点击 配置。
继续按照顺序点击配置。
选择要部署的 war 包,点击确定。
最后为了方便访问,修改下上下文(不改也可以)。
点击部署,成功后自动打开首页。
感谢《尚硅谷》的学习资源。
来源:https://blog.csdn.net/wessonlan/article/details/124812966
标签:SpringMVC,RESTFul,访问首页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
OpenFeign设置header的三种方式总结
2023-06-25 19:03:46
![](https://img.aspxhome.com/file/2023/8/79338_0s.jpg)
Java算法之递归算法计算阶乘
2021-06-30 14:10:56
![](https://img.aspxhome.com/file/2023/4/63454_0s.png)
IDEA:Git stash 暂存分支修改的实现代码
2023-11-29 09:16:35
![](https://img.aspxhome.com/file/2023/3/78363_0s.jpg)
springboot读取配置文件中的参数具体步骤
2023-11-29 05:46:14
![](https://img.aspxhome.com/file/2023/2/59582_0s.png)
教你如何使用Java8实现菜单树形数据
2022-09-11 12:29:22
![](https://img.aspxhome.com/file/2023/3/67163_0s.png)
mybatis中foreach嵌套if标签方式
2023-11-20 23:11:05
![](https://img.aspxhome.com/file/2023/3/59703_0s.png)
vscode+platformIO开发stm32f4的实现
2023-11-02 16:43:37
![](https://img.aspxhome.com/file/2023/9/105169_0s.png)
Java语法基础之函数的使用说明
2022-07-20 15:55:09
java(包括springboot)读取resources下文件方式实现
2021-06-03 20:16:06
![](https://img.aspxhome.com/file/2023/4/68184_0s.png)
Java输出打印工具类封装的实例
2022-06-18 10:02:27
c#使用listbox的详细方法和常见问题解决
2023-08-27 11:28:56
![](https://img.aspxhome.com/file/2023/9/68069_0s.jpg)
一文快速掌握Spring Cloud Stream
2023-09-01 23:09:38
![](https://img.aspxhome.com/file/2023/6/73116_0s.png)
基于Java中字符串indexof() 的使用方法
2022-12-09 19:37:38
![](https://img.aspxhome.com/file/2023/7/65587_0s.jpg)
一篇文章带你入门Java变量
2021-09-21 07:46:02
![](https://img.aspxhome.com/file/2023/8/61948_0s.jpg)
详解APP微信支付(java后台_统一下单和回调)
2023-11-10 17:26:42
![](https://img.aspxhome.com/file/2023/3/59153_0s.png)
使用Feign传递请求头信息(Finchley版本)
2023-06-07 22:38:07
![](https://img.aspxhome.com/file/2023/2/62742_0s.jpg)
SpringBoot持久化层操作支持技巧
2023-11-24 06:40:32
![](https://img.aspxhome.com/file/2023/3/59933_0s.png)
java使用JNA(Java Native Access)调用dll的方法
2022-02-06 09:27:34
SpringBoot中的Thymeleaf用法
2023-10-30 17:30:39
IDEA无法使用Git Pull的问题
2023-05-04 10:55:39
![](https://img.aspxhome.com/file/2023/5/79945_0s.png)