SpringMVC的架设,使用Freemarker模板引擎
yinsitong
9年前
<p> 自行编译完spring4.0后,要架设以Freemaker为模板引擎的网站,同时还要整合Hibernate3.6。这当然要用Spring的MVC架构。我希望是注解模式完成,经过几次查询和修改,终于架设完成,现记录一下,以备后面使用。</p> <p> </p> <p>准备工作:自行编译Spring4,也可以网上下载已经编译好的包;下载Hibernate3.6;下载各类依赖包,以下为我列出的各类依赖包:</p> <pre> <code class="language-java">antlr-2.7.6.jar aopalliance-1.0.jar aspectj.jar c3p0-0.9.1.2.jar commons-collections-3.2.1.jar commons-dbcp-1.4.jar commons-logging-1.1.3.jar commons-pool-1.5.4.jar dom4j-1.6.1.jar freemarker.jar jstl.jar standard.jar slf4j-nop-1.7.5.jar mysql-connector-java-5.1.25-bin.jar</code></pre> <p> 其中的某些包不是必需的,如jstl.jar+standerd是为jstl模板准备的,可以不用添加;commons-dbcp-1.4.jar+commons-pool-1.5.4.jar是tomcat的mysql连接池,在本文中用的是c3p0连接池,所以tomcat的连接池也可以不用加入。这些包准备好后就要添加Spring4所有的包(除了一些test包),Hibernate3.6的核心包,还有Hibernate的jpa包。</p> <p>新建一个web项目后,这里用的是Tomcat7,相关的xml配置如下(这里配置只解析.html的文件)</p> <p>web.xml</p> <pre> <code class="language-java"><?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>mvc0201</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>triplogs</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>triplogs</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app></code></pre> <p>在web.xml的相同目录下新建一个triplogs-servlet.xml的文件,其内容如下:</p> <p>triplogs-servlet.xml</p> <pre> <code class="language-java"><?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启动事务管理的注解模式 --> <context:annotation-config /> <!-- Spring 的组件自动描的包的目录 --> <context:component-scan base-package="org.triplog.mvc0201" /> <!-- <aop:aspectj-autoproxy /> --> <!-- 数据库 --> <context:property-placeholder location="/WEB-INF/jdbc.properties" /> <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- Hibernate3 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- hibernate注解包扫描的位置 --> <property name="packagesToScan"> <list> <value>org.triplog.mvc0201.model</value> </list> </property> <!-- Hibernate配置 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- Spring对Hibernate的事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 以下为SpringMVC内容 --> <!-- 支持spring4.0新的mvc注解 --> <mvc:annotation-driven /> <!-- 启动Spring MVC的注解功能 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 地址映射 --> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> </bean> <!-- FreeMarker --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property name="contentType" value="text/html; charset=utf-8"/> </bean> <bean id="freeMarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <!-- 模板根目录 --> <property name="templateLoaderPaths" value="/static/templates/" /> <!-- 编码格式 --> <property name="defaultEncoding" value="utf-8" /> </bean> </beans></code></pre> <p> 因为已经配置了模板加载的默认根目录,所以不用配置模板前辍了,同时,个人认为必要配置后辍,所以后辍也省了。</p> <p>在web.xml的相同目录下新建jdbc.properties文件,添加以下内容:</p> <p>jdbc.properties</p> <pre> <code class="language-java">jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/sampledb?characterEncoding=utf8&useUnicode=true jdbc.username=root jdbc.password=123456</code></pre> <p>配置文件到此全部完成,现在写两个文件测试一下。新建包org.triplog.mvc0201.controller在该包下新建一个类HelloIndex,代码如下:</p> <p>HelloIndex.java:</p> <pre> <code class="language-java">package org.triplog.mvc0201.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloIndex { @RequestMapping({"/index.html","hello.html"}) public String Index(Model model){ model.addAttribute("message", "这是主页!"); return "hellotpl.html"; } }</code></pre> <p>在WEB-INF的同级目录新建如下的目录结构static/templates,在这个文件夹下新建一个html文件,命名为</p> <p>hellotpl.html, 内容如下:</p> <pre> <code class="language-java"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${message } -- Insert title here</title> </head> <body> <h5>这是主页。</h5> </body> </html></code></pre> <p>至此所有代码相关工作完成,把spirng和hibernate及其依赖包放入/WEB-INF/lib目录下,测试运行,结果如下:</p> <p><img alt="" src="https://simg.open-open.com/show/1e186ddc6950ea727e5eecb4839761f8.jpg" style="margin:0px auto"></p> <p>成功!</p> <p>来源:<a href="/misc/goto?guid=4959662344024234732">http://cleans.iteye.com/blog/1949611</a></p>