The simplest spring mvc + Maven project

First configure the pom file, you only need to reference three jar package files:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rybt.kpi</groupId>
    <artifactId>api</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <!-- constant -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.2.6.RELEASE</spring.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>


    <build>
        <finalName>api</finalName>
    </build>
</project>

  

web.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>rybt.kpi.api</display-name>


  <!-- The contextConfigLocation parameter is used to specify that the Spring Bean configuration file is configured in the WEB-INF directory by default -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/*.xml</param-value>
  </context-param>

  <!-- Configure the spring core listener, which will use /WEB-INF/applicationContext.xml as the configuration file by default -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>



  <servlet>
    <servlet-name>rybt.kpi.api</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- You can customize the location and name of the servlet.xml configuration file, the default is in the WEB-INF directory, the default name is [<servlet-name>]-servlet.xml, such as spring-servlet.xml -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>rybt.kpi.api</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>rybt.kpi.api</servlet-name>
    <url-pattern>/home/index</url-pattern>
  </servlet-mapping>

  <!-- Let the files with the following suffixes not take the mvc route-->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
  </servlet-mapping>


  <!-- Default page -->
  <welcome-file-list>
    <welcome-file>home/index</welcome-file>
  </welcome-file-list>

</web-app>

  Another spring-mvc.xml configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- The following is the location of the configuration scan package, the package name is com.maskkk, that is to say, our attempt parser should be placed under the com.maskkk package. -->
    <context:component-scan base-package="com.rybt.kpi.controller" />
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- prefix, our view files should be placed in the /WEB-INF/view/ directory, here we need to create a view folder under WEB-INF -->
        <property name="prefix" value="/WEB-INF/views/" />
        <!-- Set the suffix to .jsp -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

  Finally, write a class for the controller layer:

package com.rybt.kpi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/home")
public class HomeController {

    @RequestMapping(value="/index",method = {RequestMethod.GET,RequestMethod.POST})
    public String  index(Model model){
        return "home/index";
    }
}

By the way, you still need an index.jsp page, and you're done;

Finally, here is the code structure diagram:

 

Guess you like

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