在IDEA中搭建最小可用SpringMVC項(xiàng)目(純Java配置)
1. 新建 SpringMVC 的 Web 項(xiàng)目
- File - New - Project..
- 勾選 SpringMVC 和 WebApplication ,點(diǎn)擊Next
- 填寫(xiě) Project name : hello
- 點(diǎn)擊 Finish
- IDEA 會(huì)自動(dòng)下載所需的 SpringMVC 的 jar 包
2. 代碼編寫(xiě)
代碼參考 《Spring 實(shí)戰(zhàn)》(第四版),本文和書(shū)中代碼略有差異
刪除不需要的配置文件
- 刪除 WEB-INF 下的 web.xml
- 刪除 WEB-INF 下的 dispatcher-servlet.xml
- 刪除 WEB-INF 下的 applicationContext.xml
- 刪除 web 下的 index.jsp
編寫(xiě) 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();
}
}
編寫(xiě) 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 //聲明為一個(gè)控制器
public class HelloController {
@RequestMapping(value = "/home",method = GET)//處理對(duì) “/” 的 GET 請(qǐng)求
public String hello(){
return "hello"; //邏輯視圖名為hello
}
}
編寫(xiě) 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 的配置和啟動(dòng)
配置tomcat服務(wù)
- 點(diǎn)擊 IDEA 右上角 綠色的小錘子圖標(biāo)旁的 Add Configuration...
- 在彈出頁(yè)面中,點(diǎn)擊加號(hào)
- 選擇 Tomcat Server - Local
- 填寫(xiě) Name : helloServer
- 點(diǎn)擊 Deployment - 點(diǎn)擊 + ,選擇 Artifact
- 點(diǎn)擊 Apply, OK
將Sping MVC 相關(guān)包放到 Web 工程 中的 lib 下
- File - Project Structure...
- 選擇 Artifacts
- 在右側(cè)的 Available Elements 中 hello 下 有兩個(gè) Spring 的jar上,右鍵 選擇 `Put into /WEB-INF/lib
- 點(diǎn)擊 Apply - OK
啟動(dòng)tomcat
這是啟動(dòng)tomcat 會(huì)報(bào)錯(cuò)
Error:(5, 8) java: 無(wú)法訪問(wèn)javax.servlet.ServletException
找不到j(luò)avax.servlet.ServletException的類(lèi)文件
這時(shí)需要添加 javax.servlet-api
4. 測(cè)試
瀏覽器訪問(wèn) http://localhost:8080/home
顯示
hello world
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用IDEA搭建SSM框架的詳細(xì)教程(spring + springMVC +MyBatis)
- IDEA上面搭建一個(gè)SpringBoot的web-mvc項(xiàng)目遇到的問(wèn)題
- 教你使用idea搭建ssm詳細(xì)教程(Spring+Spring Mvc+Mybatis)
- 在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解
- 使用idea搭建一個(gè)spring mvc項(xiàng)目的圖文教程
- 如何使用Idea搭建全注解式開(kāi)發(fā)的SpringMVC項(xiàng)目
- SpringMVC框架搭建idea2021.3.2操作數(shù)據(jù)庫(kù)的示例詳解
- 使用Idea快速搭建SpringMVC項(xiàng)目的詳細(xì)步驟記錄
相關(guān)文章
SpringBoot+Security 發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot+Security 發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
詳解SpringBoot時(shí)間參數(shù)處理完整解決方案
這篇文章主要介紹了詳解SpringBoot時(shí)間參數(shù)處理完整解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別
這篇文章主要介紹了Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別,文章為榮啊主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
淺談MyBatis循環(huán)Map(高級(jí)用法)
這篇文章主要介紹了淺談MyBatis循環(huán)Map(高級(jí)用法),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
詳解Java枚舉類(lèi)在生產(chǎn)環(huán)境中的使用方式
本文主要介紹了Java枚舉類(lèi)在生產(chǎn)環(huán)境中的使用方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
Springboot中使用Filter實(shí)現(xiàn)Header認(rèn)證詳解
這篇文章主要介紹了Springboot中使用Filter實(shí)現(xiàn)Header認(rèn)證詳解,當(dāng)在?web.xml?注冊(cè)了一個(gè)?Filter?來(lái)對(duì)某個(gè)?Servlet?程序進(jìn)行攔截處理時(shí),它可以決定是否將請(qǐng)求繼續(xù)傳遞給?Servlet?程序,以及對(duì)請(qǐng)求和響應(yīng)消息是否進(jìn)行修改,需要的朋友可以參考下2023-08-08

