【问题标题】:Unable to debug web application using Eclipse无法使用 Eclipse 调试 Web 应用程序
【发布时间】:2015-09-29 03:31:55
【问题描述】:

我已经密切关注了几个教程,但我无法开始使用最简单的应用程序。这是我目前所拥有的。

我用 Eclipse 启动了一个动态 Web 应用程序,并将它做成了一个 Maven 项目。

这是项目结构:

我的 Maven pom 是:

<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>
  <groupId>HappySchedulerSpring</groupId>
  <artifactId>HappySchedulerSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
     <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>1.2.5.RELEASE</version>
    </plugin>
    </plugins>
  </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.2.5.RELEASE</version>
        </dependency>
    </dependencies>
        <repositories>
        <repository>
            <id>spring-milestone</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

Application.java 和 TweetsController.java 也很基础(直接来自 Spring 示例

package com.pistacchioso.happyscheduler;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

和控制器:

package com.pistacchioso.happyscheduler;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@EnableAutoConfiguration
public class TweetsController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "HappyScheduler";
    }

}

.jsp 文件中也没有什么特别之处:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

当我单击调试图标时,Eclipse 让我选择 Tomcat 8,没有报错,依赖项已正确解析并启动服务器,但 http:localhost:8080/greeting 一直给我 404。

有什么帮助吗?

【问题讨论】:

  • 你能再检查一下端口号吗?
  • 端口号正确。事实上,是 Tomcat 给了我一个 404
  • 可能是网址拼错了?你能告诉我们控制台的最后几行吗?
  • 我猜 HappyScheduler.jsp 需要放在 src/main/resources/templates 文件夹中..

标签: java eclipse debugging tomcat


【解决方案1】:

首先引起我注意的是控制器中的@EnableAutoConfiguration。此注释旨在放在配置类上,在您的情况下为 Application。

但是你不需要它,因为你使用的是@SpringBootApplication。这是一个方便的注解,它假设包括自动配置在内的几件事。

尝试从控制器中删除 @EnableAutoConfiguration。

另外,你可以找到一个类似你的例子here

【讨论】:

    【解决方案2】:

    第一个spring boot内置了嵌入式tomcat,所以我们可以把它打包成jar,直接用jar运行

    mvn spring-boot:run 
    

    或者使用java

    java -jar target/SpringBootApplication.jar
    

    当我们使用 jar 文件打包时,WebApp 文件夹将被忽略。所以html文件需要放在src/main/resources/templates文件夹下。

    我已经在本地桌面实现并得到了结果

    pom 文件供您参考 - http://txs.io/jw6b

    【讨论】:

      猜你喜欢
      • 2018-10-01
      • 2012-08-04
      • 2012-12-15
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多