In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how SpringBoot uses JSP in the project". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Project structure after adding JSP support
Compared with the previous project structure main directory under the more webapp directory, used to store directory jsp files.
Spring-boot-jsp +-src +-main +-java +-resources +-webapp +-WEB-INF +-jsp +-welcome.jsp +-test +-pom.xml2. You need to specify the location and suffix spring.mvc.view.prefix: / WEB-INF/jsp/spring.mvc.view.suffix: .jsp3. Jsp in the configuration file. Introduce POM dependency
The spring-boot-starter-web package depends on spring-boot-starter-tomcat and does not need to be configured separately
Org.springframework.boot spring-boot-starter-web javax.servlet jstl org.apache.tomcat.embed tomcat-embed-jasper4. Write the jsp page Time: ${time}
Message: ${message} 5. Write background code @ Controllerpublic class WelcomeController {@ GetMapping ("/") public String welcome (Map model) {model.put ("time", new Date ()); model.put ("message", "hello world"); return "welcome";}} 6. Launch and access the page
Cd goes to the project root directory
Cd...\ spring-boot-jsp
Execute the following startup command
Mvn clean spring-boot:run
After startup is complete, access http://localhost:8080/ in the browser
Time: Sat Aug 11 13:26:35 CST 2018 Message: hello world7. Debugging and deployment
If you start the project directly through the main method in IDEA, as with other projects, 404 not found will appear when you access the test.
This is because the Spring Boot JSP project requires an additional setting: select the Edit Configurations option and open Run/Debug Configurations:
Set the path of Working directory to the project root path:
Then restart the project and you can access the page normally.
Run in a separate tomcat
Set the packaging format to war in pom.xml.
War
Eliminate embedded Tomcat dependencies; eliminate embedded Tomcat dependencies when packaging to avoid jar package conflicts
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat
Support for Servlet
The SpringBoot project must implement the configure () method of the SpringBootServletInitializer interface in order for the external container to run the SpringBoot project and start to create the ServletInitializer class in the same directory:
Public class ServletInitializer extends SpringBootServletInitializer {@ Override protected SpringApplicationBuilder configure (SpringApplicationBuilder application) {return application.sources (JspApplication.class);}}
Package the release and execute the maven command in the project root directory:
Mvn clean package
Publish war packages to Tomcat
8. Summary
Spring Boot supports running JSP; using embedded Tomcat as well as packaging the project as a War package and deploying it into a separate Tomcat. In actual projects, it is recommended to use a separate Tomcat to deploy projects that use JSP. The embedded Tomcat is not very stable, and there is occasional slow access.
This is the end of "how SpringBoot uses JSP in the project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.