1. 程式人生 > >spring boot-jsp專案main啟動出現process finished with exit code 0 錯誤

spring boot-jsp專案main啟動出現process finished with exit code 0 錯誤

百度有人說是因為pom.xml中缺少web的依賴,但是我pom.xml是有這個web的依賴的,而以前的話,這個專案是可以啟動的,但現在卻無法啟動,只能是用war的方式部署到tomcat才行,可是我需要的是用main的方式一鍵啟動。

然後突然想到了spring boot已經出到了2.0了,我想會不會是版本過老了,版本號是1.4.7;

我就只發成功的demo吧,有需要的可以參考一下,不當之處歡迎批評指點.

啟動器:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication
; @SpringBootApplication public class ApplicationTest { public static void main(String[] args) { SpringApplication.run(ApplicationTest.class, args); } }

application.properties:

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

controller:

import org.springframework.stereotype.Controller
; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @RequestMapping("/jsp") @Controller public class JspTestController { @RequestMapping("/test") public ModelAndView test() { //檢視需要根據application.properties中配置的路徑下存在 ModelAndView mv = new
ModelAndView("test"); mv.addObject("msg", "資料來自後臺..."); return mv; } }

jsp(注意路徑:WEB-INF/jsp/test.jsp):

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${msg }</body>
</html>

pom.xml:

<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
   <groupId>cn.itcast.springboot</groupId>
   <artifactId>spring-boot-04-view-jsp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <!-- 配置jdk版本 -->
<properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <!-- web應用啟動器 -->
<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>

      <!-- servlet依賴. -->
<dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>
         <scope>provided</scope>
      </dependency>

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

      <!-- 使用引導類啟動時候也可以訪問jsp檢視;不然只能打出war部署到tomcat下才能訪問 -->
<dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
      </dependency>

   </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>