`

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的配置。

 

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

分享到:
评论

相关推荐

    制定CA6140车床拨叉的加工工艺,设计钻φ5孔的钻床夹具设计.rar

    制定CA6140车床拨叉的加工工艺,设计钻φ5孔的钻床夹具设计.rar

    128 基于STM32的儿童误锁车内远程报警系统【QT上位机源码】.zip

    这是 《128 基于STM32的儿童误锁车内远程报警系统【QT上位机源码】》 项目的Qt上位机上位机源码包。 这是一个Qt工程,采用QT5.12.6版本开发的源码。支持生成Windows系统运行程序。也支持生成Android手机APP。 对应项目的博客链接:https://blog.csdn.net/xiaolong1126626497/article/details/132015856 注意 注意 注意!!!: 如果不需要修改上位机源码,就不用下载本资源 (本项目的STM32源码包里就包含了上位机APP安装包,可以直接使用),在设计文档里也写了上位机的核心代码。 如果想学习本项目的上位机开发,学习上位机的源码,修改源。那么可以下载。 最好自己具备一定的Qt开发基础。

    水泥粉磨生产工艺流程图.zip

    水泥粉磨生产工艺流程图.zip

    ParagonHFS+forWin v14.0.24 x64.rar

    WINDOWS系统读取苹果分区的利器,支持HFS+及APFS分区。

    基于Ryu 控制器和 Mininet 实现软件定义网络(SDN)负载均衡解决方案,用于网络模拟.zip

    基于Ryu 控制器和 Mininet 实现软件定义网络(SDN)负载均衡解决方案,用于网络模拟.zip

    20250415API翻譯

    20250415API翻譯

    Git知识学习(尚硅谷)

    Git知识学习(尚硅谷)

    手机充电器的模具设计.zip

    手机充电器的模具设计.zip

    原神弹琴脚本,让风告诉你,只需要pynput库即可使用

    python

    基于SpringBoot的体育商品推荐系统(源码+数据库+万字文档+ppt)535

    基于SpringBoot的体育商品推荐系统,系统包含两种角色:管理员、用户主要功能如下。 【用户功能】 1. **首页:** 浏览体育商品推荐系统的主要信息。 2. **商品信息:** 查看系统推荐的体育商品。 3. **交流论坛:** 参与用户间的体育商品讨论和交流。 4. **公告资讯:** 查看系统发布的重要通知和体育商品资讯。 5. **留言板:** 发表个人意见和留言,参与系统互动。 6. **购物车:** 查看已选购的体育商品,进行结算和下单。 7. **个人中心:** 管理个人信息,查看订单历史和进行相关操作。 【管理员功能】 1. **首页:** 查看体育商品推荐系统。 2. **个人中心:** 修改密码、管理个人信息。 3. **用户管理:** 审核和管理注册用户的信息。 4. **商品分类管理:** 管理体育商品的分类信息。 5. **商品信息管理:** 监管和管理体育商品的信息。 6. **交流论坛:** 管理用户间的讨论和交流,包括删除不当内容。 7. **留言板:** 管理用户的留言,进行适当的处理。 8. **系统管理:** - **轮播图管理:** 管理系统首页的轮播图,包括添加、编辑和删除。 - **关于我们:** 编辑和更新关于体育商品推荐系统的介绍。 - **公告资讯:** 发布、编辑和删除系统的通知和公告。 - **系统简介:** 提供体育商品推荐系统的简要介绍。 9. **订单管理:** - **已退款订单:** 查看和管理已退款的订单信息。 - **未支付订单:** 查看和管理未支付的订单信息。 - **已发货订单:** 查看和管理已发货但未完成的订单信息。 - **已支付订单:** 查看和管理已支付但未完成的订单信息。 - **已完成订单:** 查看和管理已完成的订单信

    ### 【物联网操作系统】LiteOS从入门到实战:开发环境搭建、内核解析及网络编程详解、LiteOS简介

    内容概要:本文详细介绍了LiteOS这一轻量级物联网操作系统,涵盖其特点、应用场景、开发环境搭建、内核机制、实战演练及进阶学习。LiteOS由华为开发,专为资源受限设备设计,具备轻量级、高效性、安全性和开放性等特点,适用于智能家居、工业自动化、智能穿戴和智能城市建设等领域。文章逐步讲解了Windows和Linux系统下搭建LiteOS开发环境的具体步骤,包括安装交叉编译器、HiSpark Studio、配置Python环境、下载并配置LiteOS SDK等。深入探讨了LiteOS内核的任务管理和内存管理机制,并通过Hello World程序展示了创建任务、编写代码、编译和烧录的完整流程。最后,介绍了SAL及socket编程,提供了丰富的学习资源,包括官方文档、技术论坛和开源代码库。 适合人群:具备一定编程基础,尤其是对物联网开发感兴趣的开发者,以及希望深入了解嵌入式操作系统原理的技术人员。 使用场景及目标:①学习如何在资源受限的设备上开发高效稳定的应用程序;②掌握LiteOS的任务管理、内存管理等核心机制;③通过实战演练和进阶学习,提高物联网设备的网络通信能力,如使用SAL及socket编程实现设备与服务器之间的TCP通信。 其他说明:本文不仅提供了理论知识,还结合具体代码示例和实际操作步骤,帮助读者更好地理解和应用LiteOS。物联网技术正处于快速发展阶段,掌握LiteOS开发技能将为开发者在智能家居、工业自动化、智能穿戴等领域提供强大的竞争力。

    Android14请求存储权限适配demo源码

    Android开发14版本请求存储权限,它有部分允许权限,有一点难度。

    在vs集成开发环境中,使用C/C++开发的游戏:球球大作战(注意要使用EasyX库)

    在vs集成开发环境中,使用C/C++开发的游戏:球球大作战(注意要使用EasyX库)

    【露天矿山边坡安全监测】基于技术规范的金属非金属露天矿山边坡安全监测系统设计与实施:变形、应力、爆破振动及水文气象监测方法和要求

    内容概要:本文档为《露天矿山边坡安全监测技术规范》,旨在规定金属非金属露天矿山采场边坡安全监测的原则、内容、方法和技术要求,涵盖变形监测、采动应力监测、爆破振动监测、降雨和地下水监测、视频监控、在线监测系统等方面。文档详细介绍了监测系统的安装、维护和监测资料的整理分析等管理要求。通过定义边坡分类、安全监测分级、监测要求和具体监测方法,确保露天矿山边坡的安全性和稳定性。 适用人群:适用于从事露天矿山边坡安全监测的设计、施工、管理和研究人员,以及矿山企业的安全管理人员。 使用场景及目标:①用于指导露天矿山边坡的安全监测工作,确保监测系统的设计、安装、调试和运行符合标准;②通过对边坡变形、应力、爆破振动、水文气象等进行监测,预防和控制边坡失稳事故的发生;③利用在线监测系统和数据分析,实现对边坡安全状况的实时监控和预警。 其他说明:本文档不适用于与煤共生、伴生的金属非金属露天矿山采场边坡。文档提供了详细的监测方法和要求,强调了监测系统的兼容性、可扩展性和数据的安全存储。此外,还特别强调了定期巡查和信息反馈机制的重要性,以确保监测系统的有效运行和及时响应异常情况。

    acacia_door_bottom.png

    acacia_door_bottom

    Android开发不用存储权限进行拍照demo源码

    Android开发不用存储权限进行拍照,得到拍照后的图片效果。有一点难度,关键是存储路径的定义。

    27910240_g.zip

    27910240_g.zip

    Android开发红包动画效果demo源码

    Android开发红包动画效果,红包在那左晃右晃,吸引你点击。

    一些demo使用ElmentPlus的一些demo

    资源demo

    black_concrete.png

    black_concrete

Global site tag (gtag.js) - Google Analytics