SpringMVC RESTFul实战案例访问首页

目录
  • SpringMVC RESTFul访问首页实现
    • 一、新建 index.html
    • 二、配置视图控制器
    • 三、Idea 部署配置

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 包,点击确定。

最后为了方便访问,修改下上下文(不改也可以)。

点击部署,成功后自动打开首页。

感谢《尚硅谷》的学习资源。

以上就是SpringMVC RESTFul实战案例访问首页的详细内容,更多关于SpringMVC RESTFul访问首页的资料请关注我们其它相关文章!

(0)

相关推荐

  • SpringMVC RESTFul实现列表功能

    目录 SpringMVC RESTFul列表功能实现 一.增加控制器方法 二.编写列表页 employee_list.html 三.访问列表页 SpringMVC RESTFul列表功能实现 一.增加控制器方法 在控制器类 EmployeeController 中,添加访问列表方法. @Controller public class EmployeeController { @Autowired private EmployeeDao employeeDao; @RequestMapping(v

  • 关于SpringMVC对Restful风格的支持详解

    目录 前言 一.RESTful简介 1.1.资源 1.2.资源的表述 1.3.状态转移 RESTful规范 二.RESTful的实现 三.HiddenHttpMethodFilter 3.1.HiddenHttpMethodFilter 过滤器处理put和delete请求 3.2.在web.xml中注册HiddenHttpMethodFilter 3.3.过滤器的先后配置顺序 总结 前言 RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义

  • SpringMVC RESTFul实战案例修改功能实现

    目录 SpringMVC RESTFul实现修改功能 一.回显功能 1.修改操作超链接 2.处理控制器方法 3.创建修改页面 二.修改功能 1.添加控制器方法 2.测试效果 SpringMVC RESTFul实现修改功能 一.回显功能 做实际修改操作之前,需要有个回显功能,就是点编辑页后可以看到数据. 1.修改操作超链接 这里的请求地址跟删除的一样,需要带上 id,因为要回显这个 id 的数据. <td> <a @click="deleteEmployee" th:h

  • SpringMVC实战案例RESTFul实现添加功能

    目录 RESTFul实现添加功能 一.前端改动 1. 修改列表页,增加一个[添加]按钮 2. 配置 view-controller 3. 编写添加页面 二.后端处理 三.测试效果 RESTFul实现添加功能 数据被删除差不多了,得做个添加的功能. 一.前端改动 1. 修改列表页,增加一个[添加]按钮 点击这个按钮可以调到新增页面. <tr> <th colspan="5">员工列表</th> </tr> <tr> <th

  • SpringMVC RESTFul实战案例删除功能实现

    目录 SpringMVC RESTFul实现删除功能 一.修改列表前端代码 1. 修改删除的请求地址 2. 添加删除用的 form 表单 3. 删除超链接绑定点击事件 二.增加后端控制器 三.测试效果 SpringMVC RESTFul实现删除功能 删除相对麻烦一点,因为 Rest 中得用 delete 方法请求. 在前面已经提到如何实现 delete 和 put 方法请求了,这里同样借助表单来提交 post 请求,然后转成 delete 请求方法. 一.修改列表前端代码 1. 修改删除的请求地

  • SpringMVC RESTFul实战案例访问首页

    目录 SpringMVC RESTFul访问首页实现 一.新建 index.html 二.配置视图控制器 三.Idea 部署配置 SpringMVC RESTFul访问首页实现 一.新建 index.html 在 webapp\WEB-INF\templates 下新建首页 index.html. <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <

  • SpringMVC使用RESTful接口案例详解

    目录 一.准备工作 二.功能清单 三.具体功能-访问首页 一.准备工作 和传统 CRUD 一样,实现对员工信息的增删改查. ①搭建环境 添加相关依赖 web.xml springmvc.xml ②准备实体类 public class Employee { private Integer id; private String lastName; private String email; //1 male, 0 female private Integer gender; public Integ

  • SpringMVC RESTFul实体类创建及环境搭建

    目录 一.搭建 mvc 环境 二.创建实体类 三.准备 dao 模拟数据 四.准备控制器 一.搭建 mvc 环境 新建一个 module 模块,创建 maven 工程,步骤跟以前一样,各种配置文件内容也可以拷贝修改一下即可. 二.创建实体类 新建个 bean 包,创建实体类 Employee: package com.pingguo.rest.bean; public class Employee { private Integer id; private String lastName; pr

  • SpringMVC RESTful支持实现过程演示

    这篇文章主要介绍了SpringMVC RESTful支持实现过程演示,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.概述 1.1 什么是RESTful RESTful软件开发理念,RESTful对http进行非常好的诠释. RESTful即Representational State Transfer的缩写. 综合上面的解释,我们总结一下什么是RESTful架构: 1)每一个URI代表一种资源: (2)客户端和服务器之间,传递这种资源的某种

  • SpringMVC RESTFul及REST架构风格介绍

    目录 一.RESTful 简介 二.RESTful 的实现 实践一下 1. get 和 post 请求 2. put 和 delete 请求 一.RESTful 简介 REST 是一种软件架构风格. REST:Representational State Transfer,表现层资源状态转移. 对此,有几个名字需要理解一下: 表现层:实际上就是前端的页面到后端的控制层. 资源:当应用部署到服务器上之后,万物皆资源,比如一个类.一个html页面等等. 1-资源是一种看待服务器的方式,即将服务器看作

  • Ajax异步请求的五个步骤及实战案例

    目录 前言 1.建立xmlHttpRequest异步对象 2.创建HTTP请求(设置请求方法和URL) 3.发送数据 4.设置回调函数 5.在回调函数中对不同的响应状态进行处理 案例实现 总结 前言 AJAX(Asynchronous JavaScript and XML):是指一种创建交互式网页应用的网页开发技术,通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这就意味着可以在不重新加载整个网页的情况下,对网页的局部进行更新. 1.建立xmlHttpRequest异步对象

随机推荐