`

SpringMVC+MyBatis+Freemarker 简单框架搭建(一)

阅读更多

一、开发环境:

Eclipse、Tomcat、SVN等请参见如下的帖子,很详细了。

http://www.iteye.com/topic/982182

 

svn和maven插件的安装:

1、先安装gef插件

地址:http://download.eclipse.org/tools/gef/updates/interim/

2、安装svn插件

地址:http://subclipse.tigris.org/update_1.6.x

3、maven插件

m2eclipse-core Update 地址: http://m2eclipse.sonatype.org/sites/m2e

m2eclipse-extras Update 地市: http://m2eclipse.sonatype.org/sites/m2e-extras

4、安装可能出现的问题

 直接在线安装maven2 会出现依赖插件找不到的问题,无法安装。必须先安装gef 插件后才能安   装m2eclipse-core 插件,然而安装m2eclipse-extras 插件又依赖subclipse 插件。所以,三个插   件的正确的安装顺序是:gef插件 subclipse插件 m2eclipse插件

 

二、web.xml 和 applicationContext.xml 配置

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Spring MVC 配置 -->
    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:contextConfigLocation-springService.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 事件监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>    
    
    <!-- shiro 权限控制的过滤器 -->

	
	<!-- Spring 刷新Introspector防止内存泄露 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<!-- session超时定义,单位为分钟 -->
	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	
	<!-- 设置servlet编码开始 -->
	<filter>
		<filter-name>Set Character Encoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>GB2312</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>Set Character Encoding</filter-name>
		<url-pattern>/</url-pattern>
	</filter-mapping>
	<!-- 设置servlet编码结束 -->
    
    
    
    
  <display-name>Archetype Created Web Application</display-name>
</web-app>
  

<context-param>节点和<listener>节点是web项目启动时最先读取的两个节点,所以这两个节点里面所配置的内容是web项目启动时最先加载的部分。

<context-param>节点中配置的applicationContext.xml里面主要是数据库的配置信息,以及事务等等

<listener>节点配置的是web请求的监听器

 

2、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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       ">
       
    <!-- 自动注解除Controller以外的Component -->
    <context:component-scan base-package="com.weiluo.example">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 此配置可以让我们以${xxx}的形式来读取property.properties里面的信息 -->
    <!-- <context:property-placeholder/> -->
    
    <bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>
    
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url"
			value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>

		<!-- Connection Pooling Info -->
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="defaultAutoCommit" value="false" />
		<!-- 连接Idle一个小时后超时 -->
		<property name="timeBetweenEvictionRunsMillis" value="360000" />
		<property name="minEvictableIdleTimeMillis" value="360000" />
    </bean>
    
    <!-- 配置SessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref = "dataSource"
        p:configLocation = "classpath:sqlMapConfig.xml" />
    
    <!-- 采用spring与mybatis整合的第二种方法 -->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0"  ref="sqlSessionFactory" />
	</bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref = "dataSource" />
    
    <!-- MapperScanner配置,自动搜索mapper里面的对象,并注入 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage = "com.weiluo.example.entity" />
    
    <!-- 启动Spring注解事务 -->
    <tx:annotation-driven/>
    
</beans>
 

3、web.xml中的<servlet>节点配置的是与请求处理相关的一些内容,主要是 url路由处理器,视图解析器,等等,如下

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
       
    <!-- url映射拦截器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="order" value="1" />
	</bean>
	<!-- 配置数据格式转换器 -->
    <bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="jsonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="jsonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	</bean>
    
    <!-- 开启controller注解支持 -->
    <!-- 注:如果base-package=cn.javass 则注解事务不起作用 TODO 读源码 -->
    <context:component-scan base-package="com.weiluo.example.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
	<!-- 因为web-inf目录下面的静态资源文件是不能直接通过目录过去的,所以需要特殊声明-->
	<mvc:resources location="/static/" mapping="/static/**" />
    
    <!-- 视图解析器 -->
    <bean id="viewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true" />
		<property name="prefix" value="" />
		<property name="suffix" value=".html" />
		<property name="exposeSpringMacroHelpers" value="true" />
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
		<property name="requestContextAttribute" value="rc" />
		<property name="contentType" value="text/html;charset=GB2312" />
		<property name="order" value="0"/>  
	</bean>
	
    
    <!-- 自定义的freemarker标签 -->
	<bean id="blockDirective"
		class="com.weiluo.example.freemarker.directive.BlockDirective" />
	<bean id="extendsDirective"
		class="com.weiluo.example.freemarker.directive.ExtendsDirective" />
	<bean id="overrideDirective"
		class="com.weiluo.example.freemarker.directive.OverrideDirective" />
	<bean id="superDirective"
		class="com.weiluo.example.freemarker.directive.SuperDirective" />
    
    <!-- freemarker的配置项 -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/views/" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">5</prop>
				<prop key="defaultEncoding">GB2312</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="date_format">yyyy-MM-dd</prop>
				<prop key="time_format">HH:mm:ss</prop>
				<prop key="number_format">0.######</prop>
				<prop key="whitespace_stripping">true</prop>
			</props>
		</property>
		<property name="freemarkerVariables">
			<map>
				<entry key="extends" value-ref="extendsDirective"></entry>
				<entry key="override" value-ref="overrideDirective"></entry>
				<entry key="block" value-ref="blockDirective"></entry>
				<entry key="super" value-ref="superDirective"></entry>
			</map>
		</property>
	</bean>
    
    
</beans>

 

4、另外,一些常常变化的信息习惯于存放在application.properties文件里面,如:

#oracle version database setting
database=sqlserver
#jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.driver=net.sourceforge.jtds.jdbc.Driver
#jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:jtds:sqlserver://127.0.0.1:1433;databaseName=SpringMyBatisExample
#jdbc.url=jdbc:mysql://localhost:3306/springmvcdemo
jdbc.username=sa
jdbc.password=sasa

#dbcp settings
dbcp.maxIdle=5
dbcp.maxActive=20

 

到这里,一个spring mvc的基本框架就已经搭建起来了。

后续再完成数据库mybatis的配置以及freemarker的配置。

 

欢迎大家提出宝贵意见~~~

分享到:
评论

相关推荐

    springmvc+mybatis 和一个简单的freemarker代码生成工具

    SpringMVC作为Spring框架的一部分,是用于构建MVC(Model-View-Controller)架构的轻量级Web框架,而MyBatis则是一个优秀的持久层框架,它简化了数据库操作,FreeMarker则是一个动态模板引擎,常用于生成视图层的...

    SpringMVC+Mybatis,CRM系统教程和源代码(SSM项目)

    本教程将深入讲解如何利用SpringMVC和Mybatis两大主流技术框架构建一个完整的CRM系统,旨在帮助开发者理解和实践SSM(SpringMVC、Spring、Mybatis)集成开发。 **一、SpringMVC框架** SpringMVC是Spring框架的一个...

    ssm(Spring+SpringMVC+MyBatis)整合框架

    SSM框架,即Spring、SpringMVC和MyBatis的集成,是Java Web开发中常用的一种轻量级框架组合。这三种技术各有其特点,结合使用可以构建出高效、灵活的Web应用程序。下面我们将深入探讨这三个框架的原理、整合过程以及...

    Spring+SpringMVC+Mybatis+Velocity+Maven demo

    Spring、SpringMVC、Mybatis、Velocity和Maven是Java Web开发中常用的一组技术栈,它们各自在软件开发的不同层面发挥着重要作用。这个压缩包文件的标题和描述表明,它提供了一个集成这些技术的演示项目,下面我们将...

    maven+springmvc+mybatis+freemarker整合框架

    **maven** Maven是一个Java项目管理工具,它...总之,“maven+springmvc+mybatis+freemarker”的整合框架提供了一种高效、灵活的Web应用开发模式,它结合了现代Java开发的多个优秀组件,为开发者带来了极大的便利。

    用 SpringMVC+MyBatis 来搭建网站.7z

    在本文中,我们将深入探讨如何使用SpringMVC和MyBatis这两个流行的Java框架来搭建一个高效、可扩展的网站。SpringMVC作为Spring框架的一部分,主要用于处理Web应用程序的请求和响应,而MyBatis则是一个轻量级的持久...

    java spring+springMVC+mybatis基础框架

    Java Spring、SpringMVC和MyBatis是三个在企业级应用开发中广泛使用的开源框架,它们共同构建了一个强大的基础架构,使得开发人员可以更高效地进行Web应用的开发。Spring作为一个全面的轻量级容器,提供了依赖注入...

    【ssm框架】 spring+springMVC+mySQL+myBatis+freemarker+Maven小示例1

    SSM框架是Java web开发中常用的整合框架,由Spring、SpringMVC和MyBatis三个组件构成。这个项目示例提供了使用SSM框架构建Web应用的基础结构,让我们逐一解析其中涉及的技术点。 **Spring框架**:Spring是核心的...

    最新ssm:Spring + SpringMVC + Mybatis + FreeMarker 整合示例

    Spring + SpringMVC + Mybatis + FreeMarker 整合示例。所用jar包均是目前位置最新版本:201710最新版: spring mvc4.3.12 , mybatis: 3.4.5 , FreeMarker : 2.3.26。 有mysql数据库脚本。导入即可使用。有个简单的...

    idea+spring+springmvc+mybatis

    【标题】"idea+spring+springmvc+mybatis" 概述的是一个基于Java的Web开发集成框架,SSM(Spring、SpringMVC、MyBatis)。这是一个经典的后端技术栈,广泛应用于企业级应用开发中。下面将详细介绍这三个组件以及它们...

    SpringMvc+mybatis框架结合的一个练习项目

    在这个"SpringMvc+mybatis框架结合的一个练习项目"中,开发者利用 SpringMvc 处理 Web 请求,通过 Mybatis 进行数据操作。项目包含了论坛和聊天系统,这涉及到用户认证、消息传递、数据存储等多个方面,充分展示了这...

    springmvc+mybatis整合项目

    SpringMVC作为Spring框架的一部分,提供了强大的MVC(Model-View-Controller)架构支持,而MyBatis则是一个轻量级的持久层框架,它将SQL语句与Java代码解耦,提高了开发效率和可维护性。接下来,我们将深入探讨如何...

    springmvc+mybatis+分页查询

    在本项目中,我们主要探讨的是如何利用Spring MVC和MyBatis两大流行框架构建一个具有分页查询功能的Web应用。Spring MVC作为控制层,负责处理HTTP请求和响应,而MyBatis则作为数据访问层,用于处理数据库交互。下面...

    springmvc+mybatis+redis+shiro+bootstrap+freemarker+jsp+logback框架

    springmvc+mybatis+redis+shiro+bootstrap+freemarker+jsp+logback框架,几乎集合了所有企业开发技术,可以下载下来直接使用,任务调度,登陆拦截应有尽有

    spring+springMVC+mybatis+log4j框架整合

    在IT行业中,构建高效、可维护的Web应用是至关重要的,而Spring、SpringMVC和Mybatis这三大框架的整合正是实现这一目标的常用手段。本文将深入探讨这些框架的集成以及log4j日志系统的应用。 首先,Spring框架作为...

    Springmvc+dubbo+mybatis+mysql+redis

    标题 "Springmvc+dubbo+mybatis+mysql+redis" 描述了一个基于Java技术栈的分布式微服务架构。在这个系统中,SpringMVC作为前端控制器处理HTTP请求,Dubbo用于服务治理,MyBatis是持久层框架,MySQL是关系型数据库,...

    学生系统平台的SpringMVC+MyBatis代码

    在本项目中,"学生系统平台的SpringMVC+MyBatis代码" 是一个教育管理系统的实现,旨在帮助开发者和学习者深入理解如何整合SpringMVC框架与MyBatis持久层框架来构建高效、可维护的Web应用。下面将详细阐述这两个技术...

    spring+springmvc+mybatis+shiro+freemarker+dubbo

    标题 "spring+springmvc+mybatis+shiro+freemarker+dubbo" 涵盖的是一个基于Java的完整Web应用程序开发框架。这个框架结合了Spring、SpringMVC、MyBatis、Shiro、FreeMarker以及Dubbo的核心技术,用于构建高效、可...

    Spring+SpringMVC+Mybatis CXF

    总的来说,这个压缩包提供了一个学习和实践SSM(Spring、SpringMVC、Mybatis)和CXF集成的平台,帮助开发者掌握企业级Java应用开发的关键技能,包括服务化、Web接口设计、数据库操作和框架整合等。通过研究这些文件...

    springMVC+mybatis搭建框架

    在IT行业中,构建高效、可扩展的Web应用是至关重要的,SpringMVC和MyBatis作为两个非常流行的Java开发框架,常被结合使用以实现这一目标。本文将深入探讨如何利用这两个框架搭建一个完整的Web应用。 SpringMVC是...

Global site tag (gtag.js) - Google Analytics