[英]How to deploy a SpringBoot webapp: war or jar?
A Spring Boot server application is fairly simple to set up using Maven. 使用Maven设置Spring Boot服务器应用程序非常简单。 But I can't seem to figure out why I'm having trouble using it for a webapp and I can't find clear documentation on how to do what I am trying to. 但是我似乎无法弄清楚为什么在将其用于Web应用程序时会遇到麻烦,并且找不到关于如何执行自己想做的事情的清晰文档。 Perhaps, I'm trying to fit a square peg into a round hole. 也许,我正在尝试将方形钉插入圆孔中。
Normally, one would build a war and deploy it into a standalone started Tomcat instance. 通常,人们会发动战争并将其部署到独立启动的Tomcat实例中。 My understanding is that SpringBoot let's you have an embedded tomcat instance. 我的理解是,SpringBoot让您拥有一个嵌入式的tomcat实例。 Then you start the server from the jar using the java -jar /path/to/target/springbootapp.jar
然后,使用java -jar /path/to/target/springbootapp.jar
从jar启动服务器
But when I add webapp contents, how do I deploy it? 但是,当我添加webapp内容时,如何部署它? Do I have to change my <packaging>jar</packaging>
to <packaging>war</packaging>
? 我必须将我的<packaging>jar</packaging>
更改为<packaging>war</packaging>
吗? or can I still use the jar? 还是我可以继续使用罐子? If I do change it war, then how do I start it? 如果我确实改变了战争,那我该如何开始呢?
I guess you already found answer through above comments and by yourself. 我想您已经通过上面的评论和自己找到了答案。 Here some summary 这里有一些总结
Packaging should be war on pom.xml to include static file under WEB-INF. 打包应该对pom.xml采取战争,以将静态文件包含在WEB-INF下。
Below is just example 以下仅是示例
pom.xml 的pom.xml
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery-ui-themes</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>datatables</artifactId>
<version>1.10.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>datatables-plugins</artifactId>
<version>ca6ec50</version>
</dependency>
You can run from command line also with specifying launcher (JarLauncher / WarLauncher) 您还可以通过指定启动器从命令行运行(JarLauncher / WarLauncher)
command line 命令行
java -cp your-target.war;some-dependency.jar org.springframework.boot.loader.WarLauncher
Also you can configure the path of resource like webjars by setting up resourceHandlerRegistry like below 您还可以通过如下设置resourceHandlerRegistry来配置资源(如webjars)的路径
WebConfig.java WebConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.your.spring.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/images/**")) {
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
}
if (!registry.hasMappingForPattern("/css/**")) {
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
}
if (!registry.hasMappingForPattern("/js/**")) {
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
}
}
@Bean
public InternalResourceViewResolver internalViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
viewResolver.setOrder(1);
return viewResolver;
}
}
Now your jsp file can have the path for the static content inside webjar like below 现在,您的jsp文件可以具有webjar中静态内容的路径,如下所示
On jsp file 在jsp文件上
<link rel="stylesheet" type="text/css" href="webjars/jquery-ui-themes/1.11.3/smoothness/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="webjars/datatables/1.10.5/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="webjars/datatables-plugins/ca6ec50/integration/foundation/dataTables.foundation.css">
<script type="text/javascript" src="webjars/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/jquery-ui/1.11.3/jquery-ui.js"></script>
<script type="text/javascript" src="webjars/datatables/1.10.5/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="webjars/datatables-plugins/ca6ec50/integration/foundation/dataTables.foundation.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.