Maven+SpringMVC配置文件

项目目录:

1.修改web.xml

<?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">  <display-name>Archetype Created Web Application</display-name>  <!-- 服务器启动自动加载spring文件 -->  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext-*.xml</param-value>  </context-param>  <!--启动时,加载log4j2并向控制台打印日志信息-->  <context-param>    <param-name>log4jConfigLocation</param-name>    <param-value>classpath:log4j2.xml</param-value>  </context-param>  <!-- 配置spring的字符集编码格式 -->  <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>

  <!-- 监听spring -->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- springmvc配置 -->  <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:springmvc-servlet.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>  <!-- log4j2-begin -->  <listener>    <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>  </listener>  <filter>    <filter-name>log4jServletFilter</filter-name>    <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>log4jServletFilter</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>FORWARD</dispatcher>    <dispatcher>INCLUDE</dispatcher>    <dispatcher>ERROR</dispatcher>  </filter-mapping></web-app>

2.applicationContext-mybatis.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:jdbc="http://www.springframework.org/schema/jdbc"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">    <!--若springmvc中只扫描controller,则需要加上service,和dao的扫描功能-->    <context:component-scan base-package="com.tnmy.service"/>    <context:component-scan base-package="com.tnmy.dao"/>    <context:property-placeholder location="classpath:database.properties"/>    <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close" scope="singleton">        <property name="driverClassName" value="${driver}" />        <property name="url" value="${url}" />        <property name="username" value="${user}" />        <property name="password" value="${password}" />        <property name="initialSize" value="${initialSize}"/>        <property name="maxActive" value="${maxActive}"/>        <property name="maxIdle" value="${maxIdle}"/>        <property name="minIdle" value="${minIdle}"/>        <property name="maxWait" value="${maxWait}"/>        <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>        <property name="removeAbandoned" value="${removeAbandoned}"/>        <!-- sql  -->        <property name= "testWhileIdle" value="true"/>        <property name= "testOnBorrow" value="false"/>        <property name= "testOnReturn" value="false"/>        <property name= "validationQuery" value="select 1"/>        <property name= "timeBetweenEvictionRunsMillis" value="60000"/>        <property name= "numTestsPerEvictionRun" value="${maxActive}"/>    </bean>    <!--配置sqlSessionFactory-->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <property name="configLocation" value="classpath:mybatis-config.xml"/>        <property name="mapperLocations">            <list>                <value>classpath:mappers/*.xml</value>            </list>        </property>    </bean>    <!--扫描映射文件(mapper动态代理)-->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage"  value="com.tnmy.dao"/>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean>    <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"/>    </bean>

    <!-- 开启注解 -->    <mvc:annotation-driven/>    <!--transaction-manager="transactionManager"一定要加上,否则会报错,该配置是以事物的方式开启注解-->    <tx:annotation-driven transaction-manager="transactionManager"/></beans>

3.mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <settings>        <!-- changes from the defaults -->        <setting name="lazyLoadingEnabled" value="false" />    </settings>    <typeAliases>        <package name="com.tnmy.dao"/>    </typeAliases></configuration>
4.springmvc-servlet.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"       xmlns:tx="http://www.springframework.org/schema/tx"       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-3.2.xsd      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">    <!--需要扫描的包,注意,若spring配置文件 中不配置相关包的扫描功能,则此处只到cn.tnmy,否则会报错:错误的创建bean-->    <context:component-scan base-package="com.tnmy.controller"/>    <!--配置json字符集编码格式-->    <mvc:annotation-driven>        <mvc:message-converters>            <bean class="org.springframework.http.converter.StringHttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>application/json;charset=UTF-8</value>                    </list>                </property>            </bean>            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>text/html;charset=UTF-8</value>                        <value>application/json</value>                    </list>                </property>                <property name="features">                    <list>                        <value>WriteDateUseDateFormat</value>                    </list>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>    <!--访问需要访问的映射之前,先加载静态资源-->    <mvc:default-servlet-handler/>    <!--配置试图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean>    <!-- 配置文件上传  MultipartResolver-->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="maxUploadSize" value="500000000"/>        <property name="defaultEncoding" value="UTF-8"/>    </bean></beans>

原文地址:https://www.cnblogs.com/Forever-wind/p/12641470.html

时间: 2024-07-31 19:35:10

Maven+SpringMVC配置文件的相关文章

maven+springMVC+mybatis+junit详细搭建过程 【转】

springMVC+mybatis框架搭建 首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什么样的效果,自己的功能有哪些? (假设效果:项目目录结构清晰,能够查询到本地数据库中的内容..) 1.  工程目录结构整理清楚 在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean), cn.springmvc.dao(存放spring与mybatis连接接口), cn.springmvc.service(service接口), cn.spri

maven+springMVC+mybatis+junit详细搭建过程

springMVC+mybatis框架搭建 首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什么样的效果,自己的功能有哪些? (假设效果:项目目录结构清晰,能够查询到本地数据库中的内容..) 1.  工程目录结构整理清楚 在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean), cn.springmvc.dao(存放spring与mybatis连接接口), cn.springmvc.service(service接口), cn.spri

Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) ----DawnHeaven

Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)       ------DawnHeaven 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomcat 4.Maven  maven安装指南 二.构建工程 1.file->new->Project 2.next 3. next 4.next 5.finish 工程目录如下 三.完善项目 首先,完善目录,增加重要的source Folder,这个不是

maven+springmvc+dubbo+zookeeper

为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/ 一般 nginx+tomcat  | ----> Controller1--------->service1 请求----->nginx  |  |----->Controller2--------->service2 请求进了Controller 就只有一条路可以走了 使用dubbo后 | ------->servic

Maven+SpringMVC+Dubbo 简单的入门demo配置

之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo的介绍哈!DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点(其他的详细介绍可以查看dubbo的官网地址dubbo,写

IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子

很久之前写了一篇SSH搭建例子,由于工作原因已经转到SpringMVC+Mybatis,就以之前SSH实现简单登陆的例子,总结看看SpringMVC+Mybatis怎么实现. Spring一开始是轻量级的框架,在SSH中,处于中间粘合剂的作用,核心作用是IoC(控制反转).DI(依赖注入),IoC和DI是同一个概念,只是以不同角度进行解释.简单的说,就是Spring帮助你管理Bean,只要写好了配置文件或者Spring注解,那么Spring可以自动帮你创建Bean,不需要手动new.经过后来的发

1.Maven+SpringMVC+Eclipse软件安装配置,Maven报插件错误,Eclipse总是卡死的解决办法,导入一个maven工程后 一直显示importing maven project

 使用Maven+SpringMVC+Eclipse软件安装配置过程中的问题: 1.Eclipse总是卡死的解决办法: 一:内存不足所以会卡死,配置一下eclipse.ini修改这几个值就好了-XX:MaxPermSize=768m-Xms1024m-Xmx3072m搜索 二:修改window->preferences --- > JAVA->Edittor->Hovers,将右侧所有复选框清空. 2.因为eclipse上一次未正常关闭,导致启动卡死,错误日志为: [plain

用maven+springMVC创建一个项目 【转】

1.新建maven project 注册电气工程师收入将大幅上涨 [点击进入] 据悉,2016年随着政策变化,注册电气工程师 年薪收入将大幅增加,从平均30万/年的水准 查 看 Mars原班人马亲自授课 [点击进入] 育知专注HTML5/Android/iOS培训,月薪10000+ ,指定名师授课,拒绝新手讲师,一个月免费听. 查 看 3.选择webapp 服务器托管景安最快! [点击进入] 机房位于骨干节点,用户访问网站全国最快. 景安-河南最大的多线服务器托管商! 查 看 4.填写选项 注册

搭建项目Maven+springMVC+hibernate时,JUnit測试出现报ClassNotFoundException错误的解决

近期在搭建Maven+springMVC+hibernate项目,正常启动项目时一切正常.但JUNIT測试时出现报ClassNotFoundException错误,经过细致排查发现没有生成class文件. 如今解决例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDE2MTA4Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" &g