SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(一):maven WEB 项目的搭建 | 掘金技术征文...

原本地址:springMVC干货系列:从零搭建springMVC+mybatis(一):maven WEB项目的搭建
博客地址:tengj.top/

前言

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

正文

开发环境介绍

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

好了,基本环境只要上面这些就行,maven不需要安装,只要有解压包就行。MyEclipse 2014有自带的插件,我们只要指定下路径就行。

MyEclipse 2014中Maven的配置

打开myeclipse中的 Window=>Preferences=>Myeclipse=>Maven4Myeclipse
配置jdk,这里我们选择1.7

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

指定User Settings,对应settings.xml以及本地仓库地址,我这边是建在盘,路径为E:\m2\repository

创建WEB项目

新建一个web项目

配置项目基本信息,项目名,java EE,java版本,其中记得要勾上Add maven support,这样才会生成带有maven的工程。


创建成功后出正确的项目结构应该是这样

配置需要的安装包

下面列出目前需要用到包,直接在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.0modelVersion>
  <groupId>SpringMVCMybatisgroupId>
  <artifactId>SpringMVCMybatisartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
  <name>SpringMVCMybatisname>
  <description/>
  <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
          <spring.version>4.1.1.RELEASEspring.version>
          <cxf.version>2.7.15cxf.version>
        <slf4j-version>1.7.12slf4j-version>
        <log4j-version>1.2.17log4j-version>
        <mybatis-version>3.3.0mybatis-version>
        <mybatis-spring-version>1.2.3mybatis-spring-version>
  properties>
  <dependencies>
      
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>${log4j-version}version>
    dependency>
    <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-log4j12artifactId>
        <version>${slf4j-version}version>
    dependency>
      <dependency>
        <groupId>org.slf4jgroupId>
        <artifactId>slf4j-apiartifactId>
        <version>${slf4j-version}version>
    dependency>
    <dependency>
        <groupId>javax.servletgroupId>
        <artifactId>javax.servlet-apiartifactId>
        <version>3.1.0version>
    dependency>
    <dependency>
       <groupId>org.apache.commonsgroupId>
       <artifactId>commons-lang3artifactId>
       <version>3.3version>
    dependency>
    <dependency>
       <groupId>com.alibabagroupId>
       <artifactId>fastjsonartifactId>
       <version>1.2.1version>
    dependency>
    
    <dependency>
        <groupId>com.mchangegroupId>
        <artifactId>c3p0artifactId>
        <version>0.9.5.1version>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.37version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatis-springartifactId>
        <version>${mybatis-spring-version}version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>${mybatis-version}version>
    dependency>
    <dependency>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-coreartifactId>
        <version>1.3.2version>
    dependency>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-coreartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-contextartifactId>
        <version>4.1.1.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-beansartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-jdbcartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>${spring.version}version>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-txartifactId>
        <version>${spring.version}version>
    dependency>
    
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.11version>
        <scope>testscope>
    dependency>
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-testartifactId>
        <version>4.1.1.RELEASEversion>
        <scope>testscope>
    dependency>

    <dependency>
      <groupId>javaxgroupId>
      <artifactId>javaee-apiartifactId>
      <version>7.0version>
      <scope>providedscope>
    dependency>
    <dependency>
      <groupId>org.glassfish.webgroupId>
      <artifactId>javax.servlet.jsp.jstlartifactId>
      <version>1.2.2version>
    dependency>
  dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-pluginartifactId>
        <version>2.3.2version>
        <configuration>
          <source>1.7source>
          <target>1.7target>
        configuration>
      plugin>
      <plugin>
        <artifactId>maven-war-pluginartifactId>
        <version>2.2version>
        <configuration>
          <version>3.1version>
          <failOnMissingWebXml>falsefailOnMissingWebXml>
        configuration>
      plugin>
    plugins>
  build>
project>复制代码

配置SpringMVC需要的配置文件

xml配置,注意里面配置springMvc需要用到的DispatcherServlet,指向的地址是classpath:spring/applicationContext.xml,当然此时还没创建这个applicationCVontext.xml,下一个步骤来创建。

<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>springmvctouchbaidudisplay-name>
    <servlet>
        <servlet-name>springmvctouchbaiduservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/applicationContext.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvctouchbaiduservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <session-config>
        <session-timeout>60session-timeout>
    session-config>
web-app>复制代码

在src/main/resources下面创建个spring目录用来存放applicationContext.xml
后期会用的的配置很多,所以我一般会根据不同用途创建不同别名的spring配置文件,然后用applicationContext.xml统一起来。
这里我创建了 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-8value>
                        <value>application/json;charset=utf-8value>
                        <value>application/x-www-form-urlencodedvalue>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>    
beans>复制代码

其中一些重要的配置下篇文章详细的说明

context:component-scan 用来扫描指定的路径下的包,来加载组件用的
mvc:resources 指定静态资源路径

实现一个简单的springMVC请求

配置文件配置好了,我们就来测试一下,写一个controller来实现前后台交互
在src/main/java下面创建如下格式的文件夹。这是我习惯的写法。
common用来存放公共的类
controller控制层
service层用来处理业务逻辑
dao层用来处理数据库操作
dto层用来存放mybatis根据表自动生成的实体对象
具体结构如下图:

在controller包下面创建一个类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+"/";
%>


  
    
    My JSP 'index.jsp' starting page
    
    
        
    
    
  
  
        跳转进来,表示springMVC流程成功!
  
复制代码

部署到tomcat启动服务看看 输入地址是否控制台会打印“进来了”这3字,并跳转到index界面
http://localhost:8080/springmvctouchbaidu/sendBaidu/view
大家看,如图表示springMVC整体流程成功了


总结

到此,基于Maven的SpringMVC WEB项目搭建算是成功了,后续会基于这个基础添加其他的功能,比如整合数据库框架mybatis,多数据源处理,定时任务等等。下篇文章将整体上介绍springMVC的配置文件以及注解标签的使用。


一直觉得自己写的不是技术,而是情怀,一篇篇文章是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你少走弯路,希望我能帮你抹去知识的蒙尘,希望我能帮你理清知识的脉络,希望未来技术之巅上有你也有我。

掘金技术征文第三期:聊聊你的最佳实践

你可能感兴趣的:(java,测试,开发工具)