`
周一Monday
  • 浏览: 345110 次
  • 来自: 北京
社区版块
存档分类
最新评论

Spring3.1整合FreeMarker2.3.19

阅读更多

使用的技术:

Spring+Freemarker

 

其中Spring包括SpringJdbc、声明式事务、SpringMVC、(单元测试没写,偷个懒)...

View层使用就是FreeMarker+HTML

 

搭建开发环境:

 

Spring开发需要的
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.test-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
org.springframework.web.servlet-3.1.0.RELEASE.jar

 

com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.junit-4.7.0.jar

 

整合FreeMarker需要此Jar
org.springframework.context.support-3.1.0.RELEASE.jar

 

FreeMarker核心jar文件(就一个)
freemarker.jar

 

数据库驱动
ojdbc14.jar

 

开发:

 

数据库脚本:

--创建商品表
create table guestbook(
	id		varchar2(255) primary key,
	name	varchar2(255),
	author  varchar2(255),
	price	number(6,2),
	quantity number,
	description	varchar2(255)
);


--主键采用UUID,由程序给出

 

持久层:

package org.chendl.freemarkercrud.dao.impl;

import java.io.Serializable;
import java.sql.Types;
import java.util.List;

import org.chendl.freemarkercrud.dao.ProductDao;
import org.chendl.freemarkercrud.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ProductDaoImpl implements ProductDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(Product product) {
		String sql = "INSERT INTO product(id,name,author,price,quantity,description)VALUES(?,?,?,?,?,?)";
		Object[] args = { product.getId(), product.getName(),
				product.getAuthor(), product.getPrice(), product.getQuantity(),
				product.getDescription() };
		int[] argTypes = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
				Types.DECIMAL, Types.INTEGER, Types.VARCHAR };
		jdbcTemplate.update(sql, args, argTypes);
	}

	@Override
	public void update(Product product) {
		String sql = "UPDATE product SET name=?,author=?,price=?,quantity=?,description=? WHERE id=?";
		Object[] args = { product.getName(), product.getAuthor(),
				product.getPrice(), product.getQuantity(),
				product.getDescription(), product.getId() };
		int[] argTypes = { Types.VARCHAR, Types.VARCHAR, Types.DECIMAL,
				Types.INTEGER, Types.VARCHAR, Types.VARCHAR };
		jdbcTemplate.update(sql, args, argTypes);
	}

	@Override
	public void delete(Serializable id) {
		String sql = "DELETE FROM product  WHERE id=?";
		Object[] args = { id };
		int[] argTypes = { Types.VARCHAR };
		jdbcTemplate.update(sql, args, argTypes);
	}

	@Override
	public Product findById(Serializable id) {
		String sql = "SELECT * FROM product WHERE id=?";
		List<Product> list = jdbcTemplate.query(sql,
				BeanPropertyRowMapper.newInstance(Product.class), id);
		return list.get(0);
	}

	@Override
	public List<Product> findAll() {
		String sql = "SELECT * FROM product";
		List<Product> list = jdbcTemplate.query(sql,
				BeanPropertyRowMapper.newInstance(Product.class));
		return list;
	}
}

 

业务层很简单就是调用Dao而已,具体的看附件

 

Web层:

package org.chendl.freemarkercrud.web.action;

import java.util.List;

import org.chendl.freemarkercrud.domain.Product;
import org.chendl.freemarkercrud.service.ProductService;
import org.chendl.freemarkercrud.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;

	/** 列表 */
	@RequestMapping("/list.action")
	public ModelAndView list() {
		List<Product> list = productService.getAll();
		return new ModelAndView("list", "list", list);
	}

	/** 添加页面 */
	@RequestMapping("/saveUI.action")
	public String saveUI() {
		return "save";
	}

	/** 添加 */
	@RequestMapping("/save.action")
	public String save(Product product) {
		product.setId(ToolUtil.getUUID());  //设置主键UUID
		productService.save(product);
		return "redirect:list.action";
	}

	/** 编辑页面 */
	@RequestMapping("/editUI.action")
	public ModelAndView editUI(String id) {
		Product product = productService.getById(id);
		return new ModelAndView("edit", "product", product);
	}

	/** 编辑 */
	@RequestMapping("/edit.action")
	public String edit(Product product) {
		productService.update(product);
		return "redirect:list.action";
	}

	/** 删除 */
	@RequestMapping("/delete.action")
	public String delete(String id) {
		productService.delete(id);
		return "redirect:list.action";
	}
}

 

配置:

1.beans.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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/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">
						
	<!-- 加载属性文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>	
	
	<!-- 配置包扫描器 -->
	<context:component-scan base-package="org.chendl.freemarkercrud.dao.impl"/>	
	<context:component-scan base-package="org.chendl.freemarkercrud.service.impl"/>	
						
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- jdbcTemplate -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 配置事务 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
	

</beans>

 

2.springmvc-servlet.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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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/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">

	<context:component-scan base-package="org.chendl.freemarkercrud.web.action" />

	<!-- FreeMarker环境配置 -->
	<bean id="freeMarkerConfigurer"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">0</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
			</props>
		</property>
	</bean>

	<!-- FreeMarker视图解析 -->
	<bean id="freeMarkerViewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="false" />
		<property name="prefix" value="" />
		<property name="suffix" value=".ftl" />
		<property name="contentType" value="text/html;charset=UTF-8"/>
	</bean>



</beans>

 

3.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"
	id="WebApp_ID" version="2.5">

	<!-- 加载Spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>

	<!-- Spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 设置字符集 -->
	<!-- 
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	 -->

	<!-- 配置SpringMVC -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

	<!-- 配置freemarker -->
	<servlet>
		<servlet-name>freemarker</servlet-name>
		<servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>freemarker</servlet-name>
		<url-pattern>*.ftl</url-pattern>
	</servlet-mapping>

	<!-- 配置首页 -->
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>

 

视图:

 

index.html

<meta http-equiv="Refresh" content="0;url=list.action">

 

common.ftl

<#macro page title>
	<html>
		<head>
			<title>test freemarker ${title?html}</title>
		</head>
		<body>
			<h1>${title?html}</h1>
			<hr/>
			<#nested>
		</body>
	</html>
</#macro>

 

list.ftl

 

<#import "/common/common.ftl" as com>
<#escape x as x?html>
<@com.page title="FreeMarker Demo">
<center>
	<a href="saveUI.action">save</a>
	<#if list?size=0>
		<p>Nothing...</p>
	<#else>
		<p>List</p>
		<table border="0" cellspacing="2" cellpadding="2" width="100%">
			<tr align="center" valign=""top>
				<th bgcolor="#C0C0C0">Id</th>
				<th bgcolor="#C0C0C0">Name</th>
				<th bgcolor="#C0C0C0">Author</th>
				<th bgcolor="#C0C0C0">Price</th>
				<th bgcolor="#C0C0C0">Quantity</th>
				<th bgcolor="#C0C0C0">Description</th>
				<th bgcolor="#C0C0C0" colspan="2">operation</th>
			</tr>
		<#list list as e>
			<tr align="center" valign=""top>
				<td bgcolor="#E0E0E0">${e.id}</td>
				<td bgcolor="#E0E0E0">${e.name}</td>
				<td bgcolor="#E0E0E0">${e.author}</td>
				<td bgcolor="#E0E0E0">${e.price}</td>
				<td bgcolor="#E0E0E0">${e.quantity}</td>
				<td bgcolor="#E0E0E0">${e.description}</td>
				<td bgcolor="#E0E0E0">
					<a href="editUI.action?id=${e.id}">edit</a>
					<a href="delete.action?id=${e.id}" onclick="return confirm('Are you sure?');">delete</a>
				</td>
			</tr>
		</#list>	
		</table>
	</#if>
</center>	
</@com.page>	
</#escape>

 

save.ftl

<#import "/common/common.ftl" as com>
<#escape x as x?html>
<@com.page title="FreeMarker Demo">
<form action="save.action" method="post">
	<table border="0" cellspacing="2" cellpadding="2" width="50%">
		<tr>
			<td bgcolor="#C0C0C0">Name</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="name"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Author</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="author"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Price</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="price"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Quantity</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="quantity"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Description</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="description"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">&nbsp;</td>
			<td bgcolor="#E0E0E0">
				<input type="submit" value="save"/>
				<input type="button" value="cancel" onclick="location.href='list.action'"/>
			</td>
		</tr>
	</table>
</form>	
</@com.page>	
</#escape>

 

edit.ftl 与 save.ftl很像

 

<#import "/common/common.ftl" as com>
<#escape x as x?html>
<@com.page title="FreeMarker Demo">
<form action="edit.action" method="post">
	<input type="hidden" name="id" value="${product.id}"/>
	<table border="0" cellspacing="2" cellpadding="2" width="50%">
		<tr>
			<td bgcolor="#C0C0C0">Name</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="name" value="${product.name}"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Author</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="author" value="${product.author}"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Price</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="price" value="${product.price}"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Quantity</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="quantity" value="${product.quantity}"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">Description</td>
			<td bgcolor="#E0E0E0"><input  type="text" name="description" value="${product.description}"/></td>
		</tr>
		<tr>
			<td bgcolor="#C0C0C0">&nbsp;</td>
			<td bgcolor="#E0E0E0">
				<input type="submit" value="edit"/>
				<input type="button" value="cancel" onclick="location.href='list.action'"/>
			</td>
		</tr>
	</table>
</form>	
</@com.page>	
</#escape>

 

以上的是关键代码。

 

今天也是自己第一天学FreeMarker。上面写的就当做自己的一份笔记。

 

感性觉得ftl比jsp快,第一次运行几乎不需要等待,而且配置很灵活。

 

但是还有一个问题没有解决:乱码问题。google后,也没找到太好的方法,可能没找对。

 

留个疑问吧... 希望有“达人”留言...不胜感激...

 

 

分享到:
评论
3 楼 周一Monday 2012-11-21  
zengshaotao 写道
更改tomcat的编码字符集即可

如果可以不依赖外部容器就更好了
2 楼 zengshaotao 2012-11-21  
更改tomcat的编码字符集即可
1 楼 周一Monday 2012-05-16  
晕,事务注解@Transactional忘记写了。

相关推荐

    SSH 配置实例: Spring 3.1 + Hibernate 4.2 + Struts 2.3

    5. **配置视图**:Struts 2默认支持JSP作为视图,也可以通过FreeMarker(`freemarker-2.3.19.jar`)等模板引擎来渲染视图。视图层应保持轻量,避免过多的业务逻辑。 6. **测试与部署**:确保所有配置正确无误后,...

    struts+spring+hibernate整合包

    5. Freemarker:`freemarker-2.3.19.jar`是FreeMarker模板引擎,它是一个强大的、非侵入式的模板语言,用于生成HTML、XML等文本格式的输出。在Struts2中,FreeMarker常作为视图层技术,分离视图和业务逻辑。 6. ...

    Spring3+Hibernate4+Struts2 jar包 SSH框架

    freemarker-2.3.19.jar google-collections-1.0.jar guava-12.0.jar hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.6.Final.jar hibernate-entitymanager-4.1.6.Final.jar hibernate-envers-...

    SSH(Spring Struts Hibernate)所有jar包

    1.1.24-20120814.043343-7.jar freemarker-2.3.19.jar hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.2.1.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate-validator-5.1.0.Final....

    Spring集成struts以及hibernate jar包

    标题中的"Spring集成struts以及hibernate jar包"指的是在Java Web开发中,将Spring框架与Struts2和Hibernate框架进行整合的过程。这通常是为了构建一个完整的MVC(Model-View-Controller)架构,其中Spring负责依赖...

    SSM整合jar包详解

    `freemarker-2.3.19.jar`则是一个模板引擎,负责生成HTML或其他类型的文本输出。其他如`commons-lang3-3.1.jar`提供对Java标准库的扩展,`commons-fileupload-1.2.2.jar`支持文件上传,而`commons-logging-1.1.1.jar...

    ssh框架整合包

    Freemarker (`freemarker-2.3.19.jar`) 是一个模板引擎,常用于生成HTML或其他类型的文本输出。在SSH框架中,它可以与Struts2结合,帮助开发者创建动态视图。 Ehcache (`ehcache-2.8.3.jar`) 是一个内存缓存系统,...

    Spring3+Hibernate4+Struts2 jar包

    2013/03/21 18:20 931,168 freemarker-2.3.19.jar 2013/03/21 18:20 639,592 google-collections-1.0.jar 2013/03/21 18:20 1,795,936 guava-12.0.jar 2013/03/21 18:20 81,271 hibernate-commons-annotations-4.0.1...

    java ssh框架整合包

    - `freemarker-2.3.19.jar`:FreeMarker是一个模板引擎,常用于Struts2中的视图层,生成动态HTML或其他格式的文档。 - `xwork-core-2.3.4.1.jar`:XWork是Struts2的基础,提供动作和拦截器处理的核心库。 - `...

    ssh(struts2.3.4+spring3.2+hibernate4.1.1)整合中jar包作用介绍

    ### SSH (Struts2.3.4 + Spring3.2 + Hibernate4.1.1) 整合中JAR包作用介绍 #### 一、SSH综述 SSH框架指的是Struts2、Spring与Hibernate三个开源项目的集成应用。这种集成不仅提高了开发效率,还提升了应用程序的...

    SSHjar包 .rar

    freemarker-2.3.19.jar hibernate3.jar hibernate-jpa-2.0-api-1.0.1.Final.jar javassist-3.11.0.GA.jar javassist-3.12.0.GA.jar jta-1.1.jar log4j-1.2.17.jar mysql-connector-java-5.1.7-bin.jar ognl-3.0.6....

    一名初学者关于SSH整合的问题

    - **freemarker-2.3.19.jar**:FreeMarker是一个模板引擎,可以用来生成动态HTML或其他文本,常用于Spring MVC中的视图渲染。 - **javassist-3.18.1-GA.jar**:Javassist是一个处理字节码的库,Hibernate用它在运行...

    web开发中常用的jar包

    freemarker-2.3.19.jar hibernate-jpa-2.0-api-1.0.1.Final.jar hibernate3.jar javassist-3.11.0.GA.jar javassist-3.12.0.GA.jar jta-1.1.jar log4j-1.2.17.jar mail.jar mysql-connector-java-5.0.4-bin.jar ognl...

    Struts2+spring+jdbc+mysql搭建

    * freemarker-2.3.19.jar * javassist-3.11.0.GA.jar * ognl-3.0.6.jar * strut2-core-2.3.12.jar * xwork-core-2.3.12.jar 将这些包导入到 Eclipse 工程的 lib 文件夹中,确保 Struts2 框架正确集成。 二、配置 ...

    ssh 整合案例及所需jar包

    - `freemarker-2.3.19.jar`: 模板引擎。 - `ognl-3.0.5.jar`: OGNL表达式语言。 - `mysql-connector-java-5.1.18-bin.jar`: MySQL JDBC驱动。 - `Oracle10gDriver.jar`: Oracle JDBC驱动。 - `sqljdbc2008.jar`...

    SSM所有用到的jar包作用说明

    7. `freemarker-2.3.19.jar`: FreeMarker是一个模板引擎,用于生成HTML或其他文本输出。 8. `ognl-3.0.5.jar`: Object-Graph Navigation Language,用于表达式语言,Struts2中的数据绑定和表达式评估依赖于它。 9....

    ssh+mysql55jar包集合

    /xscjManager/WebContent/WEB-INF/lib/freemarker-2.3.19.jar /xscjManager/WebContent/WEB-INF/lib/hibernate3.jar /xscjManager/WebContent/WEB-INF/lib/javassist-3.7.ga.jar /xscjManager/WebContent/WEB-INF/...

    ssh.zip_ssh.jar

    - freemarker-2.3.19.jar:FreeMarker是一个模板引擎,用于生成动态内容,如HTML页面。它与Struts2结合,可以生成动态视图。 - javassist-3.18.1-GA.jar:Javassist是一个Java字节码操作库,常用于动态修改类或...

Global site tag (gtag.js) - Google Analytics