【问题标题】:Spring boot Application output not displayedSpring Boot 应用程序输出未显示
【发布时间】:2021-01-05 02:01:25
【问题描述】:

我是 Spring Boot 的初学者。我制作了简单的 crud 应用程序,但在编译项目时没有任何错误,但在浏览器中看不到输出。我不知道为什么。

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

只有这样的显示无法在浏览器中看到输出。我不知道为什么。

员工控制器如下所示

@Controller
public class employeeController {   
    @Autowired
    private employeeservice service;    
    @RequestMapping("/")
    public String viewHomePage(Model model) {
        List<employee> listemployee = service.listAll();
        model.addAttribute("listemployee", listemployee);
        
        return "index";
    }   
    @RequestMapping("/new")
    public String showNewEmployeePage(Model model) {
        employee emp = new employee();
        model.addAttribute("employee", emp);
        
        return "new_employee";
    }   

Index.html

<tbody>
    <tr th:each="employee : ${listemployee}">
        <td th:text="${employee.id}">Employee ID</td>
        <td th:text="${employee.firstname}">FirstName</td>
        <td th:text="${employee.brand}">LastName</td>

        <td>
            <a th:href="@{'/edit/' + ${employee.id}}">Edit</a>
            &nbsp;&nbsp;&nbsp;
            <a th:href="@{'/delete/' + ${employee.id}}">Delete</a>
        </td>
    </tr>
</tbody>

2020-09-18 13:05:15.081  INFO 10660 --- [           main] com.example.gov.GovApplication           : Starting GovApplication on kobinath-pc with PID 10660 (C:\Users\kobinath\eclipse-workspace1s\gov.zip_expanded\gov\target\classes started by kobinath in C:\Users\kobinath\eclipse-workspace1s\gov.zip_expanded\gov)
2020-09-18 13:05:15.085  INFO 10660 --- [           main] com.example.gov.GovApplication           : No active profile set, falling back to default profiles: default
2020-09-18 13:05:15.993  INFO 10660 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-09-18 13:05:16.082  INFO 10660 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 73ms. Found 1 JPA repository interfaces.
2020-09-18 13:05:17.018  INFO 10660 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9040 (http)
2020-09-18 13:05:17.031  INFO 10660 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-09-18 13:05:17.031  INFO 10660 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-09-18 13:05:17.184  INFO 10660 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-09-18 13:05:17.184  INFO 10660 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2015 ms
2020-09-18 13:05:17.468  INFO 10660 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-18 13:05:17.532  INFO 10660 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-09-18 13:05:17.643  INFO 10660 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-09-18 13:05:18.072  INFO 10660 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-09-18 13:05:18.094  INFO 10660 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page template: index
2020-09-18 13:05:18.301  INFO 10660 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-09-18 13:05:18.410  INFO 10660 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9040 (http) with context path ''
2020-09-18 13:05:18.412  INFO 10660 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-09-18 13:05:18.615  INFO 10660 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-09-18 13:05:18.657  INFO 10660 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
2020-09-18 13:05:19.546  INFO 10660 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-09-18 13:05:19.558  INFO 10660 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-09-18 13:05:19.836  INFO 10660 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-09-18 13:05:19.849  INFO 10660 --- [           main] com.example.gov.GovApplication           : Started GovApplication in 5.297 seconds (JVM running for 5.845)

porm.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 https://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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>gov</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gov</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-data-jpa</artifactId>
        </dependency>
        <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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

spring.jpa.hibernate.ddl-auto=none

spring.datasource.url=jdbc:mysql://localhost:3306/gov?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC server.error.whitelabel.enabled=false

server.port=8030
spring.datasource.username=root
spring.datasource.password=
logging.level.root=WARN
spring.jpa.open-in-view=false

【问题讨论】:

  • 您的代码看起来不错。这应该工作。您是否将记录器相关属性添加到您的 application.properties 文件中?
  • application.properties 我在上面添加请检查先生
  • 从您的 application.properties 文件中删除这一行 logging.level.root=WARN 。由于这个 DEBUG 和 INFO 级别的日志对您不可见
  • 删除后,我收到了我在上面附加的消息,请检查是否有任何错误
  • 您的日志中没有错误

标签: spring spring-boot


【解决方案1】:

我相信您在 Enity 和模板之间犯了一些拼写错误。我希望这个 repo 能帮到你

https://github.com/toannh0104/spring-boot-test

我更喜欢你按照 java 标准编写代码,那么它更具可读性

https://imgur.com/wrTWBp9

https://imgur.com/ct7k2uI

https://imgur.com/iw5bS0c

【讨论】:

  • localhost:8070 没有显示 localhost:8070/employee 也 HTTP 状态 404 – 未找到
  • 啊,您可以在班级级别删除该映射@RequestMapping("/employee"),我设置了因为它与我的模式发生冲突
  • 如果我把它运行localhost:8020/employee 就像显示这个错误一样配置的模板解析器
  • 删除@RequestMapping("/employee")后 -> http://localhost:&lt;port&gt;/
  • localhost:8021ran 无法访问此站点
猜你喜欢
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多