First experience of Maven project in IntelliJ IDEA and building Spring MVC framework

Reminder : The GitHub address of this tutorial is " intellij-idea-tutorial ", welcome to children's shoes Star, Fork, and make corrections.

In the article " Detailed IntelliJ IDEA to create a Maven project and set the java source directory ", we have completed the framework of the Maven project in IntelliJ IDEA. Following on from the above, in this article, we go one step further and run our web project with Tomcat.

structure

As shown in the figure above, we further expanded the structure of the project, javacreated a series of directory levels in the annotationdirectory, and created a Java class named in the directory AnnotationControllerto test the Spring MVC framework; in the WEB-INFdirectory, created a new one The pagesdirectory is used to store jspthe page, and a new springmvc-servlet.xmlfile named is created to write the configuration items of the Spring MVC framework. Next, let's take a look at the contents of these files in turn:

  • Control class:AnnotationController
package com.hit.action.demo.annotation;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * author:Charies Gavin
 * https:github.com/guobinhit
 */
@Controller
public class AnnotationController {

    // 请求映射地址 http://localhost:8080/gitcode/test,其中 8080 为默认端口
    @RequestMapping(value = "/test")
    public String goTest(HttpServletRequest request) {
        // 输出请求 URL 路径
        System.out.println(request.getRequestURL());
        // 返回逻辑名
        return "index";
    }
}
  • Spring MVC configuration file:springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

    <!-- 声明注解开发方式 -->
    <mvc:annotation-driven/>

    <!-- 包自动扫描 -->
    <context:component-scan base-package="com.hit.action"/>

    <!-- 内部资源视图解析器,前缀 + 逻辑名 + 后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  • Global configuration file:web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 如果修改 Spring 配置文件的位置和名称,则通过以下方式进行声明全局配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </context-param>

    <!-- 配置 Spring 监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置 DispatcherServlet 对 url 进行过滤 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 显示配置用于解析 DispatcherServlet 的配置文件 -->
        <!-- 如果不显示指定,则 Spring MVC 会自动扫描 WEB-INF 下以 servlet-name 标签声明的名称开头以 servlet 结尾的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
        </init-param>
        <!-- 显示指定加载顺序-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 如果声明 /* 则会拦截所有请求,包括 action 返回的 .jsp 页面 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 配置欢迎页 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

As shown in the figure above, we


———— ☆☆☆ ——Back- >The easiest IntelliJ IDEA tutorial in history<- Contents—— ☆☆☆ ————

Guess you like

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