IDEA creates a Spring MVC framework Java Web project, Gradle builds

Note: Some details of this article have not been written. This article mainly writes about important links and points that need attention.

New Project

         Select Gradle, check java and web. After that, set the project path and name, so I won't go into details here.

New interface

        Import the required packages under the build.gradle file and refresh it.

  1 group 'com.benjious.createTest'
  2 version '1.0-SNAPSHOT'
  3 
  4 apply plugin: 'java'
  5 apply plugin: 'war'
  6 
  7 sourceCompatibility = 1.8
  8 
  9 repositories {
 10     mavenCentral()
 11 }
 12 
 13 dependencies {
 14     testCompile group: 'junit', name: 'junit', version: '4.11'
 15     testCompile group: 'junit', name: 'junit', version: '4.12'
 16 
 17 
 18     compile'org.springframework:spring-context:4.3.6.RELEASE'
 19     compile'org.springframework:spring-webmvc:4.3.6.RELEASE'
 20     compile'org.springframework:spring-test:4.3.6.RELEASE'
 21 }
 22 

 

Directory Structure

         Set up project structure

final directory

       Post the code for each class

  1 public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  2 
  3     @Override
  4     protected Class<?>[] getRootConfigClasses() {
  5         return new Class[]{RootConfig.class};
  6     }
  7 
  8     @Override
  9     protected Class<?>[] getServletConfigClasses() {
 10         return new Class[]{WebConfig.class};
 11     }
 12 
 13     @Override
 14     protected String[] getServletMappings() {
 15         return new String[] { "/" };
 16     }
 17 }
 18 
  1 //exclude : 过滤掉
  2 @Configuration
  3 @ComponentScan(basePackages = {"createtest"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})
  4 public class RootConfig {
  5 
  6 }
  7 
  1 @Configuration
  2 @EnableWebMvc
  3 @ComponentScan("createtest.web")
  4 public class WebConfig extends WebMvcConfigurerAdapter {
  5 
  6 
  7     @Bean
  8     public ViewResolver viewResolver() {
  9         InternalResourceViewResolver resolver = new InternalResourceViewResolver();
 10         resolver.setPrefix("/WEB-INF/view/");
 11         resolver.setSuffix(".jsp");
 12         return resolver;
 13     }
 14 
 15 
 16     @Override
 17     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
 18         configurer.enable();
 19     }
 20 
 21 }
  1 @Controller
  2 public class HomeControl {
  3 
  4     @RequestMapping("/home")
  5     public String home() {
  6         return "home";
  7     }
  8 
  9 }
  1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2 <html>
  3 <head>
  4     <title>Home</title>
  5 </head>
  6 <body>
  7 <p>home界面</p>
  8 </body>
  9 </html>

 

Configure Tomcat

         First configure Tomcat, the following is the configured interface

configure tomcat

 Configure Tomcat1

        Run the project and visit  http://localhost:8080/home/

        interface

        What is needed here is to set the Application Context in the configuration of Tomcat. For example, if the Application Context is "/home", then the root address of the server of the project is:

http://localhost:8080/home/ , then to display the home.jsp interface should be: http://localhost:8080/home/ home .

 

refer to:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339036&siteId=291194637