在IDEA中搭建最小可用SpringMVC項目(純Java配置)
1. 新建 SpringMVC 的 Web 項目
- File - New - Project..
- 勾選 SpringMVC 和 WebApplication ,點擊Next
- 填寫 Project name : hello
- 點擊 Finish
- IDEA 會自動下載所需的 SpringMVC 的 jar 包
2. 代碼編寫
代碼參考 《Spring 實戰(zhàn)》(第四版),本文和書中代碼略有差異
刪除不需要的配置文件
- 刪除 WEB-INF 下的 web.xml
- 刪除 WEB-INF 下的 dispatcher-servlet.xml
- 刪除 WEB-INF 下的 applicationContext.xml
- 刪除 web 下的 index.jsp
編寫 JavaConfig 文件
- 新建 package : com.yangrd.springmvc.config
- 新建 配置文件 HelloWebAppInitializer.java
package com.yangrd.springmvc.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class HelloWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { System.out.println("getRootConfig"); return new Class<?>[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { System.out.println("getServletConfig"); return new Class<?> []{WebConfig.class}; } @Override protected String[] getServletMappings() { System.out.println("getServletMappings"); return new String[]{"/"}; } }
新建配置文件 RootConfig.java
package com.yangrd.springmvc.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @ComponentScan(basePackages = {"com.yangrd.springmvc"}, excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value = EnableWebMvc.class)}) public class RootConfig { }
新建配置文件 WebConfig.java
package com.yangrd.springmvc.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @ComponentScan("com.yangrd.springmvc.controller") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".html"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ configurer.enable(); } }
編寫 Controller
- 新建 package : com.yangrd.springmvc.controller
- 新建 文件 HelloController.java
package com.yangrd.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.GET; /** * */ @Controller //聲明為一個控制器 public class HelloController { @RequestMapping(value = "/home",method = GET)//處理對 “/” 的 GET 請求 public String hello(){ return "hello"; //邏輯視圖名為hello } }
編寫 view 文件
- 在 WEB-INF 下新建文件夾 views
- 在 views 文件夾下新建 hello.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> hello world </body> </html>
3. Tomcat 的配置和啟動
配置tomcat服務(wù)
- 點擊 IDEA 右上角 綠色的小錘子圖標(biāo)旁的 Add Configuration...
- 在彈出頁面中,點擊加號
- 選擇 Tomcat Server - Local
- 填寫 Name : helloServer
- 點擊 Deployment - 點擊 + ,選擇 Artifact
- 點擊 Apply, OK
將Sping MVC 相關(guān)包放到 Web 工程 中的 lib 下
- File - Project Structure...
- 選擇 Artifacts
- 在右側(cè)的 Available Elements 中 hello 下 有兩個 Spring 的jar上,右鍵 選擇 `Put into /WEB-INF/lib
- 點擊 Apply - OK
啟動tomcat
這是啟動tomcat 會報錯
Error:(5, 8) java: 無法訪問javax.servlet.ServletException
找不到j(luò)avax.servlet.ServletException的類文件
這時需要添加 javax.servlet-api
4. 測試
瀏覽器訪問 http://localhost:8080/home
顯示
hello world
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用IDEA搭建SSM框架的詳細(xì)教程(spring + springMVC +MyBatis)
- IDEA上面搭建一個SpringBoot的web-mvc項目遇到的問題
- 教你使用idea搭建ssm詳細(xì)教程(Spring+Spring Mvc+Mybatis)
- 在IntelliJ IDEA 搭建springmvc項目配置debug的教程詳解
- 使用idea搭建一個spring mvc項目的圖文教程
- 如何使用Idea搭建全注解式開發(fā)的SpringMVC項目
- SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫的示例詳解
- 使用Idea快速搭建SpringMVC項目的詳細(xì)步驟記錄
相關(guān)文章
關(guān)于springboot集成swagger3時spring-plugin-core報錯的問題
這篇文章主要介紹了關(guān)于springboot集成swagger3時spring-plugin-core報錯的問題,本文給大家分享解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09SpringBoot的@ControllerAdvice處理全局異常詳解
這篇文章主要介紹了SpringBoot的@ControllerAdvice處理全局異常詳解,但有時卻往往會產(chǎn)生一些bug,這時候就破壞了返回數(shù)據(jù)的一致性,導(dǎo)致調(diào)用者無法解析,所以我們常常會定義一個全局的異常攔截器,需要的朋友可以參考下2024-01-01Java Synchronized字節(jié)碼層分析體驗
這篇文章主要介紹了Java Synchronized字節(jié)碼層分析,synchronized關(guān)鍵字解決了多個線程之間的資源同步性,synchronized關(guān)鍵字保證了它修飾的方法或者代碼塊任意時刻只有一個線程在訪問2023-04-04