Spring4 in action-5.2.2-spring Web application-Simple controller implementation jump
// Source: http://download.csdn.net/download/poiuy1991719/10111794
First create the class for the startup configuration:
PackageSpittr.config;ImportOrg.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;ImportSpittr.web.WebConfig;/*** * Container will find Abstractannotationconfigdispatcherservletinitializer this class (including extensions of this class) * And then use this class to configure the servlet context*/ Public classSpitterwebinitializerextendsAbstractannotationconfigdispatcherservletinitializer {@OverrideprotectedClass<?>[] Getrootconfigclasses () {//non-Web Components, classes with @configuration annotations to define the application context return NewClass<?>[] {rootconfig.class }; } @OverrideprotectedClass<?>[] Getservletconfigclasses () {//Specifies the configuration class that is used to define the application context with the @configuration annotation class return NewClass<?>[] {webconfig.class }; } @OverrideprotectedString[] Getservletmappings () {//map Dispatcherservlet to "/" return NewString[] {"/" }; }}
Then write the Webconfig configuration class instead of the previous XML configuration
PackageSpittr.web;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.servlet.ViewResolver;ImportOrg.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;ImportOrg.springframework.web.servlet.view.InternalResourceViewResolver;/*** * Configure Web*/@Configuration//configuration classes, required annotations@EnableWebMvc//Enable Spring MVC@ComponentScan ("Spittr.web")//enable component Scan to scan to controller Public classWebconfigextendsWebmvcconfigureradapter {@Bean PublicViewresolver Viewresolver () {//Configuring the JSP view resolverInternalresourceviewresolver resolver =NewInternalresourceviewresolver (); Resolver.setprefix ("/web-inf/views/");//before the request path, join the resource pathResolver.setsuffix (". jsp");//after the request path, add the resource name, such as/web-inf/views/home.jspResolver.setviewclass (Org.springframework.web.servlet.view.JstlView.class); returnResolver; } @Override Public voidConfiguredefaultservlethandling (//working with Static resourcesdefaultservlethandlerconfigurer Configurer) {configurer.enable (); } @Override Public voidaddresourcehandlers (Resourcehandlerregistry registry) {Super. Addresourcehandlers (registry); }}
Write the root configuration class: You can use other non-web components, such as database connections:
PackageSpittr.config;ImportJava.util.regex.Pattern;ImportOrg.springframework.context.annotation.ComponentScan;ImportOrg.springframework.context.annotation.ComponentScan.Filter;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.FilterType;ImportOrg.springframework.context.annotation.Import;ImportOrg.springframework.core.type.filter.RegexPatternTypeFilter;ImportSpittr.config.RootConfig.WebPackage;/*** * Introduce the database configuration class to automatically scan the SPITTR packet, and the child package*/@Configuration//configuration classes, required annotations@Import (Dataconfig.class) @ComponentScan (basepackages= {"Spittr"}, Excludefilters = {@Filter (type = filtertype.custom, value = WebPackage.class) }) Public classRootConfig { Public Static classWebPackageextendsRegexpatterntypefilter { PublicWebPackage () {Super(Pattern.compile ("Spittr\\.web")); } }}
Database Connection Configuration:
PackageSpittr.config;ImportJavax.sql.DataSource;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.jdbc.core.JdbcOperations;Importorg.springframework.jdbc.core.JdbcTemplate;ImportOrg.springframework.jdbc.datasource.DriverManagerDataSource; @Configuration Public classDataconfig {@Bean PublicDataSource DataSource () {Drivermanagerdatasource DataSource=NewDrivermanagerdatasource (); Datasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.seturl ("Jdbc:mysql:///spittle"); Datasource.setusername ("Root"); Datasource.setpassword ("Root"); returnDataSource; } @Bean Publicjdbcoperations JdbcTemplate (DataSource DataSource) {return NewJdbcTemplate (DataSource); }}
Website application, basic configuration is OK, write controller:
PackageSpittr.web.spittle;Import Staticorg.springframework.web.bind.annotation.requestmethod.*;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping, @Controller @requestmapping ("/")//requests to process all master paths "/" Public classHomeController {@RequestMapping (method=GET) PublicString Home (model) {return"Home";//"home.jsp": Because in the Webconfig class, the auto-join ". JSP" is set, so the corresponding is "home.jsp" }}
Create page: (Path:/web-inf/views/home.jsp)
<HTML><Head><title>Spitter</title></Head><Body> <H1>Welcome to Spitter</H1></Body></HTML>
Spring4 in action-5.2.2-spring Web application-Simple controller implementation jump