In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "does SpringBoot 2.x support jsp deployment using jar". In daily operation, I believe many people have doubts about whether SpringBoot 2.x supports jsp deployment using jar. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "does SpringBoot 2.x support jsp deployment using jar?" Next, please follow the editor to study!
1 pom.xml file modification
1 increase tomcat jsp dependency
Javax.servlet javax.servlet-api provided javax.servlet.jsp javax.servlet.jsp-api 2.3.1 javax.servlet jstl Org.springframework.boot spring-boot-starter-tomcat provided org.apache.tomcat.embed tomcat-embed-jasper 9.0.27 provided
2 modify resource packaging configuration
Src/main/webapp META-INF/resources * * / * * src/main/resources * / *
Second project catalogue
Add directory src/main/webapp, and jsp related files can be added to the directory
Add to application.properties
Spring.mvc.view.prefix=/WEB-INF/views/
Spring.mvc.view.suffix=.jsp
# support jsp debugger
Server.servlet.jsp.init-parameters.development=true
Third, modify the code
Add conversion configuration
StaticResourceConfigurer.java
Package com.vipkid.configuration;import java.io.File;import java.net.MalformedURLException;import java.net.URL;import org.apache.catalina.Context;import org.apache.catalina.Lifecycle;import org.apache.catalina.LifecycleEvent;import org.apache.catalina.LifecycleListener;import org.apache.catalina.WebResourceRoot.ResourceSetType;import org.springframework.util.ResourceUtils;/** * Resource path conversion configuration * Add main class fat jar/exploded directory into tomcat ResourceSet. * * @ author zouqinghua * @ date 9:44:34 on October 30th 2019 * * / public class StaticResourceConfigurer implements LifecycleListener {private final Context context; StaticResourceConfigurer (Context context) {this.context = context } @ Override public void lifecycleEvent (LifecycleEvent event) {if (event.getType () .equals (Lifecycle.CONFIGURE_START_EVENT)) {URL location = this.getClass () .getProtectionDomain () .getCodeSource () .getLocation () If (ResourceUtils.isFileURL (location)) {/ / when run as exploded directory String rootFile = location.getFile () If (rootFile.endsWith ("/ BOOT-INF/classes/")) {rootFile = rootFile.substring (0, rootFile.length ()-"/ BOOT-INF/classes/" .length () + 1) } if (! new File (rootFile, "META-INF" + File.separator + "resources") .isDirectory () {return } try {location = new File (rootFile). ToURI (). ToURL ();} catch (MalformedURLException e) {throw new IllegalStateException ("Can not add tomcat resources", e) }} String locationStr = location.toString () If (locationStr.endsWith ("/ BOOTHUBUINFHANG classeship /")) {/ / when run as fat jar locationStr = locationStr.substring (0, locationStr.length ()-"/ BOOTMUINFUBG classeship /" .length () + 1) Try {location = new URL (locationStr);} catch (MalformedURLException e) {throw new IllegalStateException ("Can not add tomcat resources", e) This.context.getResources () .createWebResourceSet (ResourceSetType.RESOURCE_JAR, "/", location, "/ META-INF/resources");}
Add listening class TomcatConfiguration.jsp
Package com.vipkid.configuration;import org.apache.catalina.Context;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory;import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;import org.springframework.boot.web.server.WebServerFactory;import org.springframework.boot.web.server.WebServerFactoryCustomizer;import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered Import org.springframework.core.annotation.Order / * configure addLifecycleListener * @ author zouqinghua * @ date 9:45:28 on October 30, 2019 * * / @ Order (Ordered.HIGHEST_PRECEDENCE) @ Configuration@ConditionalOnProperty (name = "tomcat.staticResourceCustomizer.enabled" MatchIfMissing = true) public class TomcatConfiguration {/ * SpringBoot 1.x configuration @ return @ Bean public EmbeddedServletContainerCustomizer staticResourceCustomizer () {return new EmbeddedServletContainerCustomizer () {@ Override public void customize (ConfigurableEmbeddedServletContainer container) { If (container instanceof TomcatEmbeddedServletContainerFactory) {((TomcatEmbeddedServletContainerFactory) container) .addContextCustomizers (new TomcatContextCustomizer () {@ Override) Public void customize (Context context) {context.addLifecycleListener (new StaticResourceConfigurer (context)) );}} } * / * SpringBoot 2.x configuration * @ return * / @ Bean public WebServerFactoryCustomizer webServerFactoryCustomizerBean () {return new WebServerFactoryCustomizer () {@ Override public void customize (ConfigurableTomcatWebServerFactory factory) ) {factory.addContextCustomizers (new TomcatContextCustomizer () {@ Override public void customize (Context context) { Context.addLifecycleListener (new StaticResourceConfigurer (context)) });}} }}
Four codes
IndexController and
Src/main/webapp/WEB-INF/views/hello.jsp
Package com.smc.sys.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController {@ RequestMapping ("/ hello") public String index () {System.out.println ("index = > >"); return "hello";} Hello World-${name}
Five, pack and start.
Mvn clean package-Dmaven.test.skip=true
Java-jar target/xxx.jar
Six visits
Http://localhost:8080/hello
At this point, the study on "does SpringBoot 2.x support jsp deployment using jar?" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.