SpringMVC RESTFul实战案例访问首页

2023-01-21 0 1,216

目录

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 部署配置

点击 配置。

SpringMVC RESTFul实战案例访问首页

继续按照顺序点击配置。

SpringMVC RESTFul实战案例访问首页

选择要部署的 war 包,点击确定。

SpringMVC RESTFul实战案例访问首页

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

SpringMVC RESTFul实战案例访问首页

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

SpringMVC RESTFul实战案例访问首页

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

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

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 SpringMVC RESTFul实战案例访问首页 https://www.20zxx.cn/462973/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务