首页 > 代码库 > 基于maven从头搭建springMVC框架

基于maven从头搭建springMVC框架

0.准备工作

首先将eclipse和需要的插件准备好,例如maven插件,spring IDE插件。

1.建立maven下的webapp项目

  1.新建一个maven项目,类型为webapp,如下图

  2.然后给项目命名,加入groupId等

  3.配置项目的发布目录,在 Deployment Assemly下,如图

2.配置Spring和Maven

  1.配置pom.xml,添加如下包依赖。版本不一定要对应,后边可能会用到些新的包,缺少哪些包可以后续去百度然后加入到pom.xml中

     <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.1.2.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.1.2.RELEASE</version>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.1.0</version>        </dependency>

  2.配置web.xml文件,添加ContextLoaderListener监听器,在servlet启动时会去装配制定配置文件的配置;然后添加springDispatcherServlet,指定mvc配置文件,配置mvc框架。全部配置如下:

<!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>  <display-name>Archetype Created Web Application</display-name>  <!-- needed for ContextLoaderListener -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>        /WEB-INF/spring-context.xml,        /WEB-INF/spring-hibernate.xml        </param-value>    </context-param>    <!-- Bootstraps the root web application context before servlet initialization -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->    <servlet>        <servlet-name>springDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- 设置监听位置,‘/‘为全部监听 -->    <servlet-mapping>        <servlet-name>springDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

  3.配置spring-mvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd        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-4.1.xsd">    <!-- 激活@controller模式 -->    <mvc:annotation-driven />    <!-- 配置包扫描位置(会在此包下扫描@controller控制器) -->    <context:component-scan base-package="com.test.maven.controller" />    <!-- 配置视图解析器(jsp文件前缀后缀) -->    <bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <value>/WEB-INF/jsp/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean>    <!-- 配置tiles模板(没有用到tiles可以不用配置此项) -->    <bean id="tilesConfigurer"        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">        <property name="definitions">            <list>                <value>/WEB-INF/tiles/tiles-definitions.xml</value>            </list>        </property>    </bean></beans>

  4.配置spring-context.xml。暂时只建立最简单的框架,所以这里暂时可以不用配置。

到此最基本的springMVC框架就搭建好了。把项目装入tomcat,运行访问项目的主目录,就会调用默认的index.jsp,显示hello world了。

 

基于maven从头搭建springMVC框架