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后,也没找到太好的方法,可能没找对。

 

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

 

 

你可能感兴趣的:(freemarker,springMVC,SpringJDBC)