springboot-thymeleaf(html页面跳转)

遇到的问题:按照别人的blog搭thymeleaf页面就是html页面跳转不了,我那个郁闷呀!!!终于尝试了无数遍搞好了,报的错误是模板不存在。html

ctrl层对比代码java

易犯错误1web

@RequestMapping(value ="/home_3", method = RequestMethod.GET)
    @ResponseBody
    public String homes(){
        return "index3";
    }//输入localhost:8081/home_3  只是返回了string字段为home_3  错误

    @RequestMapping(value ="/homess")
    public String homess(){
        return "sys/index";
    }//正确输出结果页面
}

易犯错误2

ctrl的标签是@Controller而不是@RestController,否则也会直接返回字段值spring


注意点 关于路径这一块可写可不写tomcat

-------------------------------不废话  直接上代码------------------------app

本项目是打成war格式,外置tomcat启动。ide

项目结构:spring-boot



pom.xml测试

<dependencies>

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

		<!-- 测试 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<!-- 只在test测试里面运行 -->
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

		<!--网页模板-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>


注意添加的是ui

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

ctrl层代码

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

/**
 * Created by ASUS on 2018/1/5.
 */
@Controller
public class PageController {
    @RequestMapping("/page")
    public String page3(Model model){
        model.addAttribute("userName","张三");
        return "hello";
    }

    @RequestMapping("sys/index")
    public String page(){
        return "sys/index"; 
    }

    @RequestMapping("sys/index_out")
    public String page3(){
        return "index_out";
    }
}//访问只能是tem文件夹下

配置文件

spring.thymeleaf.prefix=classpath:/templates/
注意: 可写可不写

html页面内容

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>my thymeleaf indexpage</h1>
<a href="/sys/more">更多详情</a>
</body>
</html>


访问对应url如



code download:http://download.csdn.net/download/sicily_winner/10190933