自行编译完spring4.0后,要架设以Freemaker为模板引擎的网站,同时还要整合Hibernate3.6。这当然要用Spring的MVC架构。我希望是注解模式完成,经过几次查询和修改,终于架设完成,现记录一下,以备后面使用。
准备工作:自行编译Spring4,也可以网上下载已经编译好的包;下载Hibernate3.6;下载各类依赖包,以下为我列出的各类依赖包:
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
其中的某些包不是必需的,如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包。
新建一个web项目后,这里用的是Tomcat7,相关的xml配置如下(这里配置只解析.html的文件)
web.xml
<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>mvc0201display-name> <welcome-file-list> <welcome-file>index.htmlwelcome-file> <welcome-file>index.htmwelcome-file> <welcome-file>index.jspwelcome-file> <welcome-file>default.htmlwelcome-file> <welcome-file>default.htmwelcome-file> <welcome-file>default.jspwelcome-file> welcome-file-list> <servlet> <servlet-name>triplogsservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>triplogsservlet-name> <url-pattern>*.htmlurl-pattern> servlet-mapping> web-app>
在web.xml的相同目录下新建一个triplogs-servlet.xml的文件,其内容如下:
triplogs-servlet.xml
<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 /> <context:component-scan base-package="org.triplog.mvc0201" /> <context:property-placeholder location="/WEB-INF/jdbc.properties" /> <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> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>org.triplog.mvc0201.modelvalue> list> property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialectprop> <prop key="hibernate.show_sql">trueprop> props> property> bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> bean> <tx:annotation-driven transaction-manager="transactionManager" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> bean> <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>
因为已经配置了模板加载的默认根目录,所以不用配置模板前辍了,同时,个人认为必要配置后辍,所以后辍也省了。
在web.xml的相同目录下新建jdbc.properties文件,添加以下内容:
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sampledb?characterEncoding=utf8&useUnicode=true
jdbc.username=root
jdbc.password=123456
配置文件到此全部完成,现在写两个文件测试一下。新建包org.triplog.mvc0201.controller在该包下新建一个类HelloIndex,代码如下:
HelloIndex.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";
}
}
在WEB-INF的同级目录新建如下的目录结构static/templates,在这个文件夹下新建一个html文件,命名为
hellotpl.html, 内容如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${message } -- Insert title heretitle> head> <body> <h5>这是主页。h5> body> html>
至此所有代码相关工作完成,把spirng和hibernate及其依赖包放入/WEB-INF/lib目录下,测试运行,结果如下:
成功!
以上摘自的推酷网上的,感觉不错,特意摘下来!
http://www.tuicool.com/articles/3Iva6j
推酷是摘自网友的博客:
http://cleans.iteye.com/blog/1949611?utm_source=tuicool