项目结构:
框架整合所需jar
注:xwork-core-2.1.6选择2.1.6版本内含SpringUtil.class
1、CustomDao
package cn.ddj.dao;
import java.util.List;
import cn.ddj.entity.Custom;
public interface CustomDao {
/*
* 添加用户
*/
public void save(Custom user);
/*
* 删除用户
*/
public void delete(Custom user);
/*
* 修改用户
*/
public void update(Custom user);
/*
* 按Id获取用户
*/
public Custom findById(Integer id);
/*
* 按条件查询用户列表
*/
public List findByExample(Custom instance);
/*
* 按数学查询用户列表
*/
public List findUserByProperty(String propertyName,Object value);
/*
* 获取全部用户列表
*/
public List findAll();
}
2、CustomDaoImpl
package cn.ddj.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import cn.ddj.dao.CustomDao;
import cn.ddj.entity.Custom;
@Repository("customDao")
public class CustomDaoImpl extends HibernateDaoSupport implements CustomDao{
public CustomDaoImpl() {
// TODO Auto-generated constructor stub
}
//注入方法三 构造注入
@Autowired
public CustomDaoImpl(@Qualifier("sessionFactory")SessionFactory sessionFactory) {
// TODO Auto-generated constructor stub
this.setSessionFactory(sessionFactory);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#save(cn.houserent.entity.Custom)
*/
@Override
public void save(Custom user) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(user);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#delete(cn.houserent.entity.Users)
*/
@Override
public void delete(Custom user) {
// TODO Auto-generated method stub
this.getHibernateTemplate().delete(user);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#update(cn.houserent.entity.Custom)
*/
@Override
public void update(Custom user) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(user);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#findById(java.lang.Integer)
*/
@Override
public Custom findById(Integer id) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().get(Custom.class, id);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#findByExample(cn.houserent.entity.Custom)
*/
@Override
public List findByExample(Custom instance) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().findByExample(instance);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#findUserByProperty(java.lang.String, java.lang.Object)
*/
@Override
public List findUserByProperty(String propertyName, Object value) {
// TODO Auto-generated method stub
String queryString="from Custom as u where u."+propertyName+"=?";
return this.getHibernateTemplate().find(queryString,value);
}
/* (non-Javadoc)
* @see cn.houserent.dao.UserDao#findAll()
*/
@Override
public List findAll() {
// TODO Auto-generated method stub
return this.getHibernateTemplate().find("from Custom");
}
public Custom findBycid(Integer id) {
return this.getHibernateTemplate().load(Custom.class, id);
}
}
3、CustomBiz
package cn.ddj.service;
import java.util.List;
import cn.ddj.entity.Custom;
public interface CustomBiz {
public Custom getUserById(Integer id);
public List findAllUsers();
public void updateCustom(Custom custom);
public void deleteById(Integer id);
}
4、CustomeBizImpl
package cn.ddj.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import cn.ddj.dao.CustomDao;
import cn.ddj.entity.Custom;
import cn.ddj.service.CustomBiz;
@Service("customBiz")
public class CustomeBizImpl implements CustomBiz{
//注入方法一:属性上注解
@Autowired
@Qualifier("customDao")
private CustomDao customDao;
public CustomDao getCustomDao() {
return customDao;
}
public void setCustomDao(CustomDao customDao) {
this.customDao = customDao;
}
@Override
public Custom getUserById(Integer id) {
// TODO Auto-generated method stub
try {
Custom user=customDao.findById(id);
return user;
} catch (RuntimeException e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
@Override
public void updateCustom(Custom custom) {
// TODO Auto-generated method stub
try {
System.out.println("执行更新");
customDao.update(custom);
} catch (RuntimeException e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public List findAllUsers() {
try {
List users = new ArrayList();
users = customDao.findAll();
return users;
} catch (RuntimeException e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
@Override
public void deleteById(Integer id) {
// TODO Auto-generated method stub
try {
Custom user = customDao.findById(id);
customDao.delete(user);
} catch (RuntimeException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
5、Custom
package cn.ddj.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Custom entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "custom", catalog = "custom5805")
public class Custom implements java.io.Serializable {
// Fields
private static final long serialVersionUID = -2167909965383436954L;
private Integer customid;
private String cname;
private String caddress;
private String cemail;
private String cpost;
// Constructors
/** default constructor */
public Custom() {
}
/** minimal constructor */
public Custom(String cname, String caddress, String cemail) {
this.cname = cname;
this.caddress = caddress;
this.cemail = cemail;
}
/** full constructor */
public Custom(String cname, String caddress, String cemail, String cpost) {
this.cname = cname;
this.caddress = caddress;
this.cemail = cemail;
this.cpost = cpost;
}
// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "customid", unique = true, nullable = false)
public Integer getCustomid() {
return this.customid;
}
public void setCustomid(Integer customid) {
this.customid = customid;
}
@Column(name = "cname", nullable = false, length = 20)
public String getCname() {
return this.cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Column(name = "caddress", nullable = false, length = 20)
public String getCaddress() {
return this.caddress;
}
public void setCaddress(String caddress) {
this.caddress = caddress;
}
@Column(name = "cemail", nullable = false, length = 100)
public String getCemail() {
return this.cemail;
}
public void setCemail(String cemail) {
this.cemail = cemail;
}
@Column(name = "cpost", length = 13)
public String getCpost() {
return this.cpost;
}
public void setCpost(String cpost) {
this.cpost = cpost;
}
@Override
public String toString() {
return "Custom [customid=" + customid + ", cname=" + cname
+ ", caddress=" + caddress + ", cemail=" + cemail + ", cpost="
+ cpost + "]";
}
}
6、applicationContext.xml
classpath:database.properties
org.hibernate.dialect.MySQLDialect
true
true
none
7、database.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/house?useSSL=false
username=root
password=
8、hibernate.cfg.xml
9、web.xml
page/login.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
requestContextFilter
org.springframework.web.filter.RequestContextFilter
requestContextFilter
/*
org.springframework.web.context.request.RequestContextListener
OpenSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
OpenSessionInViewFilter
*.action
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
REQUEST
FORWARD
10、struts.xml
dataMap
11、CustomAction
package cn.ddj.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.ddj.entity.Custom;
import cn.ddj.service.CustomBiz;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Scope("prototype")
@Controller("customAction")
public class CustomAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private List customs;
private Map dataMap = new HashMap();
private Integer id;
private String cname;
private String caddress;
private String cemail;
private String cpost;
private Custom custom = new Custom();
@Autowired
@Qualifier("customBiz")
private CustomBiz customBiz;
public Custom getCustom() {
return custom;
}
public void setCustom(Custom custom) {
this.custom = custom;
}
public CustomBiz getCustomBiz() {
return customBiz;
}
public void setCustomBiz(CustomBiz customBiz) {
this.customBiz = customBiz;
}
public List getCustoms() {
return customs;
}
public void setCustoms(List customs) {
this.customs = customs;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCaddress() {
return caddress;
}
public void setCaddress(String caddress) {
this.caddress = caddress;
}
public String getCemail() {
return cemail;
}
public void setCemail(String cemail) {
this.cemail = cemail;
}
public String getCpost() {
return cpost;
}
public void setCpost(String cpost) {
this.cpost = cpost;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Map getDataMap() {
return dataMap;
}
public void setDataMap(Map dataMap) {
this.dataMap = dataMap;
}
public String jsonall() {
System.out.println("获取json格式");
customs = customBiz.findAllUsers();
// dataMap中的数据将会被Struts2转换成JSON字符串,所以这里要先清空其中的数据
dataMap.clear();
dataMap.put("customs", customs);
dataMap.put("success", true);
return SUCCESS;
}
public void jsonall2() throws IOException {
System.out.println("传统servlet获取json格式");
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
PrintWriter out;
out = response.getWriter();
customs = customBiz.findAllUsers();
JSONObject json=new JSONObject();
json.accumulate("success", true);
json.accumulate("custom", customs);
out.println(json.toString());
out.flush();
out.close();
}
}
12、showall.jsp(引入jq和struts标签库)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'showall.jsp' starting page
结果返回:
{"success":true,"custom":[{"caddress":"rrrr","cemail":"rrr","cname":"kkkk","cpost":"aaaa","customid":1},{"caddress":"sdfsdf","cemail":"[email protected]","cname":"sdfsdf","cpost":"fsdfsdf","customid":2}]}