SpringMVC干貨系列:從零搭建SpringMVC+mybatis(一):maven WEB 項目的搭建

前言

今天手把手來教大家如何搭建基于Maven的SpringMVC WEB項目。

正文

開發(fā)環(huán)境介紹

IDE: MyEclipse 2014
jdk: 1.7
maven:apache-maven-3.1.1

好了,基本環(huán)境只要上面這些就行,maven不需要安裝,只要有解壓包就行。MyEclipse 2014有自帶的插件,我們只要指定下路徑就行。

MyEclipse 2014中Maven的配置

打開myeclipse中的 Window=>Preferences=>Myeclipse=>Maven4Myeclipse
配置jdk,這里我們選擇1.7


配置好maven路徑,也就是你之前存放maven解壓包的路徑


指定User Settings,對應settings.xml以及本地倉庫地址,我這邊是建在盤,路徑為E:\m2\repository。

創(chuàng)建WEB項目

新建一個web項目


配置項目基本信息,項目名,java EE,java版本,其中記得要勾上Add maven support,這樣才會生成帶有maven的工程。



創(chuàng)建成功后出正確的項目結(jié)構(gòu)應該是這樣


配置需要的安裝包

下面列出目前需要用到包,直接在pom.xl里面添加就行

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SpringMVCMybatis</groupId>
  <artifactId>SpringMVCMybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>SpringMVCMybatis</name>
  <description/>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <spring.version>4.1.1.RELEASE</spring.version>
          <cxf.version>2.7.15</cxf.version>
        <slf4j-version>1.7.12</slf4j-version>
        <log4j-version>1.2.17</log4j-version>
        <mybatis-version>3.3.0</mybatis-version>
        <mybatis-spring-version>1.2.3</mybatis-spring-version>
  </properties>
  <dependencies>
      <!-- 日志配置-->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j-version}</version>
    </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-lang3</artifactId>
       <version>3.3</version>
    </dependency>
    <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>fastjson</artifactId>
       <version>1.2.1</version>
    </dependency>
    <!-- 數(shù)據(jù)源配置 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.1</version>
    </dependency>
    <!--數(shù)據(jù)庫相關(guān), mysql, mybatis-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis-version}</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- spring用到的包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!--測試-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.1.1.RELEASE</version>
        <scope>test</scope>
    </dependency>
        
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.web</groupId>
      <artifactId>javax.servlet.jsp.jstl</artifactId>
      <version>1.2.2</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <version>3.1</version>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

配置SpringMVC需要的配置文件

xml配置,注意里面配置springMvc需要用到的DispatcherServlet,指向的地址是classpath:spring/applicationContext.xml,當然此時還沒創(chuàng)建這個applicationCVontext.xml,下一個步驟來創(chuàng)建。

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

    <display-name>springmvctouchbaidu</display-name>
    <servlet>
        <servlet-name>springmvctouchbaidu</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvctouchbaidu</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

在src/main/resources下面創(chuàng)建個spring目錄用來存放applicationContext.xml
后期會用的的配置很多,所以我一般會根據(jù)不同用途創(chuàng)建不同別名的spring配置文件,然后用applicationContext.xml統(tǒng)一起來。
這里我創(chuàng)建了 applicationContext.xml以及applicationContext-mvc.xml
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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <import resource="classpath:spring/applicationContext-mvc.xml" />
</beans>

applicationContext-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: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-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.xsd"
       default-lazy-init="true">
    <context:component-scan base-package="com.tengj.demo"/>
    <mvc:resources location="/WEB-INF/pages/" mapping="/pages/**"/>
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 默認的注解映射的支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=utf-8</value>
                        <value>application/json;charset=utf-8</value>
                        <value>application/x-www-form-urlencoded</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>    
</beans>

其中一些重要的配置下篇文章詳細的說明

context:component-scan 用來掃描指定的路徑下的包,來加載組件用的
mvc:resources 指定靜態(tài)資源路徑

實現(xiàn)一個簡單的springMVC請求

配置文件配置好了,我們就來測試一下,寫一個controller來實現(xiàn)前后臺交互
在src/main/java下面創(chuàng)建如下格式的文件夾。這是我習慣的寫法。
common用來存放公共的類
controller控制層
service層用來處理業(yè)務邏輯
dao層用來處理數(shù)據(jù)庫操作
dto層用來存放mybatis根據(jù)表自動生成的實體對象
具體結(jié)構(gòu)如下圖:


在controller包下面創(chuàng)建一個類SendToBaiduController

package com.tengj.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/sendBaidu")
public class SendToBaiduController {
    @RequestMapping(value="/view",method = RequestMethod.GET)
    public String index(){
        System.out.println("進來了");
        return "index";
    }
    
}

index.jsp代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
  <body>
        跳轉(zhuǎn)進來,表示springMVC流程成功!
  </body>
</html>

部署到tomcat啟動服務看看 輸入地址是否控制臺會打印“進來了”這3字,并跳轉(zhuǎn)到index界面
http://localhost:8080/springmvctouchbaidu/sendBaidu/view
大家看,如圖表示springMVC整體流程成功了


總結(jié)

到此,基于Maven的SpringMVC WEB項目搭建算是成功了,后續(xù)會基于這個基礎(chǔ)添加其他的功能,比如整合數(shù)據(jù)庫框架mybatis,多數(shù)據(jù)源處理,定時任務等等。下篇文章將整體上介紹springMVC的配置文件以及注解標簽的使用。


一直覺得自己寫的不是技術(shù),而是情懷,一篇篇文章是自己這一路走來的痕跡??繉I(yè)技能的成功是最具可復制性的,希望我的這條路能讓你少走彎路,希望我能幫你抹去知識的蒙塵,希望我能幫你理清知識的脈絡(luò),希望未來技術(shù)之巔上有你也有我。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 前言 上篇文章介紹了maven WEB 項目的搭建,基本的配置文件也都貼出來了,今天就來介紹下SpringMVC的...
    嘟爺MD閱讀 4,007評論 9 72
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 喜歡你為我們的安全而伸出的手,我的心暖暖的 喜歡你為求助的我給予無私奉獻,我的心充滿愛 喜歡你做事果斷,不拖泥帶水...
    夏夢LC閱讀 261評論 0 0
  • ‘每月利潤穩(wěn)定2萬,漲50%薪水就立刻虧損’。這店長現(xiàn)在就月4萬的工資啊?。。∵@例子的數(shù)據(jù)有問題。 不過雙贏談判一...
    大夢張吉玲閱讀 216評論 0 0

友情鏈接更多精彩內(nèi)容