Thymleaf不渲染结果,而是打印Spring Boot应用程序的控制器方法的返回值

时间:2019-01-15 05:21:34

标签: spring-boot thymeleaf

我不熟悉Thymleaf-spring-boot组合应用程序开发。 我按照教程进行操作,并尝试将Spring Boot应用程序与Thymleaf一起逐步构建应用程序。我的应用程序已启动并正在运行,但没有显示使用Model model解析为(在控制器中)到model.addAttribute("greet", "Hello there");中的值。在视图中,我将其打印为<span th:text="${greet}"></span>。它仅显示在方法中以return "index";返回的值(索引)。预期要打印的值是Hello。

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Greet</title>

</head>
<body>
   <h1>Greet Is </h1>
   <span th:text="${greet}"></span>

</body>
</html>

GreetController

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetController {
    @GetMapping("/")
    public String greet(Model model) {
        model.addAttribute("greet", "Hello there");
        return "index";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>product.manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>product.manager</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

控制台一次浏览localhost:8083

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-15 10:26:19.520  INFO 14144 --- [  restartedMain] com.test.product.manager.Application     : Starting Application on DESKTOP-AUANCVF with PID 14144 (started by acer in E:\MyStuff\Study projects\product.manager\product.manager)
2019-01-15 10:26:19.524  INFO 14144 --- [  restartedMain] com.test.product.manager.Application     : No active profile set, falling back to default profiles: default
2019-01-15 10:26:19.597  INFO 14144 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-01-15 10:26:19.597  INFO 14144 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-01-15 10:26:19.598  INFO 14144 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-01-15 10:26:19.598  INFO 14144 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-01-15 10:26:21.575  INFO 14144 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8083 (http)
2019-01-15 10:26:21.607  INFO 14144 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-15 10:26:21.607  INFO 14144 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-15 10:26:21.622  INFO 14144 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_171\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\Docker\Docker\Resources\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files\Java\jdk1.8.0_171\bin;E:\RequiredTools\Maven\apache-maven-3.5.0\bin;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft VS Code\bin;C:\Users\acer\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\acer\AppData\Local\Programs\Python\Python37-32\;C:\Users\acer\.windows-build-tools\python27\;C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\acer\AppData\Roaming\npm\node_modules\windows-build-tools\node_modules\.bin;C:\Users\acer\AppData\Roaming\npm\node_modules\.bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files\Java\jdk1.8.0_171\bin;E:\RequiredTools\Maven\apache-maven-3.5.0\bin;C:\Users\acer\AppData\Local\Microsoft\WindowsApps;C:\Users\acer\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;C:\Users\acer\AppData\Local\Android\Sdk\platform-tools;C:\Users\acer\AppData\Local\Android\Sdk\tools;C:\Users\acer\AppData\Local\Microsoft\WindowsApps;C:\Program Files\PostgreSQL\10\bin;;C:\Users\acer\Downloads;;.]
2019-01-15 10:26:21.754  INFO 14144 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-15 10:26:21.755  INFO 14144 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2156 ms
2019-01-15 10:26:22.025  INFO 14144 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-15 10:26:22.196  INFO 14144 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2019-01-15 10:26:22.279  INFO 14144 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-01-15 10:26:22.430  INFO 14144 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8083 (http) with context path ''
2019-01-15 10:26:22.434  INFO 14144 --- [  restartedMain] com.test.product.manager.Application     : Started Application in 3.314 seconds (JVM running for 3.839)
2019-01-15 10:26:35.719  INFO 14144 --- [nio-8083-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-01-15 10:26:35.719  INFO 14144 --- [nio-8083-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-01-15 10:26:35.727  INFO 14144 --- [nio-8083-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms

1 个答案:

答案 0 :(得分:1)

如果要返回模板视图,则需要用<app-combo-box _ngcontent-c2="" placeholder="Client Name" _nghost-c4="" style=""> <input _ngcontent-c4="" type="text" placeholder="Client Name" maxlength="100" tabindex="2"> <ul _ngcontent-c4="" class="results_list" style="right: 43px; width: 382px; top: 32px;"> <!----> <li _ngcontent-c4="" class="selected"> <!----> <!----> <!----> <!----> <!----> <!----> <h4 _ngcontent-c2="">DAVID MIZRAHI LIVING TRUST</h4> <!----> <span _ngcontent-c2="">Tax ID: 55-4043677</span> <!----> <!----> <!----> <!----> </li> <li _ngcontent-c4="" class=""> <!----> <!----> <!----> <!----> <!----> <!----> <!----> Create Client Name <!----> <!----> </li> </ul> </app-combo-box> 而不是GreetController来注释@Controller类。 要了解这些注释,只需搜索它们,就会有很多Controller or RestController

这样的答案。