簡體   English   中英

使用JSP的簡單Spring Boot Hello World示例

[英]Simple Spring Boot Hello World Example Using JSP

測試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

訪問本地主機將顯示whitelabel錯誤頁面。

項目結構

UPDATE


當我嘗試使用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並僅返回字符串,則可以正常工作。

如果您在嵌入式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>

嘗試這樣:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM