解决SpringBoot项目通过Controller的return没法跳转页面的问题

Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers

这个是我的报错。

我在SpringBoot项目中的Controller中通过return跳转页面的时候无法跳转,项目报上面的异常。首先我把Controller类中的注解@RestController修改为了@Contrller,但是没有什么作用,接着我在Controller类中的方法上面添加了注解@Responsebody,这个时候发现页面还是没有跳转成功,但是页面直接打印出来我返回的内容。随后我检查了自己的项目目录,关键是templates的位置,对比了我的配置文件application.yml中关于thymeleaf的配置。

仔细对照,并没有什么不对的地方。然后我查看了添加的依赖,我的依赖是

<dependency>       

    <groupId>org.springframework.boot</groupId>      

    <artifactId>spring-boot-starter-thymeleaf</artifactId>    

</dependency>

依赖也没有问题。最后我注意到我的pom.xml中的build中有一段配置

这段配置是为了项目能够扫描到xml或者priperties而配置的,其实并没有什么额外的作用,但是也就是这段配置导致找不到我的html页面,将他们注释掉就好了,或者在里面添加上html文件类型就好,通过Controller中的return就可以正常跳转页面了。

Controller类中的跳转方法:

@RequestMapping(value = "/index")
@ResponseBody
public ModelAndView index(Model model){
    model.addAttribute("boot_name","jack");
    model.addAttribute("boot_age",20);
    model.addAttribute("boot_info","jack,success");
    return new ModelAndView("index");
}

添加html类型如下:

<!--保证配置文件能被扫描到-->
<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.yml</include>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <includes>
      <include>**/*.yml</include>
      <include>**/*.properties</include>
      <include>**/*.xml</include>
      <include>**/*.html</include>
    </includes>
  </resource>
</resources>
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
你可以通过配置 Spring Boot 的视图解析器和控制器来实现在访问默认端口时跳转到 index 页面。 1. 配置视图解析器 在 Spring Boot 项目中,你可以通过在 application.properties 或 application.yml 文件中添加以下配置来配置视图解析器: application.properties: ``` spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp ``` application.yml: ``` spring: mvc: view: prefix: /WEB-INF/views/ suffix: .jsp ``` 这里我们使用 JSP 页面作为示例,你也可以使用其他类型的视图。 2. 创建控制器 在 Spring Boot 中,你可以使用 @Controller 注解来声明一个控制器。在控制器中,你可以使用 @RequestMapping 注解来指定请求的 URL 和 HTTP 方法。 在我们的示例中,我们需要创建一个控制器来处理根路径的请求。在控制器中,我们可以使用 @RequestMapping("/") 注解来指定根路径的请求。我们还可以使用 @GetMapping 注解来指定 GET 请求。在控制器的方法中,我们可以使用 ModelAndView 对象来指定要返回的视图和模型数据。 下面是一个示例控制器的代码: ```java @Controller public class HomeController { @GetMapping("/") public ModelAndView home() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); return modelAndView; } } ``` 这个控制器会处理根路径的 GET 请求,返回名为 "index" 的 JSP 页面。 3. 配置默认端口 在 Spring Boot 中,默认的端口号是 8080。如果你想要更改默认端口号,可以在 application.properties 或 application.yml 文件中添加以下配置: application.properties: ``` server.port=8081 ``` application.yml: ``` server: port: 8081 ``` 这里我们将端口号设置为 8081。 现在,当你访问 http://localhost:8081/ 时,就会自动跳转到 index 页面
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one_smail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值