Primarily using Javaconfig-based configuration
Configure Dispatcherservlet
AbstractAnnotationConfigDispatcherServletInitializer Dispatcherservlet and Spring application contexts are configured by inheriting abstract classes (no need to configure in XML)
Public classSpittrwebappinitializerextendsAbstractannotationconfigdispatcherservletinitializer {? //Specify the Spring app context configuration class (the Bean for the primary Configuration Web Component)@OverrideprotectedClass<?>[] getservletconfigclasses () {return NewClass[]{webconfig.class}; } //Another application context configuration class (other beans in the app) that corresponds to@OverrideprotectedClass<?>[] getrootconfigclasses () {return NewClass[]{rootconfig.class}; }? //map Dispatcherservlet to "/"@Overrideprotectedstring[] Getservletmappings () {return Newstring[]{"/"}; }}
Where webconfig primarily configures Web component-related beans, such as controllers, view resolvers, and processing mappers, the content is roughly as follows
@Configuration//flag This class is a configuration class@EnableWebMvc//Enable annotation-driven Spring MVC, with the <mvc:annotation-driven> of XML configuration Methods@ComponentScan ("Spitter.web")//Scan the Bean component in this package Public classWebconfigextendswebmvcconfigureradapter{? /*** Configure View resolver*/@Bean Publicviewresolver Viewresolver () {internalresourceviewresolver resolver=NewInternalresourceviewresolver (); Resolver.setprefix ("/web-inf/views/"); Resolver.setsuffix (". JSP"); Resolver.setexposecontextbeansasattributes (true); returnResolver; }? /*** Configure the processing of static resources*/@Override Public voidconfiguredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {configurer.enable (); } //other web-related beans, etc. ..}
RootConfig Configure a Bean other than the Web component, the content is as follows
@Configuration // Configure the scanned package, as well as the filter @ComponentScan (basepackages = {"Spitter"}, Excludefilters = { = Filtertype.annotation, value = Enablewebmvc. class )})publicclass rootconfig { // Bean etc }
Add Controller, etc.
You can add the corresponding Controller, JSP files and so on in the corresponding location
To list a simple Controller
@Controller @requestmapping ("/") Public class HomeController { = requestmethod.get) public String Home () { return "Home"; }}
File structure
The overall structure of the file is as follows:
Spring MVC Configuration Note-based on Javaconfig