SpringBoot跳转页面详解+thymeleaf

        初次作SpringBoot,要解决页面跳转的问题,这个问题我弄了大半天,弄好后,其实也不算个事,写出来给你们提个醒!其实不要使用spring boot的@RestController注解,直接使用spring原来的注解@Controller就能够了。示例以下:html

@Controller前端

public class ActionController {java

    @RequestMapping(value = "/action",method = RequestMethod.GET)web

    public String index(){spring

        return "login";缓存

    }springboot

}服务器

若是你的项目如什么都没有配,那么你想跳到login.html时,语句必须是  return "login.html"  ,不然它会报错 。由于找不到login文件。app

        为何呢?由于@RestController注解,至关于@Controller+@ResponseBody两个注解的结合, @Responsebody后,返回结果直接写入HTTP response body中,不会被解析为跳转路径,因此你老是看到是打印字符串的效果,不是跳转效果。jsp

=========================================================

        请看更详细的解答:SpringBoot里面没有咱们以前常规web开发的WebContent(WebApp),它只有src目录,在src/main/resources下面有两个文件夹,[static]和[templates],springboot默认static中放静态页面,而templates中放动态页面,见下图:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406104541721-1302659819.png

静态页面:

        静态页面能够直接访问。这里咱们直接在static放一个hello.html,而后直接输入http://localhost:8080/hello.html便能成功访问(好像能够新建一个public文件夹,也能够放静态文件)。

        也能够经过controller跳转:

@Controller

public class HelloController {

    @RequestMapping("/Hi")

    public String sayHello() {

        return "hello.html";

    }

}

        而后输入http://localhost:8080/Hi就能够成功访问 

动态页面:

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

要在pom中要添加Thymeleaf组件

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

但你加上了上面的thymeleaf后,你必须重启工程,即便是你配置了热启动后,也要重启工程,才能够看到效果 。

    咱们先在tempates文件夹中也新建一个hello.html但内容不一样,而后先试一下直接访问该页面。输入http://localhost:8080/hello.html:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406105509468-1935985534.png

结果显然访问的是静态问价夹里面的那个hello.html

而后咱们如今再试一下用controller:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406105655969-540195122.png

彷佛没法访问到hello.html了。。。这是由于:静态页面的return默认是跳转到/static/index.html,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/index.html,注意看二者return代码也有区别,动态有或没有html后缀均可以。

也就是咱们要这样改controller:

@Controller

public class HelloController {

@RequestMapping("/Hi")

    public String sayHello() {

        return "hello";//在没有配置的状况下,return "hello”; 或者return "hello"均可以,它们都会到templates/index.html去。

    }  

}

而后就能够成功跳转了

而后咱们看看返回一点数据在前端利用Thyemleaf来拿:

@Controller

public class HelloController {

 

    @RequestMapping("/Hi")

    public ModelAndView sayHello() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("hello");

        modelAndView.addObject("key", 12345);

        //System.out.println("test");

        return modelAndView;

    }

   

}

---------------------

Templates/hello.html

---------------------

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8"/>

<title>Insert title here</title>

 

</head>

<body>

<h1>this is the hello.html in templates</h1>

<span th:text="${key}"></span> 

</body>

</html>

 

效果:

https://images2018.cnblogs.com/blog/1095436/201804/1095436-20180406115915727-969075466.png

 

 

 

若是不想返回视图,则用@RestController

若是用了静态模板你还想返回static中的页面,那么就要用重定向:

若是在使用动态页面时还想跳转到/static/index.html,可使用重定向return "redirect:/index.html"。 

 

几点tips:

1.拦截的url最后不要跟视图重合,不然会抛出Circular view path异常,我以前就是

@Controller

public class HelloController {

 

    @RequestMapping("/hello")

    public String sayHello() {

        return "hello.html"; 

    }

   

}

而后就报错说会有个循环视图的错误,反正之后注意就是。

2.每次改完都要从新中止应用,再从新启动很烦~但springboot有个叫热部署的东西,就是说在项目中修改代码能够不用从新中止应用再从新启动,能够自动重启,这里咱们用的是devtools:

方法见网址:https://www.cnblogs.com/cx-code/p/8686453.html,摘录以下:

sts热部署,便是在项目中修改代码不用从新启动服务,提升效率。

方法以下:

1.pom文件中引入  devtools  依赖:

    

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <!-- optional=true, 依赖不会传递, 该项目依赖devtools; 以后依赖boot项目的项目若是想要使用devtools, 须要从新引入 -->
  <optional>true</optional>
</dependency>

 

2.application.properties  文件中码上如下内容:

spring.thymeleaf.cache=true        //缓存
spring.devtools.restart.enabled=true   //开启
spring.devtools.restart.additional-paths=src/main/java  //监听目录

3.原理

<!-- devtools能够实现页面热部署(即页面修改后会当即生效,
这个能够直接在application.properties文件中配置spring.thymeleaf.cache=false来实现) -->
<!--
实现类文件热部署(类文件修改后不会当即生效),实现对属性文件的热部署。 -->
<!--
devtools会监听classpath下的文件变更,而且会当即重启应用(发生在保存时机),
注意:由于其采用的虚拟机机制,该项重启是很快的 -->
<!--
1base classloader Base类加载器):加载不改变的Class,例如:第三方提供的jar包。 -->
<!--
2restart classloaderRestart类加载器):加载正在开发的Class -->
<!--
为何重启很快,由于重启的时候只是加载了在开发的Class,没有从新加载第三方的jar包。 -->

-----------------------------------

如何使用thymeleaf [taim li:f] 来达到之前jsp获取Model值这样的效果(thymeleaf是什么?你自行百度),那么,你得在maven中加依赖:

<dependency>

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

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

</dependency>

 

而后,在配置文件:application.yml或application.property中加配置:

spring.thymeleaf.prefix:classpath: /templates/

spring.thymeleaf.suffix: .html

spring.thymeleaf.mode: HTML

spring.thymeleaf.encoding: utf-8

spring.thymeleaf.cache: false

 

再写控制类时把Model加在参数中:

@Controller
public class ActionController {

    @RequestMapping(value = "/action",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("aaa","我是一个兵");
        model.addAttribute("bbb","来自老百姓!");
        return "index";
    }
}

它就会跳转到/templates/index.html这个网页上去了。

你的index.html若是想显示"我是一个兵","来自老百姓"的值的话,网页应该这样写:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">    <!-- 据说这一句要,但我没有要这个标签,也能用  -->
<head>
<body>
<h1>AAAAindex.jspsdfasd</h1>
<p>Today is: <span th:text="${aaa}">13 february 2011</span>.</p>   <!-- 不能直接${aaa},而应该写在一个标签中 -->
@{${aaa}}<br> <!--这里是取不到值的 -->
${aaa}<br>    <!--这里是取不到值的 -->
${bbb}        <!--这里是取不到值的 -->

</body>
</html>

-------------------------------------------