Spring Road (17) - Configure Spring MVC with annotations

Keywords: xml JSP Java Fragment

background

The previous article details the process of configuring SpringMVC using xml, rather than specifying a Dispatcher Servlet, specifying a container configuration file, and then writing controllers and views.

Similar to using annotation configurations, we have a class responsible for specifying the Dispatcher Servlet, a configuration class responsible for configuring containers and opening scans for bean s such as controllers, and finally writing the Controller and View pages to accomplish specific functions.Below is the implementation.

New Project

The process of creating a new project, SpringMvcSecond, is essentially the same as the previous one, which is to change the project name, so it will not be repeated.

The only difference is that this article no longer uses web.xml configuration items, so delete the web.xml file.

New SpringMVC Initializer

In the previous project, we actually configure the project initialization via web.xml, including specifying Dispatcher Servlet and container configuration file. Instead, we use an initializer, which implements the function of initializing a web project when it is initialized by inheriting AbstractAnnotationConfigDispatcher Servlet Initializer.

package org.maoge.second;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
   @Override
   protected Class<?>[] getRootConfigClasses() {
   	return null;
   }
   @Override
   protected Class<?>[] getServletConfigClasses() {
   	// Introducing configuration classes
   	return new Class[] { SpringConfig.class };
   }
   @Override
   protected String[] getServletMappings() {
   	// Configure url mapping path
   	return new String[] { "*.do" };
   }
}

Note that this class specifies to accept requests ending with.do and that the configuration class is SpringConfig.

New Configuration Class

Configuring containers actually only requires opening the package scan:

package org.maoge.second;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(value = "org.maoge.second")
public class SpringConfig {
}

New Controller and View Page

This is similar to the previous one, and will not be described in detail.

package org.maoge.second;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
	@RequestMapping("/hello")
	public ModelAndView hello() {
		ModelAndView mv=new ModelAndView();	
		mv.setViewName("hello.jsp");
		return mv;
	}
}
<%@ page language="java" contentType="text/html; charset=UTF8"
	pageEncoding="UTF8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF8">
<title></title>
</head>
<body>
hello:springmvcsecond
</body>
</html>

Start Run

Error after startup

More than one fragment with the name [spring_web] was found. This is not legal with relative ordering.

This time you're angry. Remove the two jar packages in the following image and fix the problem:

Verification successful!

329 original articles published, 238 praised, 530,000 visits+
His message board follow

Posted by Phirus on Sat, 25 Jan 2020 16:46:23 -0800