Idea creates a spring MVC Framework Java Web project that Gradle build

Source: Internet
Author: User
Tags java web

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 Tomcat

First, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.