springboot 本地访问静态页面,动态页面,其他静态资源,以及默认页面的方式...

静态页面访问

(其他静态资源也行)

spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下

/META-INF/resources

/src/java/resources

                            /static

                            /public

                            /resources

                            
比如,在resources建立一个static目录和index.htm静态文件,访问地址 http://localhost:8080/index.html 

只有这三个行,放在templates 和直接放在src/main/resources 下是没办法静态访问的。

 095335_mWEn_2885163.png

HTML

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>第一个thymeleaf程序</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${xname} + '!'" />
    <div>1234567890!!!xx</div>
</body>
</html>

静态效果

093813_6peZ_2885163.png

静态首页


spring-boot静态首页的支持,即index.html放在以下目录结构会直接映射到应用的根目录下:

classpath:/META-INF/resources/index.html    
classpath:/resources/index.html    
classpath:/static/index.html    
calsspath:/public/index.html  

我一个一个试了,就截这一个图吧,不都放上来了。

093114_M0YF_2885163.png

HTML

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>第一个thymeleaf程序</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${xname} + '!'" />
    <div>1234567890!!!xx</div>
</body>
</html>

直接访问 http://localhost:8080  到默认的静态首页

092951_7say_2885163.png

就只能上面那几个文件夹可以,直接放在根目录下是不行的,通过 http://localhost:8080 访问不到

095020_S1mo_2885163.png

 

动态页面

动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问JSP。spring boot建议不要使用JSP,默认使用Thymeleaf来做动态页面。

在pom.xml  中添加Thymeleaf组件

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-thymeleaf</artifactId>  
</dependency>  

TemplatesController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/")
public class ThymeleafController {

    @RequestMapping(value = "/thymeleaf")
    public String greeting(@RequestParam(name = "name", required = false, defaultValue = "world") String name,
                           Model model) {
        model.addAttribute("xname", name);
        return "index";
    }

    
}

@RestController:上一篇中用于将返回值转换成json

@Controller:现在要返回的是一个页面,所以不能再用@RestController,而用普通的@Controller

默认跳转到 templates/index.html  动态页面,templates目录为spring boot默认配置的动态页面路径

100321_3Zij_2885163.png

默认情况下只能放这里。

HTML

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>第一个thymeleaf程序</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${xname} + '!'" />
    <div>1234567890!!!xx</div>
</body>
</html>

访问URL,  http://localhost:8080/thymeleaf  结果动态资源显示出来了。看到明显和静态访问不一样。

100453_Oi5I_2885163.png

在spring-boot下,默认约定了Controller试图跳转中thymeleaf模板文件的的

前缀prefix是”classpath:/templates/”,

后缀suffix是”.html” 
这个在application.properties配置文件中是可以修改的。 
如下配置可以修改试图跳转的前缀和后缀

spring.thymeleaf.prefix: /templates/    
spring.thymeleaf.suffix: .html   

 

更过有关thymeleaf中的默认配置可以查看org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类的属性 

 

参考 : spring boot(4)-html和templates

            spring boot 直接返回HTML

转载于:https://my.oschina.net/zjllovecode/blog/1579656

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,Controller之间的调用可以通过注入调用或使用RestTemplate进行 HTTP 请求调用两种方式实现。 1. 注入调用:在Controller类中注入其他Controller类的实例,即可在当前Controller类中调用其他Controller中的接口。注入方式一般通过@Autowired或@Resource注解实现。例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private OrderController orderController; @GetMapping("/{userId}/order") public String getUserOrder(@PathVariable Long userId) { String order = orderController.getOrder(userId); return "User: " + userId + ", Order: " + order; } } ``` 2. HTTP请求调用:在Controller中使用RestTemplate实现HTTP请求调用其他Controller中的接口。RestTemplate是Spring框架提供的基于HTTP协议的客户端工具,可以用来调用RESTful服务。例如: ```java @RestController @RequestMapping("/order") public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/{userId}") public String getOrder(@PathVariable Long userId) { String userUrl = "http://localhost:8080/user/" + userId; String user = restTemplate.getForObject(userUrl, String.class); return "Order for User " + user; } } ``` 需要注意的是,如果在同一个Spring Boot应用程序中,使用注入调用需要确保被注入的Controller类被声明为bean,否则会出现NullPointerException异常;而使用HTTP请求调用可以直接调用其他Controller类的接口,无需担心依赖关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值