Profile-based Web projects may be more convenient to maintain, but sometimes we have special needs, such as preventing customers from randomly changing configurations, which we need to hide in the code.
1. Create a Dynamic Web project (without web. xml)
2. Right-click Project to add several Package:com.easyweb.config (save project configuration) Com.easyweb.controller (save SPRINGMVC Controller)
3. Create a new class Webapplicationstartup in Com.easyweb.config, this class implements the Webapplicationinitializer interface, which is the entry of the project, acting like web. XML, with the following code:
PackageCom.easyweb.config;ImportJavax.servlet.MultipartConfigElement;ImportJavax.servlet.ServletContext;ImportJavax.servlet.ServletException;Importjavax.servlet.ServletRegistration.Dynamic;ImportOrg.springframework.web.WebApplicationInitializer;ImportOrg.springframework.web.context.support.AnnotationConfigWebApplicationContext;ImportOrg.springframework.web.servlet.DispatcherServlet;/** * Server Startup Entry class * * @author Administrator * */ Public class webapplicationstartup implements Webapplicationinitializer { Private Static FinalString Servlet_name ="Spring-mvc";Private Static Final LongMax_file_upload_size =1024x768*1024x768*5;//5 Mb Private Static Final intFile_size_threshold =1024x768*1024x768;//After 1Mb Private Static Final LongMax_request_size =-1L//No request size limit /** * Server Startup Call this method, where you can do the same configuration as Web. XML */ @Override Public void Onstartup(ServletContext ServletContext)throwsservletexception {//Register SPRINGMVC servlet This. Addservlet (ServletContext);//Register filter //Servletcontext.addfilter (arg0, arg1) //Register listener //Servletcontext.addlistener (arg0);}/** * Register Spring servlet * * @param servletcontext */ Private void Addservlet(ServletContext ServletContext) {//Build a application contextAnnotationconfigwebapplicationcontext Webcontext = Createwebcontext (Springmvc.class, ViewConfiguration.class);//Register the servlet for spring MVCDynamic dynamic = Servletcontext.addservlet (Servlet_name,NewDispatcherservlet (Webcontext));//Add SPRINGMVC to allow access to the controller suffixDynamic.addmapping ("*.html","*.ajax","*.css","*.js","*.gif","*.jpg","*.png");//All by Please use "/" //Dynamic.addmapping ("/");Dynamic.setloadonstartup (1); Dynamic.setmultipartconfig (NewMultipartconfigelement (NULL, Max_file_upload_size, Max_request_size, File_size_threshold)); }/** * Using a custom configuration class to instantiate a Web application Context * * @param annotatedclasses * @return */ PrivateAnnotationconfigwebapplicationcontextCreatewebcontext(CLASS<?> annotatedclasses) {Annotationconfigwebapplicationcontext Webcontext =NewAnnotationconfigwebapplicationcontext (); Webcontext.register (annotatedclasses);returnWebcontext; }}
4. Add Class Springmvc inherit Webmvcconfigureradapter under Com.easyweb.config, the function of this class is to make some configuration of SPRINGMVC, the code is as follows:
PackageCom.easyweb.config;ImportOrg.springframework.context.annotation.ComponentScan;ImportOrg.springframework.context.annotation.Configuration;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;@Configuration@EnableWebMvc//Specify the package name where the controller resides@ComponentScan(Basepackages = {"Com.easyweb.controller"}) Public class springmvc extends webmvcconfigureradapter { /** * Not required * / @Override Public void configuredefaultservlethandling(FinalDefaultservlethandlerconfigurer configurer) {configurer.enable (); }/** * If some of the project's resource files are placed under/web-inf/resources/* The address accessed in the browser is similar to the following: http://host:port/projectName/WEB-INF/resources/ XXX.CSS * But after adding the following definition, you can access: * HTTP://HOST:PORT/PROJECTNAME/RESOURCES/XXX.CSS * not required */ @Override Public void addresourcehandlers(FinalResourcehandlerregistry registry) {Registry.addresourcehandler ("/resources/**/*"). Addresourcelocations ("/web-inf/resources/"); }}
5. Add the View profile Com.easyweb.config under the new class Viewconfiguration, which can be defined according to their own needs of the views interceptor:
Packagecom. Easyweb. config;import org. Springframework. Context. Annotation. Bean;import org. Springframework. Context. Annotation. Configuration;import org. Springframework. Web. servlet. Viewresolver;import org. Springframework. Web. servlet. View. Jstlview;import org. Springframework. Web. servlet. View. Urlbasedviewresolver;import org. Springframework. Web. servlet. View. Tiles2. Tilesconfigurer;import org. Springframework. Web. servlet. View. Tiles2. Tilesview;@Configurationpublic class Viewconfiguration {@Bean public viewresolver urlbasedviewresolver () {Urlbasedvie Wresolver Viewresolver;Viewresolver = new Urlbasedviewresolver ();Viewresolver. Setorder(2);Viewresolver. Setprefix("/web-inf/");Viewresolver. Setsuffix(". JSP");Viewresolver. Setviewclass(Jstlview. Class);For debug Envirment viewresolver. Setcache(false);Return Viewresolver;} @Bean Public Viewresolver tilesviewresolver () {urlbasedviewresolver urlbasedviewresolver = new Urlbasedvie Wresolver ();Urlbasedviewresolver. Setorder(1);Urlbasedviewresolver. Setviewclass(Tilesview. Class);Urlbasedviewresolver. Return Urlbasedviewresolver;} @Bean Public Tilesconfigurer tilesconfigurer () {tilesconfigurer tilesconfigurer = new Tilesconfigurer ();Tilesconfigurer. Setdefinitions(New string[] {"Classpath:tiles.xml"});Return Tilesconfigurer;}}
6. The Tiles view parser is also used in this example to replace the original include method
7. The full code has been uploaded
http://download.csdn.net/detail/u013816347/8998891
On the way to study, please comment.
Copyright NOTICE: This article is the original blogger article, please indicate the source when reproduced.
SPRINGMVC code-based configuration (0 configuration, no web. xml)