使用JSP的简单Spring Boot Hello世界示例

时间:2017-05-16 07:28:29

标签: java spring maven spring-mvc spring-boot

测试Spring Boot应用时遇到问题。无论我试图做什么,它都不会因某种原因而起作用。考虑到嵌入式Tomcat和JSP编译问题,甚至添加了另一个编译器。但没有工作。

表面上可能存在错误。

的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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</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>1.8</java.version>
</properties>

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

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

    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

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

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


</project>

SpringServletInitializer

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
}

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

和控制器

@Controller
public class BootController {
private String message = "Hello World!";

@RequestMapping("/")
public String welcome(ModelMap model) {
    model.put("message", this.message);
    return "index";
}
}

application.properties

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

访问localhost会显示whitelabel错误页面。

project structure

更新

当我尝试使用mvn spring-boot:repackage重新打包项目时,我收到以下错误日志:

[INFO] BUILD FAILURE
[INFO] ---------------------------------------------------------------------
---
[INFO] Total time: 5.172 s
[INFO] Finished at: 2017-05-16T14:34:36+06:00
[INFO] Final Memory: 16M/153M
[INFO] ---------------------------------------------------------------------
---
 [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-
plugin:1.5.3.RELEASE:repackage (default-cli) on project demo: Execution 
default-cli of goal org.springframework.boot:spring-boot-maven-
plugin:1.5.3.RELEASE:repackage failed: Source must refer to an existing file 
-> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e 
switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please 
read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

还在想它会是什么。我希望这是一个重新包装问题,我无法真正跟踪,因为无法对项目进行任何更改。但是,如果我创建@RestController并只返回字符串,它可以正常工作。

2 个答案:

答案 0 :(得分:0)

如果您在嵌入式tomcat中运行,请尝试以下代码:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>

答案 1 :(得分:0)

试试这样:

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

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