在这里,我主要讲解怎么利用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包没有提供)
- 大小: 43.1 KB
分享到:
相关推荐
Struts2 Spring Hibernate 框架整合 Annotation Maven project
Struts2 Spring Hibernate 框架整合 Annotation Maven project.zip
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5图文教程整合开发.doc
struts2 spring2.5 hibernate3.0 annotation 整合
SSH2 annotation 实现struts2.1.6 spring2.5.6 hibernate3.3 全注解开发 hibernate延迟加载_懒加载 具体应用
jquery easyUI+struts2+spring+hibernate基于annotation实现的CRM
马士兵视频里的struts2 hibernate3 spring2.5 annotation 整合 绝对好用 里面还有jar包 部署就可以用
本Demo基于annotation的Struts2.1.8+Spring2.5.6+Hibernate3.3.2的整合
存贮过程进行操作,目前还有待完善,可以不用hibernate-generic-dao - Generic DAO, 用Spring3.2.0和Hibernate4替换,就是一个非常好的Struts2+Spring3+Hibernate4基于annotation的零配置的开发框架 借鉴网友的经验...
annotation hibernate struts spring springsecurity API中英文均有 从各大网站下载足足花了我50积分
用struts2和spring3和hibernate实现的一个登录的示例
整合S2SH+Freemarker+oscache,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
struts2+spring2+hibernate3 Annotation整合例子
整合S2SH+Freemarker,后台用Spring管理各个bean,Hibernate做数据库持久化,viewer用Freemarker。整合中对Struts2,Hibernate,Spring都采用Annotation进行注解类。
Struts1.3 + Hibernate3.3 + Spring3.0 Annotation整合
struts2.18+hibernate3.3.2+spring2.56、annotation注解 全部打包 可以用
此项目是struts2 + hibernate3(annotation) + spring(annotation)整合后的项目雏形,基于MVC的设计思想,很适合初学ssh框架的童鞋参考
这是一个Struts2+hibernate实现登陆的案例,hibernate使用了annotation进行配置