springmvc项目搭建

文章目录


一、基于xml的SpringMVC

  1. 创建Maven项目

  2. Add Framework Support > Web Application

  3. 添加pom依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    </dependencies>
    
  4. 编写web.xml配置文件

    <?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_4_0.xsd"
             version="4.0">
        <!--配置DispatcherServlet-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--关联springmvc的配置文件-->
            <!--此配置项可以不添加,可以将配置文件放在WEB-INF目录下,文件名必须是(DispatcherServlet的servlet-name)-servlet.xml-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
        </servlet>
        <!--匹配servlet的请求,/标识匹配所有请求-->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!--/*和/都是拦截所有请求,/会拦截的请求不包含*.jsp,而/*的范围更大,还会拦截*.jsp这些请求-->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    
  5. 创建applicationContext.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd 
           http://www.springframework.org/schema/context 
           https://www.springframework.org/schema/context/spring-context.xsd">
    
        <bean id="hello" class="com.controller.HelloController"></bean>
    </beans>
    
  6. 添加jsp-api、servlet-api依赖

    <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
  7. 创建HelloController.java

    package com.controller;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * com.controller
     *
     * @author sjh
     * @version 1.0v
     *
     * y
     */
    public class HelloController implements Controller {
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            //创建对象
            ModelAndView mv = new ModelAndView();
            //添加视图名称,要跳转的前端页面名称
            mv.setViewName("hello");
            //向前端页面添加的属性值
            mv.addObject("test", "test hello");
            return mv;
        }
    }
    
  8. WEB-INF目录下创建page/hello.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>hello</title>
    </head>
    <body>
    springmvc helloworld
    ${test}
    </body>
    </html>
    
  9. 此时访问http://localhost:8080/springmvc_helloworld_war_exploded/hello异常
    springmvc项目搭建
    project structure > artifacts,选择WEB-INF > create directory,创建lib目录
    springmvc项目搭建
    重新运行,访问报错500,但实际HelloController的代码已经执行,故此时需要配置视图解析器。

  10. applicationContext.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd 
           http://www.springframework.org/schema/context 
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/WEB-INF/page/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
        <bean id="hello" class="com.controller.HelloController"></bean>
    </beans>
    

    此时访问http://localhost:8080/springmvc_helloworld_war_exploded/hello正常。

二、基于注解的SpringMVC

  1. pom.xml
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.3.RELEASE</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    
    </dependencies>
    
  2. web.xml
    <?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_4_0.xsd"
             version="4.0">
    
        <!--配置DispatcherServlet-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--关联springmvc的配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
        </servlet>
        <!--匹配servlet的请求,/标识匹配所有请求-->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!--/*和/都是拦截所有请求,/会拦截的请求不包含*.jsp,而/*的范围更大,还会拦截*.jsp这些请求-->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    
  3. applicatinContext.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com"></context:component-scan>
        <!--视图解析器-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--配置前缀-->
            <property name="prefix" value="/WEB-INF/page/"></property>
            <!--配置后缀-->
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>
    
  4. HelloController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.Map;
    
    /**
     * com.controller
     * @version 1.0v
     *
     */
    @Controller
    public class HelloController {
    
        /*
         * @RequestMapping就是用来标识此方法用来处理什么请求,其中的/可以取消
         * 取消后默认也是从当前项目的根目录开始查找,一般在编写的时候看个人习惯
         * 同时,@RequestMapping也可以用来加在类上,
         * */
        @RequestMapping("hello")
        public String hello(Map<String, String> map) {
            map.put("test", "test springmvc hello annotation");
            return "hello";
        }
    }
    

三、tomcat的web.xml与WEB-INF下的web.xml

  1. tomcat的web.xml核心配置

    <?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_4_0.xsd"
      version="4.0">
    
        <servlet>
            <servlet-name>default</servlet-name>
            <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
            <init-param>
                <param-name>debug</param-name>
                <param-value>0</param-value>
            </init-param>
            <init-param>
                <param-name>listings</param-name>
                <param-value>false</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet>
            <servlet-name>jsp</servlet-name>
            <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
            <init-param>
                <param-name>fork</param-name>
                <param-value>false</param-value>
            </init-param>
            <init-param>
                <param-name>xpoweredBy</param-name>
                <param-value>false</param-value>
            </init-param>
            <load-on-startup>3</load-on-startup>
        </servlet>
    
        <!-- The mapping for the default servlet -->
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <!-- The mappings for the JSP servlet -->
        <servlet-mapping>
            <servlet-name>jsp</servlet-name>
            <url-pattern>*.jsp</url-pattern>
            <url-pattern>*.jspx</url-pattern>
        </servlet-mapping>
    
      <!-- ==================== Default Session Configuration ================= -->
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
      
      <!-- ==================== Default Welcome File List ===================== -->
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
    </web-app>
    
  2. WEB-INF的web.xml

    <?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_4_0.xsd"
             version="4.0">
    
        <!--配置DispatcherServlet-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!--关联springmvc的配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext.xml</param-value>
            </init-param>
        </servlet>
        <!--匹配servlet的请求,/标识匹配所有请求-->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!--/*和/都是拦截所有请求,/会拦截的请求不包含*.jsp,而/*的范围更大,还会拦截*.jsp这些请求-->
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    
  3. DispatcherServlet的url-pattern的配置
    每个项目下的web.xml会继承tomcat的web.xml,tomcat的web.xml中的defaultServlet和jspServlet分别处理静态资源请求和jsp请求。
    DispatcherServlet的url-pattern配置/时会覆盖tomcat的defaultServlet,此时就不能访问webapp下的静态资源了,所有的非jsp请求均会由DispatcherServlet进行处理;/*会覆盖defaultServlet和jspServlet,此时jsp请求不会交由tomcat的jspServlet进行处理,而是交由DispatcherServlet进行处理

    项目结构如下:
    springmvc项目搭建
    配置/时:
    http://localhost:8080/springmvc_hello_annotation_war_exploded/test.jsp 正常访问
    http://localhost:8080/springmvc_hello_annotation_war_exploded/test.html 访问异常404
    那如何解决这个问题呢?可以在WEB-INF的web.xml中配置defaultServlet

    配置/*时:
    http://localhost:8080/springmvc_hello_annotation_war_exploded/test.jsp 访问异常404
    http://localhost:8080/springmvc_hello_annotation_war_exploded/test.html 访问异常404

上一篇:SpringMVC:请求域对象的数据共享


下一篇:MySQL自增ID用完了会怎么样?