springboot-thymeleaf(html页面跳转)

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

ctrl层对比代码

易犯错误1

    @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,不然也会直接返回字段值


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

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

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

项目结构:

springboot-thymeleaf(html页面跳转)_第1张图片


pom.xml

	

		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.springframework.boot
			spring-boot-starter-test
			
			test
		

		
			org.springframework.boot
			spring-boot-starter-tomcat
			provided
		

		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
	


注意添加的是


	org.springframework.boot
	spring-boot-starter-thymeleaf

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页面内容




    demo
    


my thymeleaf indexpage

更多详情


访问对应url如


springboot-thymeleaf(html页面跳转)_第2张图片

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


你可能感兴趣的:(thymeleaf)