gpt4 book ai didi

java - Spring Boot可执行jar无法访问索引页

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:22 25 4
gpt4 key购买 nike

我使用 springboot、restapi 和 angularjs 创建了一个应用程序。我将把这个项目作为可执行的jar。但是当我执行 java -jar patchinit.jar 时,它不会重新加载索引页。它给出了

Whitelabel Error Page (There was an unexpected error (type=Not Found, status=404 No message available)

尝试 http://localhost:8080/

我错过了什么吗?我几乎绝望地尝试了一切..

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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ama.ist</groupId>
<artifactId>patchinit</artifactId>
<packaging>jar</packaging>
<version>0.0.1</version>
<name>patchinit Maven Webapp</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>


<properties>
<java.version>1.7</java.version>
</properties>

<dependencies>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<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>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!-- <scope>provided</scope> -->
</dependency>

<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.7.8</version>
</dependency>

</dependencies>

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

</plugins>
</build>
</project>

Webapplication.java

package com.ama.ist;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}

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

}

HelloController.java

package com.ama.ist.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

@RequestMapping("/")
String index() {
return "index";
}

}

index.html

<!DOCTYPE html>
<html ng-app="MyApp">
<head>

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.5/angular-material.min.css">



</head>
<body>

<div class="container">

<div class="row">

<div class="col-md-4 jumbotron">
<div class="col-md-12 dialog-demo-content">
<div ui-view="newPatch"></div>
</div>
</div>


<div class="col-md-8 ">

<div ui-view="svntab"></div>

</div>

</div>
</div>



<!-- <div class="container" class="md-padding" ng-cloak> -->
<!-- <div class="row"> -->
<!-- <div class="col-md-3 dialog-demo-content"> -->
<!-- <div ui-view="newPatch"></div> -->
<!-- </div> -->

<!-- <div class="col-md-2"></div> -->



<!-- <div class="col-md-3"> -->
<!-- <div ui-view="svntab"></div> -->
<!-- </div> -->
<!-- </div> -->
<!-- </div> -->




<!-- ************************* SCRIPTS ********************************** -->
<script src="https://code.angularjs.org/1.5.8/angular.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>

<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">

</script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>


<script src="js/app.js"></script>
<script src="js/patch.controller.js"></script>
<script src="js/svn.controller.js"></script>


</body>

</html>

这是springboot的输出

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.5.RELEASE)

2017-12-05 19:54:09.019 INFO 480 --- [ main] com.ama.ist.WebApplication : Starting WebApplication on ISTL55310 with PID 480 (C:\Erkan\eclipse-workspace-new\patchinit\target\classes started by erkan.erkisi in C:\Erkan\eclipse-workspace-new\patchinit)
2017-12-05 19:54:09.022 INFO 480 --- [ main] com.ama.ist.WebApplication : No active profile set, falling back to default profiles: default
2017-12-05 19:54:09.063 INFO 480 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1ce2a846: startup date [Tue Dec 05 19:54:09 EET 2017]; root of context hierarchy
2017-12-05 19:54:10.335 INFO 480 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-05 19:54:10.345 INFO 480 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-12-05 19:54:10.347 INFO 480 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.33
2017-12-05 19:54:10.987 INFO 480 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017-12-05 19:54:10.994 INFO 480 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-05 19:54:10.994 INFO 480 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1933 ms
2017-12-05 19:54:11.468 INFO 480 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-05 19:54:11.471 INFO 480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-05 19:54:11.639 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1ce2a846: startup date [Tue Dec 05 19:54:09 EET 2017]; root of context hierarchy
2017-12-05 19:54:11.697 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.ama.ist.controller.HelloController.index()
2017-12-05 19:54:11.698 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/resource]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.ama.ist.controller.PatchController.home()
2017-12-05 19:54:11.698 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/mk],methods=[POST]}" onto public org.springframework.http.ResponseEntity<?> com.ama.ist.controller.PatchController.createFolder(com.ama.ist.model.Patch)
2017-12-05 19:54:11.701 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/localfolders],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.ama.ist.model.Folder>> com.ama.ist.controller.SvnController.getlocalFolders(com.ama.ist.model.User)
2017-12-05 19:54:11.702 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/svnfolders],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.ama.ist.model.Folder>> com.ama.ist.controller.SvnController.getFolders(com.ama.ist.model.User)
2017-12-05 19:54:11.703 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-05 19:54:11.704 INFO 480 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-05 19:54:11.716 INFO 480 --- [ main] o.s.w.s.c.a.WebMvcConfigurerAdapter : Adding welcome page: ServletContext resource [/index.html]
2017-12-05 19:54:11.736 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
2017-12-05 19:54:11.745 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-05 19:54:11.745 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-05 19:54:11.778 INFO 480 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-05 19:54:11.878 INFO 480 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-05 19:54:11.945 INFO 480 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-05 19:54:11.949 INFO 480 --- [ main] com.ama.ist.WebApplication : Started WebApplication in 3.254 seconds (JVM running for 3.827)

Project Structure as an image

最佳答案

终于找到解决办法了。我通过添加 thymeleaf 解决了这个问题pom 中的依赖

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

什么是百里香叶?

Thymeleaf是一个Java库。这是一个XML/XHTML/HTML5模板引擎能够对模板文件应用一组转换,以便显示应用程序生成的数据和/或文本。

您的 html 文件中需要进行一处更正:

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.5/angular-material.min.css">

确保关闭此标签<link>带结束标签</link> 。否则你会得到下面的错误

There was an unexpected error (type=Internal Server Error, status=500). Exception parsing document: template="index", line 12 - column 3

项目结构

enter image description here

关于java - Spring Boot可执行jar无法访问索引页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47660049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com