SpringBoot-HelloWorld

时间:2018-07-14 13:30:49

标签: java spring spring-boot

我想用SpringBoot创建一个简单的hello world应用程序,其中localhost:8080 / welcome.html将向我们显示Hello World。

我认为我做得很好,但看不到HelloWorld,只有Whitelabel错误页面。

这是我的仓库的链接。如果有人可以检查出什么毛病,我将非常高兴! https://github.com/BElluu/ElenXHello

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>

    <groupId>com.springbelluu</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>10</java.version>
    </properties>

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

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

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>

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

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


</project>

SpringbootHelloworldApplication.java

package com.springbelluu.springboothelloworld;

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

@SpringBootApplication
public class SpringbootHelloworldApplication {

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

TestController.java

package com.javainuse.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @RequestMapping("/welcome.html")
    public ModelAndView firstPage() {
        return new ModelAndView("welcome");
    }

}

application.properties

spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp

5 个答案:

答案 0 :(得分:0)

@BElluu ...您的主应用程序和Controller类位于不同的程序包中,因此组件可能无法正常工作...只需检查程序包级别

package com.springbelluu.springboothelloworld;

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

@SpringBootApplication
public class SpringbootHelloworldApplication {

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


package com.javainuse.controllers;  
 // use this package package com.springbelluu.springboothelloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

    @RequestMapping("/welcome.html")
    public ModelAndView firstPage() {
        return new ModelAndView("welcome");
    }

}

答案 1 :(得分:0)

通过Spring Framework转到以下链接以创建全新的Springboot项目:

https://start.spring.io/

1. 创建一个简单的Web应用程序

现在您可以为一个简单的Web应用程序创建一个Web控制器。

src / main / java / hello / HelloController.java

包裹你好;

导入org.springframework.web.bind.annotation.RestController; 导入org.springframework.web.bind.annotation.RequestMapping;

@RestController 公共类HelloController {

@RequestMapping("/")
public String index() {
    return "Greetings from Spring Boot!";
}

}

  1. 创建应用程序类

    Here you create an Application class with the components:
    
    src/main/java/hello/Application.java
    
     package hello;
    
     import java.util.Arrays;
     import org.springframework.boot.CommandLineRunner;
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
     import org.springframework.context.ApplicationContext;
     import org.springframework.context.annotation.Bean;
    
     @SpringBootApplication
     public class Application {
    
     public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
      }
    
      @Bean
      public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
         return args -> {
        System.out.println("Let's inspect the beans provided by Spring Boot:");
    
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
             }
          };
        }
      }
    

答案 2 :(得分:0)

对于初学者,如果您想使用Spring Boot入门,我强烈建议使用JSP。使用JSP时有相当多的some limitations,其中之一是它不适用于jar包装。其次,它是一种过时的技术,除了在新的JEE版本中保持功能之外,再也没有受到太多关注/更新。最好使用Thymeleaf之类的东西。

接下来,您将快照版本用于已经在2.1.3.RELEASE(在撰写本文时)的Spring Boot版本中。

话虽这么说,将pom.xml更改为以下版本(修复版本,删除JSP内容并替换为Thymeleaf)。

<?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>

    <groupId>com.springbelluu</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>10</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-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>

注意::由于您现在使用的是最终版本,因此不再需要pom.xml中的所有存储库!。

现在删除您的JSP并在welcome.html中创建一个src/main/resources/templates/。 (您实际上可以完全删除webapp目录。

<html>
<body>
<h1>Welcome! Spring Boot for ElenX</h1>
</body>
</html>

您现在拥有的设置比JSP更现代,更易于使用。

在您的application.properties中删除spring.mvc.view属性,因为Spring Boot将使用正确的设置自动配置Thymeleaf。

答案 3 :(得分:0)

   Here is SpringBoot Main class file Noting Changed Here.  
    -----------------------------------------

    package com.Encee.SpringBoot_HelloWorld;

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

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

     }

    Here is controller package class 
    -----------------------------------------
    package com.Encee.SpringBoot_HelloWorld.controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController

    public class Controller {
        @RequestMapping("/hello.html")
       public String hello() {
            return "Hello World";
        }
    }

    -----------------------------------------        
    Now Main file run as Java Application will get your output.
     SCREEN SHOT

答案 4 :(得分:-1)

您的问题已得到解决,您可以在提交时查看更改。

https://github.com/farooqrahu/ElenXHello/commits/master