当前位置:首页 > 开发 > 编程语言 > Spring > 正文

基于Annotation的Strust2,Spring,Hibernate的整合,利用Struts2 convention插件

发表于: 2011-01-20   作者:clongjava   来源:转载   浏览次数:
摘要: 在这里,我主要讲解怎么利用Struts2 convention插件的加入后怎么处理和spring的整合。 首先创建实体类,Customer @Entity public class Customer implements Serializable { private static final long serialVersionUID = 6961371108792430359L
在这里,我主要讲解怎么利用Struts2 convention插件的加入后怎么处理和spring的整合。
首先创建实体类,Customer
@Entity
public class Customer implements Serializable {
	private static final long serialVersionUID = 6961371108792430359L;
	
	private Integer id;
	private String name;
	private String address;
	private Date createDate;
	
	public Customer() {
		
	}

	public Customer(String name, String address, Date createDate) {
		this.name = name;
		this.address = address;
		this.createDate = createDate;
	}

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name="name",length=45,nullable=false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="create_date")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	

}

在配置sping sessionFactory bean的时候,sessionFactory的实现类应该为AnnotationSessionFactoryBean,然后再配置一下要spiring扫描的实体类的所在包
<property name="packagesToScan">
<list>
       <value>org.clong.domain</value>
</list>
</property>
第二步,完成对dao的设计:
package org.clong.dao;

import java.util.List;

import org.clong.domain.Customer;

public interface CustomerDao {
	
	public void save(Customer customer);
	
	public List<Customer> findAllCustomers();
	
}

dao实现类如下:
package org.clong.dao.impl;

import java.util.List;

import org.clong.dao.CustomerDao;
import org.clong.dao.SuperDaoSupport;
import org.clong.domain.Customer;
import org.springframework.stereotype.Repository;

@Repository("customerDao")
public class CustomerDaoImpl extends SuperDaoSupport implements CustomerDao {
	

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> findAllCustomers() {
		return getHibernateTemplate().find("from Customer");
	}

	@Override
	public void save(Customer customer) {
		getHibernateTemplate().save(customer);
	}

}

在这里我是利用了spring提供的HibernateDaoSupport这个来实现sessionFactory的注入,由于不想在每个dao中都设置sessionFactory的注入,所以写了一个父类用来被子类继承,在父类中注入sessionFactory。由于HibernateDaoSupport的setSessionFactory(SessionFactory sessionFactory)z方法是final类型的,所以子类不能覆盖,而且又不是使用配置文件,所以写了一个辅助类,
import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class SuperDaoSupport extends HibernateDaoSupport {
	private SessionFactory sessionFactory;

	@Resource(name="sessionFactory")
	public void setSuperSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
}

第三步,设计服务层
import java.util.List;

import org.clong.domain.Customer;

public interface CustomerBo {
	void addCustomer(Customer customer);
	List<Customer> listCustomer();

}

实现类为:
import java.util.List;

import javax.annotation.Resource;

import org.clong.bo.CustomerBo;
import org.clong.dao.CustomerDao;
import org.clong.domain.Customer;
import org.springframework.stereotype.Service;

@Service("customerBo")
public class CustomerBoImpl implements CustomerBo {
	private CustomerDao customerDao;
	
	@Resource
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	@Override
	public void addCustomer(Customer customer) {
		customerDao.save(customer);

	}

	@Override
	public List<Customer> listCustomer() {
		return customerDao.findAllCustomers();
	}

}

第四步,设计action
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.clong.bo.CustomerBo;
import org.clong.domain.Customer;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;


public class CustomerAction extends ActionSupport implements ModelDriven<Customer>,Preparable {
	private static final long serialVersionUID = -4411234960652825984L;

	private Customer customer;
	private List<Customer> customerList=new ArrayList<Customer>();
	private CustomerBo customerBo;
	
	public Customer getCustomer() {
		return customer;
	}


	public List<Customer> getCustomerList() {
		return customerList;
	}

	@Resource
	public void setCustomerBo(CustomerBo customerBo) {
		this.customerBo = customerBo;
	}


	@Override
	public Customer getModel() {
		if(customer==null){
			customer=new Customer();
		}
		return customer;
	}
	
	
	@Action(value="addCustomer",
			results={
				@Result(name="success",type="redirectAction",params={"actionName","listCustomer","namespace","/customer"}),
				@Result(name="input",location="customer.jsp")
				})
	public String addCustomer() throws Exception{
		//save it
		customer.setCreateDate(new Date());
		customerBo.addCustomer(customer);
 
		//reload the customer list
//		customerList = null;
		customerList = customerBo.listCustomer();
 
		return "success";
 
	}
 
	
	@Action(value="listCustomer",results={@Result(name="success",location="customer.jsp")})
	public String listCustomer() throws Exception{
 
		return "success";
 
	}
	
	@Override
	public void prepare() throws Exception {
		customerList = customerBo.listCustomer();
		
	}

}

在这里,利用struts2提供的验证框架,简单的进行一下服务端验证
我们对CustomerAction这个类中的addCustomer这个action进行验证(验证文件名需要满足一定的规则,具体请参见Struts2官方文档中的内容),我们这里的验证文件名为CustomerAction-addCustomer-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
  		"-//OpenSymphony Group//XWork Validator 1.0.3//EN"
  		"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
  		
<validators>
	<field name="name">
		<field-validator type="requiredstring">
			<message>name cann't be empty</message>
		</field-validator>
	</field>
</validators>  

注意这里action并不需要像没用convention插件那样加上类级别的@Controller注解
最后来个页面展示一下customer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Insert title here</title>
</head>
<body>
<h2>Add Customer</h2>
<s:form action="addCustomer" namespace="/customer">
  <s:textfield name="name" label="Name" value="" />
  <s:textarea name="address" label="Address" value="" cols="50" rows="5" />
  <s:submit />
</s:form>

<h2>All Customers</h2>

<s:if test="customerList.size() > 0">
<table border="1px" cellpadding="8px">
	<tr>
		<th>Customer Id</th>
		<th>Name</th>
		<th>Address</th>
		<th>Created Date</th>
	</tr>
	<s:iterator value="customerList" status="userStatus">
		<tr>
			<td><s:property value="id" /></td>
			<td><s:property value="name" /></td>
			<td><s:property value="address" /></td>
			<td><s:date name="createDate" format="yyyy-MM-dd" /></td>
		</tr>
	</s:iterator>
</table>
</s:if>
<br/>
<br/>
</body>
</html>


最后提供一下完整的源代码供下载(Struts2 2.2.1,Spring2.5.6,Hibernate3.6.0的jar包没有提供)

基于Annotation的Strust2,Spring,Hibernate的整合,利用Struts2 convention插件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
使用Struts2的convention插件,可以打造零配置的项目,这样做有一个好处,可以统一约定大于配置的开
进入我们基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发的最后一个阶段 了,这阶段主要
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3) 进入我们基于Annotation的Struts2.
 进入我们基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发的最后一个阶段了,这阶段主
晚上花了点时间写了个spring+hibernate的整合 下面是一点点心得 首先是jar包了。这里需要注意的是
Struts2,Spring,Hibernate整合例子一个(自己笔记) 开发本项目的具体步骤请看:Struts2,Spring,Hib
[程序开发]Struts2 Spring Hibernate 的简单整合 创建工程,选择java EE5 首先,加载Spring框架,如
整合之前一定要的具备的包(在这里我使用的是Annotations方式来整合) web.xml的配置 <?xml versio
继续前两篇文章[在struts2中使用JFreeChart ]和[在struts2中使用JasperReports ],这次是要从数据库
初学 Struts2,在整合 Spring 和 Hibernate 时遇到一些麻烦,下面以一个简单样例说明操作过程,以备
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号