Date: December 1, 2019

1. Create a New SpringMVC Web project

  • File – New – Project..
  • Check SpringMVC and WebApplication and click Next
  • Fill in Project name: Hello
  • Click Finish
  • IDEA automatically downloads the required SpringMVC JAR packages

2. Code writing

The code refers to “Spring In Action” (4th edition). The code in this article is slightly different from that in the book

Delete unnecessary configuration files

  • Delete the web. XML file in web-INF
  • Delete dispatcher-servlet.xml in web-INF
  • Delete applicationContext.xml in web-INF
  • Delete the index.jsp from the Web

Write a JavaConfig file

  • The new package: com. Yangrd. For springmvc. Config
  • Creating a Configuration FileHelloWebAppInitializer.java
package com.yangrd.springmvc.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class HelloWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protectedClass<? >[] getRootConfigClasses() { System.out.println("getRootConfig");
        return newClass<? >[]{RootConfig.class}; }@Override
    protectedClass<? >[] getServletConfigClasses() { System.out.println("getServletConfig");
        return newClass<? > []{WebConfig.class}; }@Override
    protected String[] getServletMappings() {
        System.out.println("getServletMappings");
        return new String[]{"/"}; }}Copy the code
  • Creating a Configuration FileRootConfig.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 {}Copy the code
  • Creating a Configuration FileWebConfig.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(a){
        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(); }}Copy the code

Writing the Controller

  • The new package: com. Yangrd. For springmvc. Controller
  • The new fileHelloController.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 // Declare it as a controller
public class HelloController {
    @RequestMapping(value = "/home",method = GET)// Handle GET requests for "/"
    public String hello(a){
        return "hello"; // The logical view name is hello}}Copy the code

Write a View file

  • Create a folder in WEB-INFviews
  • inviewsNew folderhello.html

      
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    hello world
</body>
</html>
Copy the code

3. Configure and start Tomcat

Configuring the Tomcat Service

  • Click next to the small green hammer icon in the upper right corner of IDEAAdd Configuration...
  • On the pop-up page, click the plus sign
  • chooseTomcat ServerLocal
  • Enter Name: helloServer
  • Click on theDeployment– click on the+, the choice ofArtifact
  • Click on theApply.OK

Place the Sping MVC package under lib in your Web project

  • FileProject Structure...
  • chooseArtifacts
  • On the jar with two Springs under Hello in Available Elements on the right, right-click and select ‘Put into /WEB-INF/lib’
  • Click on theApplyOK

Start tomcat

  • This is a tomcat startup error
Error: (5, 8) Java: unable to access javax.mail. The servlet. ServletException find javax.mail. Servlet. ServletException class filesCopy the code

You need to add javax.servlet-API

4. Test

Browser to access http://localhost:8080/home

According to

hello world