springmvc整合freemarker配置的详细步骤
程序员文章站
2024-02-19 22:03:46
一、对应的导包(有些包是不必须的)
一、对应的导包(有些包是不必须的)
<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/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.ming</groupid> <artifactid>ssm</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</version> <name>ssm maven webapp</name> <url>http://maven.apache.org</url> <properties> <!-- junit测试包版本号 --> <junit.version>4.11</junit.version> <!-- spring版本号 --> <spring.version>3.0.2.release</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <mybatis-spring>1.2.2</mybatis-spring> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> <!-- jackson版本 --> <jackson.version>1.9.13</jackson.version> <!-- 上传下载 --> <commons-fileupload>1.3.1</commons-fileupload> <commons-io>2.4</commons-io> <commons-codec>1.9</commons-codec> <!-- freemarker模板包版本 --> <freemarker.version>2.3.23</freemarker.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- spring核心包 --> <!-- 1)spring核心依赖 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <!--2)spring dao层依赖 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <!-- 3)srping web依赖 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <!--4) 映射支持依赖 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-oxm</artifactid> <version>${spring.version}</version> </dependency> <!-- 5)spring 测试依赖 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> </dependency> <!-- mybatis依赖 --> <!--1) mybatis核心包 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>${mybatis.version}</version> </dependency> <!--2) mybatis spring支持依赖 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>${mybatis-spring}</version> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupid>javax</groupid> <artifactid>javaee-api</artifactid> <version>7.0</version> </dependency> <!-- 导入mysql数据库链接jar包 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.30</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationcontext.xml中配置数据库 --> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version>1.2.2</version> </dependency> <!-- jstl标签类 --> <dependency> <groupid>jstl</groupid> <artifactid>jstl</artifactid> <version>1.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.1.41</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>${slf4j.version}</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入json --> <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-mapper-asl</artifactid> <version>${jackson.version}</version> </dependency> <!-- 上传组件包 --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>${commons-fileupload}</version> </dependency> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>${commons-io}</version> </dependency> <dependency> <groupid>commons-codec</groupid> <artifactid>commons-codec</artifactid> <version>${commons-codec}</version> </dependency> <!-- freemarker依赖 --> <dependency> <groupid>org.freemarker</groupid> <artifactid>freemarker</artifactid> <version>${freemarker.version}</version> </dependency> </dependencies> <build> <finalname>mymavens</finalname> </build> </project>
二、在spring配置文件中配置
<?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:p="http://www.springframework.org/schema/p" 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.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-4.0.xsd"> <!-- 自动扫描该包,使springmvc认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="com.controller" /> <!--避免ie执行ajax时,返回json出现下载文件 --> <bean id="mappingjacksonhttpmessageconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter"> <property name="supportedmediatypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> <!-- 启动springmvc的注解功能,完成请求和注解pojo的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter"> <property name="messageconverters"> <list> <ref bean="mappingjacksonhttpmessageconverter" /> <!-- json转换器 --> </list> </property> </bean> <!-- freemarker的配置 --> <bean id="freemarkerconfig" class="org.springframework.web.servlet.view.freemarker.freemarkerconfigurer"> <property name="templateloaderpath" value="/web-inf/ftl/" /> <property name="defaultencoding" value="utf-8" /> <property name="freemarkersettings"> <props> <prop key="template_update_delay">10</prop> <prop key="locale">zh_cn</prop> <prop key="datetime_format">yyyy-mm-dd</prop><!-- 时间格式化 --> <prop key="date_format">yyyy-mm-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <!-- 视图配置 在list中按照配置的先后顺序 --> <bean class="org.springframework.web.servlet.view.contentnegotiatingviewresolver"> <property name="ignoreacceptheader" value="true" /> <property name="defaultcontenttype" value="text/html" /> <property name="mediatypes"> <map> <entry key="json" value="application/json" /> <entry key="xls" value="application/vnd.ms-excel" /> <entry key="xlsx" value="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <entry key="pdf" value="application/pdf" /> </map> </property> <property name="favorparameter" value="false" /> <property name="viewresolvers"> <list> <!-- 配置freemarker视图解析器 --> <bean class="org.springframework.web.servlet.view.freemarker.freemarkerviewresolver"> <property name="cache" value="true" /> <property name="prefix" value="" /><!-- 上面已经配了,这里就不用配啦 --> <property name="suffix" value=".ftl" /> <property name="contenttype" value="text/html;charset=utf-8" /> <property name="viewclass" value="org.springframework.web.servlet.view.freemarker.freemarkerview" /> <property name="allowsessionoverride" value="true" /> <property name="allowrequestoverride" value="true" /> <property name="exposespringmacrohelpers" value="false" /> <property name="exposerequestattributes" value="true" /> <property name="exposesessionattributes" value="true" /> <property name="requestcontextattribute" value="request" /> </bean> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" /> <property name="prefix" value="/web-inf/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> </bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <!-- 默认编码 --> <property name="defaultencoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxuploadsize" value="10485760000" /> <!-- 内存中的最大值 --> <property name="maxinmemorysize" value="40960" /> </bean> </beans>
三、jdbc连接 jdbc.properties
driver=com.mysql.jdbc.driver url=jdbc:mysql://127.0.0.1:3306/student username=root password= initialsize=0 maxactive=20 maxidle=20 minidle=1 maxwait=60000
四、log4j配置
log4j.rootlogger=info,console,file #\u5b9a\u4e49\u65e5\u5fd7\u8f93\u51fa\u76ee\u7684\u5730\u4e3a\u63a7\u5236\u53f0 log4j.appender.console=org.apache.log4j.consoleappender log4j.appender.console.target=system.out #\u53ef\u4ee5\u7075\u6d3b\u5730\u6307\u5b9a\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\uff0c\u4e0b\u9762\u4e00\u884c\u662f\u6307\u5b9a\u5177\u4f53\u7684\u683c\u5f0f log4j.appender.console.layout = org.apache.log4j.patternlayout log4j.appender.console.layout.conversionpattern=[%c] - %m%n #\u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6 log4j.appender.file = org.apache.log4j.rollingfileappender #\u6307\u5b9a\u8f93\u51fa\u76ee\u5f55 log4j.appender.file.file = logs/ssm.log #\u5b9a\u4e49\u6587\u4ef6\u6700\u5927\u5927\u5c0f log4j.appender.file.maxfilesize = 10mb # \u8f93\u51fa\u6240\u4ee5\u65e5\u5fd7\uff0c\u5982\u679c\u6362\u6210debug\u8868\u793a\u8f93\u51fadebug\u4ee5\u4e0a\u7ea7\u522b\u65e5\u5fd7 log4j.appender.file.threshold = all log4j.appender.file.layout = org.apache.log4j.patternlayout log4j.appender.file.layout.conversionpattern =[%p] [%d{yyyy-mm-dd hh\:mm\:ss}][%c]%m%n
与本项目无关的mybatis配置
<?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:p="http://www.springframework.org/schema/p" 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.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-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="com.*" /> <!-- 引入配置文件 --> <bean id="propertyconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialsize" value="${initialsize}"></property> <!-- 连接池最大数量 --> <property name="maxactive" value="${maxactive}"></property> <!-- 连接池最大空闲 --> <property name="maxidle" value="${maxidle}"></property> <!-- 连接池最小空闲 --> <property name="minidle" value="${minidle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxwait" value="${maxwait}"></property> </bean> <!-- spring和mybatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperlocations" value="classpath:com/mapping/*.xml"></property> </bean> <!-- dao接口所在包名,spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.dao" /> <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property> </bean> <!-- (事务管理)transaction manager, use jtatransactionmanager for global tx --> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> </beans>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: c++与c#的时间转换示例分享
下一篇: java从键盘输入数字并判断大小的方法
推荐阅读
-
springmvc整合freemarker配置的详细步骤
-
java Spring整合Freemarker的详细步骤
-
java Spring整合Freemarker的详细步骤
-
java Spring整合Freemarker的详细步骤
-
Spring5.1源码解析(二)spring整合springmvc去配置化的原理
-
深入phpMyAdmin的安装与配置的详细步骤_PHP
-
在CentOS6.4中安装配置LAMP环境的详细步骤_PHP教程
-
Spring Boot整合elasticsearch的详细步骤
-
MySQL5.7的安装与配置详细操作步骤
-
使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)