Note: Some details of this article did not write, this article mainly write important links and areas needing attention, light spray
New Project
Select Gradle, and check Java and the Web. After that, you set the project path and name, which is not verbose here.
Build.gradle file to import the required packages, refresh.
1Group ' com.benjious.createTest '2Version ' 1.0-snapshot '3 4Apply plugin: ' java '5Apply plugin: ' War '6 7sourcecompatibility = 1.88 9repositories {TenMavencentral () One} A -dependencies { -Testcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.11 ' theTestcompile group: ' JUnit ', Name: ' JUnit ', Version: ' 4.12 ' - - -Compile ' org.springframework:spring-context:4.3.6.release ' +Compile ' org.springframework:spring-webmvc:4.3.6.release ' -Compile ' org.springframework:spring-test:4.3.6.release ' +} A
Directory structure
Set up the project structure
Post code for each class
1 Public classMywebinitializerextendsAbstractannotationconfigdispatcherservletinitializer {2 3@Override4 protectedClass<?>[] Getrootconfigclasses () {5 return NewClass[]{rootconfig.class};6}7 8@Override9 protectedClass<?>[] Getservletconfigclasses () {Ten return NewClass[]{webconfig.class}; One} A -@Override - protectedString[] Getservletmappings () { the return NewString[] {"/" }; -} -} -
1//exclude: Filter out 2 @Configuration 3 @ComponentScan (basepackages = { "createtest"},excludefilters = {@ComponentScan. Filter (type = Filtertype.annotation,value = Enablewebmvc. Class)}) 4publicclass RootConfig { 5 6 } 7
1@Configuration2@EnableWebMvc3@ComponentScan ("Createtest.web")4 Public classWebconfigextendsWebmvcconfigureradapter {5 6 7@Bean8 PublicViewresolver Viewresolver () {9Internalresourceviewresolver resolver =NewInternalresourceviewresolver ();TenResolver.setprefix ("/web-inf/view/"); OneResolver.setsuffix (". JSP"); A returnResolver -} - the -@Override - Public voidConfiguredefaultservlethandling (Defaultservlethandlerconfigurer configurer) { -Configurer.enable (); +} - +}
1 @Controller 2publicclass Homecontrol { 3 4 @RequestMapping ("Home") 5public String () { 6 return "home"; 7 } 8 9 }
1 <%@ page contenttype= "text/html;charset=utf-8" language= "java"%> 2 3 4 <title>Home</title> 5 6 <body> 7 <p>home interface </p> 8 </body> 9
Configure TomcatFirst, Tomcat is configured, and the following is a well-configured interface
Run the project, Access http://localhost:8080/home/
What is needed here is to configure Tomcat, set application context, such as application context as "/home", then the root address of the project's server is:
http://localhost:8080/home/, then to display the Home.jsp interface should be: Http://localhost:8080/home/home.
Reference:
- "Sring in Action" SPITRR project
- Spring Example
Idea creates a spring MVC Framework Java Web project that Gradle build