我有一个简单的spring boot应用程序,但除了从目标/构建文件夹运行外,它不工作。
这是我所有的配置和文件。请帮我找出为什么它不起作用,以及我做错了什么。
当我从目标文件夹java-jar目标\demo-0.0.1-SNAPSHOT
运行并从IDE(Eclipse)运行时,它工作得非常好。
我的网址是http://localhost:8082/demo/edit.html
。我只是把这个罐子复制到不同的机器/文件夹,它不再工作了。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DemoContoller {
@RequestMapping("/edit.html")
public ModelAndView edit(HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {
ModelAndView model = new ModelAndView("edit");
return model;
}
}
package com.example.demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
}
}
application.yaml
spring:
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
output:
ansi:
enabled: ALWAYS
server:
servlet:
contextPath: /demo
port : 8082
error:
whitelabel:
enabled: false
**edit.jsp**
<% request.setCharacterEncoding("UTF-8"); %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<body>
helllloooooo Demo
</body>
</html>
**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 https://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>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
我只是运行mvn clean package spring boot:repackage
来构建项目。
这是我的项目/文件夹结构:
Maven构建:
$ mvn clean package spring-boot:repackage
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ demo ---
[INFO] Deleting C:\Users\User\Downloads\demo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 3 source files to C:\Users\User\Downloads\demo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\User\Downloads\demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo ---
[INFO] Building jar: C:\Users\User\Downloads\demo\target\demo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.6.RELEASE:repackage (default-cli) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.201 s
[INFO] Finished at: 2020-04-22T15:52:06+08:00
[INFO] ------------------------------------------------------------------------
更新了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 https://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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
已从应用程序中删除更新。yaml,我现在看到了这个错误页面。
error:
whitelabel:
enabled: false
编辑html可能是有问题的部分。你能移除吗。html,只需“/编辑”,然后重试”http://localhost:8082/demo/edit“。如果这样做有效,我们将理解这是由于那里的文件扩展名。
我的spring boot mvc应用程序也有类似的问题。当我使用“mvn spring boot:run”或“java-jar target/myapplication.jar”(注意target/)运行时,应用程序运行良好。当我尝试在下面的场景中使用“java-jar myapplication.jar”时,它从未起作用,
控制/请求到达服务器并得到处理,但未能重定向到视图(404-未找到)。
作为一种变通方法,我在pom中将包装从jar改为war。xml和它开始工作(根据我的要求)。
人们可以引爆罐子/战争,发现罐子和战争的包装是不同的。
希望这有帮助。编码快乐!
谢谢
我一直在使用java后端,它一直工作正常,直到昨天我和另一个人都开始收到以下错误消息: 无法解析配置“:compileClasspath”的所有文件。无法解析组织。springframework:SpringBeans:[2.5.2,3]。要求者:项目: 我对gradle有点熟悉,但只与android有关,而不是java/spring,以前从未遇到过这种错误。
我有一个文件夹,我想完全删除的文件: 但是我得到了: 如你所见:这是正确的道路 我的错在哪里??
我正试图通过以下文章将我的angular应用程序转换为使用cordova的android应用程序-https://medium.com/@nacojohn/convert-your-angular-project-to-mobile-app-using-cordova-f0384a7711a6 当我尝试执行最后一步,即“cordova build android”时,出现以下错误: 失败:生成失败
问题内容: 我想知道到底是什么和和做的。请通过解释进行澄清。 下面的3条命令运行并输出相同的结果,但是第一个命令花费一些时间,格式也几乎没有什么不同。 这是因为第1个命令针对来自该命令的每个文件运行该命令。因此,基本上它的运行方式为: 但是后2个命令查找命令对所有文件运行一次file命令,如下所示: 然后,我运行以下命令,第一个在没有问题的情况下运行,但第二个给出错误消息。 对于带有的命令,它会给
我想镜像两个文件夹,但排除某些文件类型(例如js文件)。我可以用 /XF选项来做到这一点。 我遇到的问题是,如果目标文件夹中有一个带有js文件的Foo文件夹,但源文件夹中没有Foo文件夹,那么下面的robocopy命令将删除目标端的Foo文件夹以及其中的js文件。 机器人复制。exe d:\Source d:\Destination/E/PURGE/XF*。js/COPYALL/FFT 我想要的是
我尝试在Ubuntu11.02的命令行上运行ant build.xml中的javac。然后运行“ant deploy-live”之后,命令行打印如下: 我关于javac的任务: 操作系统:Ubuntu 11.10(oneiric) Java版本:Java版本“1.7.0”Java(TM)SE运行时环境(build 1.7.0-B147)Java HotSpot(TM)64位服务器VM(build