SpringMVC源码环境搭建

249 阅读1分钟

在Spring源码环境的基础上(juejin.cn/post/685457…

这里的方法就是搭建一个web工程(未出现SpringBoot前就是这样搭建java web工程的),其中的Spring依赖指向Spring-Framework下的相应模块即可。web容器使用tomcat即可。

操作步骤:

1. 在Spring-Framework目录下新建一个web子模块,如图一图二。

2.创建完成后,文件结构如图三, 缺少的部分自行创建即可。

3. build.gradle文件内容如图四。

4. 由于是自建的web子模块, web.xml会报错,如图五处理即可。

5. tomcat容器自行配置即可。

6. 图三中的代码见文末。

                                                                           图一

                                                                           图二

                                                                           图三

                                                                           图四

                                                                           图五

package com.hello.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("hi")
    @ResponseBody
    public String hi(){
        return "hi, springmvctest";
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <!--配置springmvc核心servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping></web-app>

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--配置组件扫描-->
    <context:component-scan base-package="com.hello"/>
    <!--处理器映射器,处理器解析器-->
    <mvc:annotation-driven />
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--视图所在文件路径-->
        <property name="prefix" value="/WEB-INF/webpages/" />
        <!--视图文件类型-->
        <property name="suffix" value=".jsp" />
    </bean>
    <!--静态资源释放-->
    <mvc:default-servlet-handler />
</beans>