maven初始搭建一个基础项目(spring mvc+spring+jdbc mysql+jstl)

技术选型:

一.项目搭建:

1)创建maven项目 (我博客里面有介绍)

  选择aptach的maven-archetype-webapp

  填入groupIDhe artifactId等

  确认项目名称

  maven插件会自动生成项目结构

2)添加其他目录

  在/src/main下添加java目录(命名自己定),设置为源码根目录

  注:有需要的话可以在src目录下添加测试相关代码的目录

  建立如下目录结构(自己定)

    com.xx.common

    com.xx.vip

        .entity

        .dao

        .function

        .web

        -formbean

        -handler

  在webapp下建立static(放静态资源) 在webapp/WEB-INF/views(放jsp页面)

  注意:web.xml版本头一定是3.0以上的

 3)修改项目和模块的语言级别为java1.8

  默认的LanguageLevel和JavaCompiler都是1.5

  需要在pom.xml中添加如下代码,制定maven变异插件maven-compiler-plugin的版本

在<build>标签中设置

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    <plugin>
<plugins>

二.集成springMVC

 在http://mvnrepository.com/网站搜索依赖库

1)在pom.xml中添加依赖

  spring-mvc

  servlet-api

  jstl

2)在src/main/resources目录下添加spring-mvc.xml配置文件

  a)添加注解驱动<mvc:annotation-driven />

  b)注册视图解析器

  c) 扫描mvc组件

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 使用注解来请求映射 -->
    <mvc:annotation-driven/>

  <!-- 配置自动扫描包 -->
    <context:component-scan base-package="com.cc8w.Controller"></context:component-scan>
 
    <bean  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <!-- 重定向是否添加上下文路径-->
        <property name="redirectContextRelative" value="true"></property>  
        <property name="prefix" value="/WEB-INF/view/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean> 
    

</beans>    

3)在web.xml中配置spring-mvc前端控制器DispatcherServlet

  a)配置随服务启动而初始化

  b)配置参数contextConfigLocation,指向spring-mvc的路径(默认在WEB-INF/和servlet-Name一样)

  c)配置servlet-mapping(可以仅处理*.do请求)

<!-- 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>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

4)web.xml配置请求和应答字符编码处理器(配置过滤器)

   CharacterEncodingFilter

<filter>
    <description>请求和应答字符编码过滤器</description>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <!--注意这里和下面配置一致-->
    <servlet-name>spring-mvc</servlet-name>
</filter-mapping>

5)配置404,50X,欢迎等特殊页面(忽略)

6)编写测试案例

三.集成spring

1)添加spring依赖

  其实没有,因为添加spring-mvc时,已经把spring的核心添加了

2)编写配置文件spring-context.xml(applicationContext.xml)

   配置注解事务管理

  扫描业务层组件(配置上下文扫描)

3)在web.xml中配置ContextLoaderListener监听器,启用Spring容器

  配置ContextConfigLocation,指定spring-context.xml路径

  在web.xml配置监听器(在filter和servlet中间)

<listener>
    <descripting>启动spring容器</descripting>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context.xml</param-value>
</context-param>

四.集成c3p0

1)添加依赖

  c3p0,jdbc-mysql

2)在spring-context中定义c3p0数据源 CombopooledDaraSource

  配置属性

    基础信息

    driverClass/jdbcurl/user/password

    链接数相关

    initialPoolSize/minPoolSize/maxPoolSize/acquireIncrement

    其他属性请参见相关文档

    

原文地址:https://www.cnblogs.com/fps2tao/p/8629404.html