Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧启动404未找到错误_Java_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

Java 弹簧启动404未找到错误

Java 弹簧启动404未找到错误,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我正在尝试运行spring启动应用程序,但遇到404 not found错误 项目结构: src/ +- main/ +- java/ | + com/ | + demo/ | SpringBootDemo.java | + controller/ | HomeController.java +- resources/

我正在尝试运行spring启动应用程序,但遇到404 not found错误

项目结构:

src/
 +- main/
     +- java/
     |   + com/
         |   + demo/
             |    SpringBootDemo.java
             |    + controller/
                  |    HomeController.java
     +- resources/
             |   application.yml


src/
 +- main/
     +- webapp/
        +- WEB-INF/
           +- pages/
              | home.jsp
HomeController.java

package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String getHome() {
        System.out.println("Controller");
        return "home";
    }
}
application.yml

server:
  port: 8080
spring:
  mvc:
    view:
      prefix: /WEB-INF/pages/
      suffix: .jsp
格雷德尔先生

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
mainClassName = 'com.demo.SpringBootDemo'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

// In this section you declare where to find the dependencies of your project
repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('org.springframework.boot:spring-boot-starter-parent')
    compile('javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api')
}

jar {
  manifest {
    attributes(
      'Main-Class': 'com.demo.SpringBootDemo'
    )
  }
}
SpringBootDemo.java

package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(scanBasePackages="com.demo")
public class SpringBootDemo {

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

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    Hello World
</body>
</html>
但是当我访问更新时,它会显示
白标错误页面
好的,我会努力做得更好。我不是有意要伤害你

我认为您需要从
SpringBootServletInitializer
扩展springbootsapplication类,以便在您的springboot应用程序中获得servlet功能

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebApplication.class, args);
    }
}
为了能够解析视图,必须定义以下属性:

spring.mvc.view.prefix: /WEB-INF/pages
spring.mvc.view.suffix: .jsp
或者当然是它们的
yaml
变体。然后,实际的jsp页面需要放在src/main/webapp/WEB-INF/pages中

这些是我的pom中的依赖项

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

希望这将有助于您的应用程序正常运行…

使用maven spring boot运行目标:
spring boot:Run

在IntelliJ中设置maven配置的步骤:


调试/运行配置|单击左上角可见的+按钮|选择Maven |将命令行设置为
spring boot:Run

此链接提供了有关REST web服务的教程,但我一直在关注web应用程序的问题。这个链接没有给出答案,因为我已经搜索了教程,但仍然停留在这个问题上。请让我知道我哪里做错了。对不起,伙计们,我不是想给出一个糟糕的答案。希望这一个会做得更好…在build.gradle文件中添加
compile(“org.springframework.boot:spring boot starter thymeleaf”)
并将home.html放在resource/templates文件夹中后,我让它工作起来了。另外,将tomcat嵌入jasper依赖项的范围从提供更改为编译可能会有所帮助。为我工作!你读过这个吗?如何构建和运行应用程序?我没有看到任何war插件应用于gradle构建,因此您可能忽略了文档中的这一重要警告`
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
@Controller
public class HelloController {
    @RequestMapping("/")
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}