Spring Road (17) - All configuration using annotations SpringMVC

background

Previous article detailed how to use xml configuration SpringMVC process, nothing more than a specified DispatcherServlet, specify container configuration file, then write controllers and views.

Use annotations configuration is similar, we have to have a class that is responsible for specifying DispatcherServlet, then there is a configuration class is responsible for configuring container open simultaneously scan controller bean, and finally write controller and view pages to accomplish specific functions. The following specific implementation.

New Project

New Project SpringMvcSecond, this process to keep up a consistent, that is, changed the project name, description thereof is omitted.

The only difference is that herein, this is no longer using the web.xml configuration item, so delete web.xml file.

New SpringMVC initializer

Previous projects we are actually carried out by the project web.xml the initial configuration, including specifying DispatcherServlet, specify container configuration file, this article, we instead use initializer, as follows, to initialize implements web projects through inheritance AbstractAnnotationConfigDispatcherServletInitializer function initialization operation when initialization.

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() {
   	// 引入配置类
   	return new Class[] { SpringConfig.class };
   }
   @Override
   protected String[] getServletMappings() {
   	// 配置url映射路径
   	return new String[] { "*.do" };
   }
}

Note that the designated class accepts .doending request, and arranged class SpringConfig.

New configuration class

Configure the container really only need to open the scanning of the package:

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 page views

This is basically similar to the previous one, 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>

Up and running

After starting the still error

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

The anger, delete the following figure after two jar package to solve the problem:
Here Insert Picture Description
Verify success!

Published 329 original articles · won praise 238 · views 530 000 +

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/104085239