<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Step 5: Add the Configuration ClassPackage Configuration;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.enablewebmvc;import Org.springframework.web.servlet.view.internalresourceviewresolver;import Org.springframework.web.servlet.view.JstlView; @Configuration @enablewebmvc@componentscan (basepackages = " COM.SPRINGMVC ") public class Helloworldconfiguration {@Beanpublic viewresolver viewresolver () { Internalresourceviewresolver viewresolver = new Internalresourceviewresolver (); Viewresolver.setviewclass ( Jstlview.class); Viewresolver.setprefix ("/web-inf/views/"); Viewresolver.setsuffix (". jsp"); return viewresolver;}}
@Configuration @EnableWebMvc
Equivalent to <mvc:annotation-driven/>xml, which is designed to support classes of @controller annotations that @RequestMapping map incoming requests to specific methods.
@ComponentScan (basepackages = "Com.springmvc")
Equivalent to <context:component-scan base-package= "Com.springmvc"/>
Step 6: Add the Initialization classPackage Configuration;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.servletregistration;import Org.springframework.web.webapplicationinitializer;import Org.springframework.web.context.support.annotationconfigwebapplicationcontext;import Org.springframework.web.servlet.dispatcherservlet;public class Helloworldinitializer Implements webapplicationinitializer{@Overridepublic void Onstartup (ServletContext container) throws Servletexception { Annotationconfigwebapplicationcontext CTX = new Annotationconfigwebapplicationcontext (); Ctx.register ( Helloworldconfiguration.class); Ctx.setservletcontext (container); Servletregistration.dynamic servlet = Container.addservlet ("Dispatcher", New Dispatcherservlet (CTX)); Servlet.setloadonstartup (1); Servlet.addmapping ("/");}}
The above content is similar to the content of Web. XML in the previous tutorial because we use the front-end controller DispatherServler , assigning a map (Url-pattern in XML) instead of providing a path for the spring configuration file (spring-mvc.xml), where we are registering the configuration class. in General, we are doing the same thing, but the method is different.
Update: Note that you can now write the above class more succinctly [ This is the preferred way ] by extending AbstractAnnotationConfigDispatcherServletInitializer base class, as follows:
Package Configuration;import Org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer;public class Initializer extends abstractannotationconfigdispatcherservletinitializer{@Overrideprotected class<?>[] Getrootconfigclasses () {return new class[]{helloworldconfiguration.class};} @Overrideprotected class<?>[] Getservletconfigclasses () {return null;} @Overrideprotected string[] Getservletmappings () {return new string[]{"/"};}}
Deployment runs.
Spring 4 MVC HelloWorld-Annotated/javaconfig instance