最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在IDEA中搭建最小可用SpringMVC項(xiàng)目(純Java配置)

 更新時(shí)間:2019年12月05日 09:14:07   作者:墨悲絲染  
這篇文章主要介紹了在IDEA中搭建最小可用SpringMVC項(xiàng)目(純Java配置),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 新建 SpringMVC 的 Web 項(xiàng)目

  • File - New - Project..
  • 勾選 SpringMVC 和 WebApplication ,點(diǎn)擊Next
  • 填寫 Project name : hello
  • 點(diǎn)擊 Finish
  • IDEA 會(huì)自動(dòng)下載所需的 SpringMVC 的 jar 包

2. 代碼編寫

代碼參考 《Spring 實(shí)戰(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 //聲明為一個(gè)控制器
public class HelloController {
  @RequestMapping(value = "/home",method = GET)//處理對(duì) “/” 的 GET 請(qǐng)求
  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 的配置和啟動(dòng)

配置tomcat服務(wù)

  • 點(diǎn)擊 IDEA 右上角 綠色的小錘子圖標(biāo)旁的 Add Configuration...
  • 在彈出頁面中,點(diǎn)擊加號(hào)
  • 選擇 Tomcat Server - Local
  • 填寫 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: 無法訪問javax.servlet.ServletException
  找不到j(luò)avax.servlet.ServletException的類文件

這時(shí)需要添加 javax.servlet-api

4. 測(cè)試

瀏覽器訪問 http://localhost:8080/home

顯示

hello world

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java 類的加載機(jī)制

    詳解Java 類的加載機(jī)制

    這篇文章主要介紹了Java 類的加載機(jī)制,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-08-08
  • SpringBoot+Security 發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)

    SpringBoot+Security 發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot+Security 發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解微信小程序 同步異步解決辦法

    詳解微信小程序 同步異步解決辦法

    這篇文章主要介紹了詳解微信小程序 同步異步解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Java中的反射機(jī)制詳解

    Java中的反射機(jī)制詳解

    這篇文章主要介紹了JAVA 反射機(jī)制的相關(guān)知識(shí),文中講解的非常細(xì)致,代碼幫助大家更好的理解學(xué)習(xí),感興趣的朋友可以了解下
    2021-09-09
  • 詳解SpringBoot時(shí)間參數(shù)處理完整解決方案

    詳解SpringBoot時(shí)間參數(shù)處理完整解決方案

    這篇文章主要介紹了詳解SpringBoot時(shí)間參數(shù)處理完整解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別

    Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別

    這篇文章主要介紹了Synchronized?和?ReentrantLock?的實(shí)現(xiàn)原理及區(qū)別,文章為榮啊主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 淺談MyBatis循環(huán)Map(高級(jí)用法)

    淺談MyBatis循環(huán)Map(高級(jí)用法)

    這篇文章主要介紹了淺談MyBatis循環(huán)Map(高級(jí)用法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot加載啟動(dòng)的源碼解析

    SpringBoot加載啟動(dòng)的源碼解析

    這篇文章主要介紹了SpringBoot加載啟動(dòng)的源碼解析,@SpringBootApplication注解是Spring Boot的核心注解,它其實(shí)是一個(gè)組合注解,本身其實(shí)也是一個(gè)IoC容器的配置類,需要的朋友可以參考下
    2023-12-12
  • 詳解Java枚舉類在生產(chǎn)環(huán)境中的使用方式

    詳解Java枚舉類在生產(chǎn)環(huán)境中的使用方式

    本文主要介紹了Java枚舉類在生產(chǎn)環(huán)境中的使用方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • Springboot中使用Filter實(shí)現(xiàn)Header認(rèn)證詳解

    Springboot中使用Filter實(shí)現(xiàn)Header認(rèn)證詳解

    這篇文章主要介紹了Springboot中使用Filter實(shí)現(xiàn)Header認(rèn)證詳解,當(dāng)在?web.xml?注冊(cè)了一個(gè)?Filter?來對(duì)某個(gè)?Servlet?程序進(jìn)行攔截處理時(shí),它可以決定是否將請(qǐng)求繼續(xù)傳遞給?Servlet?程序,以及對(duì)請(qǐng)求和響應(yīng)消息是否進(jìn)行修改,需要的朋友可以參考下
    2023-08-08

最新評(píng)論

尉犁县| 大宁县| 枣庄市| 哈尔滨市| 两当县| 电白县| 随州市| 石家庄市| 娄烦县| 富阳市| 旺苍县| 南部县| 建瓯市| 高碑店市| 五家渠市| 峨眉山市| 卢氏县| 五莲县| 炎陵县| 漠河县| 阿坝| 哈巴河县| 兴安盟| 靖安县| 绵竹市| 黑山县| 老河口市| 鄂伦春自治旗| 武城县| 本溪市| 济源市| 皋兰县| 汝南县| 会泽县| 赤城县| 金寨县| 盐边县| 萨嘎县| 阳谷县| 浦北县| 北宁市|