javascript
Spring Boot 开发web 项目
可參考博文:
搭建Spring Boot 項目
使用idea解決新建jsp文件而找不到jsp文件模版的新建選項
(一)快速搭建Web 項目
博主使用的是IDEA ,下面是項目目錄結構:在用IDEA 創建Spring Boot 項目時,是沒有webapp 目錄的,所以增加了webapp 目錄以及webapp 下的static(用于存放靜態資源) 和 WEB-INF (用于存放jsp 頁面資源)目錄。
首先在pom.xml 中添加jsp 相關的依賴。
<dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>compile</scope></dependency>在application.properties 配置文件中配置相關的信息。
#設置上下文訪問路徑 server.context-path=/springboot #設置jsp 訪問資源前綴 spring.mvc.view.prefix=/WEB-INF/views/ #設置jsp 訪問資源后綴 spring.mvc.view.suffix=.jsp在配置文件中配置了試圖解析器的前綴與后綴后下面就來測試一下,我編寫了一個HelloController 用于進行頁面的轉發。
@Controller public class HelloController {/*** 訪問"hello" 時,轉發到"index.jsp" 頁面*/@RequestMapping("/hello")public String sayHello(){return "index";} }index.jsp 頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h1>Hello Spring Boot</h1> </body> </html>然后在瀏覽器訪問”hello”,發現成功訪問到了index.jsp 頁面。
(二)Spring Boot 處理靜態資源
2.1Spring Boot 處理靜態資源的方式
- 在src/main/webapp 目錄下,可以直接訪問。
- Spring Boot 提供了默認的靜態資源訪問路徑。
- 通過spring.resources 前綴在配置文件中配置靜態資源的路徑。
下面是Spring Boot 默認的四個靜態資源訪問路徑。
2.2效果演示
這里只演示訪問在src/main/webapp 目錄下的靜態資源。在static 目錄下存在著一張圖片。
然后就可以通過路徑訪問到了(為了顯示效果,不再貼出顯示的圖片)。
(三)在Spring Boot 下使用servlet API
3.1開發Servlet
3.1.1基于注解的Servlet 開發
Servlet 開發步驟:
3.1.2效果演示
Servlet 類
@WebServlet("/test.do") public class TestServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//向瀏覽器輸出一句話response.getWriter().write("Hello Spring Boot!");} }入口類
// 因為Servlet 所在的包與該入口類所在的包不同,所以進行對應的設置 @ServletComponentScan(value = "com.jas") @SpringBootApplication public class SpringbootApplication {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args);} }效果
3.2開發Filter
以前在使用Servlet 開發Filter 時,需要先編寫一個特定的類實現Filter 接口,然后在web.xml 配置文件中注冊該過濾器,這樣過濾器才能生效。這種方式顯得有點復雜,所以Spring Boot 提供了@WebFilter 注解,來簡化開發過程。只需要在實現Filter 類接口的類上加上該注解,這個過濾器就可以使用了。
過濾器TestFilter 類
// 設置攔截所有請求 @WebFilter("/*") public class TestFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {System.out.println("過濾器攔截到了請求!");chain.doFilter(request, response);}@Overridepublic void destroy() {} }在瀏覽器進行資源訪問時,會在控制臺輸出。
3.3開發Listener
同過濾器Filter 一樣,Listener 的開發也需要在web.xml 中進行注冊。為此,Spring Boot 也提供了一個@WebListener 注解。
監聽器TestListener 類
@WebListener public class TestListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("ServletContext 對象被創建了!");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {} }在應用程序啟動時,在控制臺的輸出如下(只截取了其中的一部分):
PS:可參考博文
Servlet 過濾器
Servlet 監聽器
總結
以上是生活随笔為你收集整理的Spring Boot 开发web 项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot 核心注解与配置文
- 下一篇: 项羽虞姬毛不易歌词(项羽虞姬毛不易)