转载

在 IDEA 中搭建最小可用 SpringMVC 项目(纯 Java 配置)

  • File - New - Project..
  • 勾选 SpringMVC 和 WebApplication ,点击Next
  • 填写 Project name : hello
  • 点击 Finish
  • IDEA 会自动下载所需的 SpringMVC 的 jar 包

2. 代码编写

代码参考 《Spring 实战》(第四版),本文和书中代码略有差异

删除不需要的配置文件

  • 删除 WEB-INF 下的 web.xml
  • 删除 WEB-INF 下的 dispatcher-servlet.xml
  • 删除 WEB-INF 下的 applicationContext.xml
  • 删除 web 下的 index.jsp

编写 JavaConfig 文件

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

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服务

  • 点击 IDEA 右上角 绿色的小锤子图标旁的 Add Configuration...
  • 在弹出页面中,点击加号
  • 选择 Tomcat Server - Local
  • 填写 Name : helloServer
  • 点击 Deployment - 点击 + ,选择 Artifact
  • 点击 Apply , OK
原文  https://juejin.im/post/5de67d316fb9a016536e9946
正文到此结束
Loading...