springboot 跳转至页面遇到的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baofengyu90/article/details/86485825

问题汇总(先声明,我的springboot 版本是1.5.7.RELEASE):

1、想跳转到某个页面,但是页面上却显示字符串,比如

检查下controller类

@Controller
public class HomeController {
	
	@RequestMapping(value="/index",method = RequestMethod.GET)
	@ResponseBody
	public String index(){
		return "/index";
	}
}

我的解决办法:去掉@ResponseBody注解

另外附:我的资源文件的目录结构

2、报错:

There was an unexpected error (type=Internal Server Error, status=500).

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

我的目录结构:

controller类代码,见问题1。

我的解决办法:在resources下新建templates文件夹,新建index.html

注:我的配置文件没有添加额外的配置,比如

spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html

为什么我再强调这个,因为在网上其他文章中有这样解决的,但是我没有按着这样配

3、发送get请求,报错:

There was an unexpected error (type=Not Found, status=404).

No message available

我的代码和资源文件目录结构:

@Controller
public class HomeController {
	
	@RequestMapping(value="/getIndex",method = RequestMethod.GET)
	public String index(){
		return "/index";
	}	
}

我的解决办法:

在pom文件里面没有添加下面的这个依赖,添加上即可

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

猜你喜欢

转载自blog.csdn.net/baofengyu90/article/details/86485825