今天参考师兄的一个项目来做了一下SSH整合的实验,不过师兄用的是注解的方式,由于本人水平有限,还是用配置文件来的,然后一不留神就出现了下面的问题:
先说明一下实验项目的分层:实体Po、接口Dao、接口实现Impl、业务Service、表单封装Dto、Action;如下图:
本来之前实验的时候都是在Action层直接使用Impl的,不过参考了师兄的项目增加了一个Service层,直接在Service层里面使用Dao,额,这个应该对老手来说是理所当然的;不过在本人实验的过程中遇到了一个很纠结的问题:
Debug的时候在Service里面调用到Dao方法时就会跳到了下面这个方法这里:
- public InvocationTargetException(Throwable target) {
- super((Throwable)null); // Disallow initCause
- this.target = target;
- }
在网上查了好久,大致了解是配置文件的问题,然后就以配置文件为切入点寻找问题;不过查看了好久,貌似配置文件是没有什么问题的
一开始我是这样配置的:
<bean id="SecHandDaoImpl" class="org.sip.dao.impl.SecHandDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="SecHandService" class="org.sip.service.SecHandService">
</bean>
<bean id="SecHandAction" scope="prototype" class="org.sip.action.SecHandAction">
<property name="secHandService" ref="SecHandService" />
</bean>
问题就出现在了红色标识的地方,因为Service里面只有Dao接口,所以在上面红色这里就不知道怎么注入了,所以尝试了好多方式都没有问题,后来找书看了一下里面的例子讲解,终于解决问题:
<bean id="SecHandService" class="org.sip.service.SecHandService">
<property name="secHandDao" ref="SecHandDaoImpl"></property>
</bean>
这里的secHandDao就是Service里面的使用的接口,SecHandDaoImpl 为接口的实现(这个要先装配好),然后问题就解决了
想了一下,还是基础不太扎实,以后得加强一下先,也应该使用注解来开发了,配置文件的话感觉太臃肿了;
下面贴一下完整的代码:(现在一个练习项目,这里的主要功能是将一些信息添加到数据库:这里使用MySQL)
(1)Po和映射文件(这个个人比较懒都是用Hibernate逆向工程来生成的):
package org.sip.po;
/**
* SecHand entity. @author MyEclipse Persistence Tools
*/
public class SecHand implements java.io.Serializable {
// Fields
private Integer sechandId;
private User user;
private String sechandName;
private String sechandContent;
private String sechandNewsold;
private String sechandPriceRegion;
private String sechandPrice;
private String sechandPicture;
private String sechandDate;
private String sechandType;
private String sechandAuPhone;
private String sechandCollege;
private String sechandCampus;
private String sechandPersonIdentity;
private String sechandOthers;
// Constructors
/** default constructor */
public SecHand() {
}
/** minimal constructor */
public SecHand(User user, String sechandName, String sechandContent, String sechandNewsold, String sechandPriceRegion, String sechandPrice, String sechandPicture, String sechandDate, String sechandType, String sechandAuPhone, String sechandCollege, String sechandCampus, String sechandPersonIdentity) {
this.user = user;
this.sechandName = sechandName;
this.sechandContent = sechandContent;
this.sechandNewsold = sechandNewsold;
this.sechandPriceRegion = sechandPriceRegion;
this.sechandPrice = sechandPrice;
this.sechandPicture = sechandPicture;
this.sechandDate = sechandDate;
this.sechandType = sechandType;
this.sechandAuPhone = sechandAuPhone;
this.sechandCollege = sechandCollege;
this.sechandCampus = sechandCampus;
this.sechandPersonIdentity = sechandPersonIdentity;
}
/** full constructor */
public SecHand(User user, String sechandName, String sechandContent, String sechandNewsold, String sechandPriceRegion, String sechandPrice, String sechandPicture, String sechandDate, String sechandType, String sechandAuPhone, String sechandCollege, String sechandCampus, String sechandPersonIdentity, String sechandOthers) {
this.user = user;
this.sechandName = sechandName;
this.sechandContent = sechandContent;
this.sechandNewsold = sechandNewsold;
this.sechandPriceRegion = sechandPriceRegion;
this.sechandPrice = sechandPrice;
this.sechandPicture = sechandPicture;
this.sechandDate = sechandDate;
this.sechandType = sechandType;
this.sechandAuPhone = sechandAuPhone;
this.sechandCollege = sechandCollege;
this.sechandCampus = sechandCampus;
this.sechandPersonIdentity = sechandPersonIdentity;
this.sechandOthers = sechandOthers;
}
// Property accessors
public Integer getSechandId() {
return this.sechandId;
}
public void setSechandId(Integer sechandId) {
this.sechandId = sechandId;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public String getSechandName() {
return this.sechandName;
}
public void setSechandName(String sechandName) {
this.sechandName = sechandName;
}
public String getSechandContent() {
return this.sechandContent;
}
public void setSechandContent(String sechandContent) {
this.sechandContent = sechandContent;
}
public String getSechandNewsold() {
return this.sechandNewsold;
}
public void setSechandNewsold(String sechandNewsold) {
this.sechandNewsold = sechandNewsold;
}
public String getSechandPriceRegion() {
return this.sechandPriceRegion;
}
public void setSechandPriceRegion(String sechandPriceRegion) {
this.sechandPriceRegion = sechandPriceRegion;
}
public String getSechandPrice() {
return this.sechandPrice;
}
public void setSechandPrice(String sechandPrice) {
this.sechandPrice = sechandPrice;
}
public String getSechandPicture() {
return this.sechandPicture;
}
public void setSechandPicture(String sechandPicture) {
this.sechandPicture = sechandPicture;
}
public String getSechandDate() {
return this.sechandDate;
}
public void setSechandDate(String sechandDate) {
this.sechandDate = sechandDate;
}
public String getSechandType() {
return this.sechandType;
}
public void setSechandType(String sechandType) {
this.sechandType = sechandType;
}
public String getSechandAuPhone() {
return this.sechandAuPhone;
}
public void setSechandAuPhone(String sechandAuPhone) {
this.sechandAuPhone = sechandAuPhone;
}
public String getSechandCollege() {
return this.sechandCollege;
}
public void setSechandCollege(String sechandCollege) {
this.sechandCollege = sechandCollege;
}
public String getSechandCampus() {
return this.sechandCampus;
}
public void setSechandCampus(String sechandCampus) {
this.sechandCampus = sechandCampus;
}
public String getSechandPersonIdentity() {
return this.sechandPersonIdentity;
}
public void setSechandPersonIdentity(String sechandPersonIdentity) {
this.sechandPersonIdentity = sechandPersonIdentity;
}
public String getSechandOthers() {
return this.sechandOthers;
}
public void setSechandOthers(String sechandOthers) {
this.sechandOthers = sechandOthers;
}
}
映射文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
<class name="org.sip.po.SecHand" table="t_sechand" catalog="gxnusip">
<id name="sechandId" type="java.lang.Integer">
<column name="sechand_id" />
<generator class="identity" />
</id>
<many-to-one name="user" class="org.sip.po.User" fetch="select">
<column name="sechand_writter_id" not-null="true" />
</many-to-one>
<property name="sechandName" type="java.lang.String">
<column name="sechand_name" not-null="true" />
</property>
<property name="sechandContent" type="java.lang.String">
<column name="sechand_content" not-null="true" />
</property>
<property name="sechandNewsold" type="java.lang.String">
<column name="sechand_newsold" not-null="true" />
</property>
<property name="sechandPriceRegion" type="java.lang.String">
<column name="sechand_price_region" not-null="true" />
</property>
<property name="sechandPrice" type="java.lang.String">
<column name="sechand_price" not-null="true" />
</property>
<property name="sechandPicture" type="java.lang.String">
<column name="sechand_picture" not-null="false" />
</property>
<property name="sechandDate" type="java.lang.String">
<column name="sechand_date" not-null="true" />
</property>
<property name="sechandType" type="java.lang.String">
<column name="sechand_type" not-null="true" />
</property>
<property name="sechandAuPhone" type="java.lang.String">
<column name="sechand_au_phone" not-null="true" />
</property>
<property name="sechandCollege" type="java.lang.String">
<column name="sechand_college" not-null="true" />
</property>
<property name="sechandCampus" type="java.lang.String">
<column name="sechand_campus" not-null="true" />
</property>
<property name="sechandPersonIdentity" type="java.lang.String">
<column name="sechand_person_identity" not-null="true" />
</property>
<property name="sechandOthers" type="java.lang.String">
<column name="sechand_others" />
</property>
</class>
</hibernate-mapping>
(2)Dao
package org.sip.dao;
import org.sip.po.SecHand;public interface SecHandDao {
public boolean addSecHand(SecHand secHand);
}
(3)DaoImpl
package org.sip.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.sip.dao.SecHandDao;
import org.sip.po.SecHand;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
public class SecHandDaoImpl extends HibernateDaoSupport implements SecHandDao {
@Override
public boolean addSecHand(SecHand secHand) {
// TODO Auto-generated method stub
try{
getHibernateTemplate().save(secHand);
System.out.println("二手信息添加成功");
return true;
}
catch(RuntimeException re){
System.out.println("二手信息添加失败"+re.toString());
return false;
}
}
@SuppressWarnings("rawtypes")
@Override
public List findSecHand(final String queryString, final int pageNumber, final int pageSize) {
// TODO Auto-generated method stub
try{
return (List) getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session s) throws HibernateException, SQLException{
Query query=s.createQuery(queryString);
query.setFirstResult((pageNumber-1)*pageSize);
query.setMaxResults(pageSize);
List list= query.list();
return list;
}
});
}catch (RuntimeException re){throw re;}
}
@SuppressWarnings("rawtypes")
@Override
public List findAllSecHand(int pageNumber, int pageSize) {
// TODO Auto-generated method stub
String queryString="from SecHand";
return findSecHand(queryString, pageNumber,pageSize);
}
}
(4)Service
package org.sip.service;
import java.util.List;
import org.sip.dao.SecHandDao;
import org.sip.po.SecHand;
public class SecHandService {
private SecHandDao secHandDao;
public SecHandDao getSecHandDao() {
return secHandDao;
}
public void setSecHandDao(SecHandDao secHandDao) {
this.secHandDao = secHandDao;
}
public void addSecHand(SecHand secHand){
System.out.print("Service:"+secHand.getSechandName());
//boolean bool=secHandDao.addSecHand(secHand);
secHandDao.addSecHand(secHand);
//System.out.print("SUCCESS");
}
public List findAllSecHand(int pageNumber, int pageSize){
return secHandDao.findAllSecHand(pageNumber, pageSize);
}
}
(5)DTO
package org.sip.dto;
import org.sip.po.SecHand;
public class SecHandAuthorization {
private String sechandName;
private String sechandContent;
private String sechandNewsold;
private String sechandPriceRegion;
private String sechandPrice;
//private String sechandPicture;
private String sechandType;
private String sechandAuPhone;
private String sechandCollege;
private String sechandCampus;
private String sechandPersonIdentity;
private String sechandOthers;
//private File sechandPicture;
//第几页
private int page;
public String getSechandName() {
return sechandName;
}
public void setSechandName(String sechandName) {
this.sechandName = sechandName;
}
public String getSechandContent() {
return sechandContent;
}
public void setSechandContent(String sechandContent) {
this.sechandContent = sechandContent;
}
public String getSechandNewsold() {
return sechandNewsold;
}
public void setSechandNewsold(String sechandNewsold) {
this.sechandNewsold = sechandNewsold;
}
public String getSechandPriceRegion() {
return sechandPriceRegion;
}
public void setSechandPriceRegion(String sechandPriceRegion) {
this.sechandPriceRegion = sechandPriceRegion;
}
public String getSechandPrice() {
return sechandPrice;
}
public void setSechandPrice(String sechandPrice) {
this.sechandPrice = sechandPrice;
}
public String getSechandType() {
return sechandType;
}
public void setSechandType(String sechandType) {
this.sechandType = sechandType;
}
public String getSechandAuPhone() {
return sechandAuPhone;
}
public void setSechandAuPhone(String sechandAuPhone) {
this.sechandAuPhone = sechandAuPhone;
}
public String getSechandCollege() {
return sechandCollege;
}
public void setSechandCollege(String sechandCollege) {
this.sechandCollege = sechandCollege;
}
public String getSechandCampus() {
return sechandCampus;
}
public void setSechandCampus(String sechandCampus) {
this.sechandCampus = sechandCampus;
}
public String getSechandPersonIdentity() {
return sechandPersonIdentity;
}
public void setSechandPersonIdentity(String sechandPersonIdentity) {
this.sechandPersonIdentity = sechandPersonIdentity;
}
public String getSechandOthers() {
return sechandOthers;
}
public void setSechandOthers(String sechandOthers) {
this.sechandOthers = sechandOthers;
}
public void setPage(int page) {
this.page = page;
}
public SecHand getSecHand(){
SecHand secHand=new SecHand();
secHand.setSechandName(sechandName);
secHand.setSechandContent(sechandContent);
secHand.setSechandPersonIdentity(sechandPersonIdentity);
secHand.setSechandType(sechandType);
secHand.setSechandNewsold(sechandNewsold);
secHand.setSechandPriceRegion(sechandPriceRegion);
secHand.setSechandPrice(sechandPrice);
//secHand.setSechandPicture(sechandPicture);
secHand.setSechandCollege(sechandCollege);
secHand.setSechandCampus(sechandCampus);
secHand.setSechandAuPhone(sechandAuPhone);
if(sechandOthers!=null){
secHand.setSechandOthers(sechandOthers);
}
return secHand;
}
public int getPage(){
return page;
}
}
(5)Action
package org.sip.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.sip.dto.SecHandAuthorization;
import org.sip.po.SecHand;
import org.sip.po.User;
import org.sip.service.SecHandService;
import com.opensymphony.xwork2.ModelDriven;
public class SecHandAction implements SessionAware,
ModelDriven<SecHandAuthorization> {
private SecHandAuthorization secHandAuthorization = new SecHandAuthorization();
private SecHandService secHandService;
private Map<String, Object> session;
private String sechand_result = "";
private File picture;
private String pictureContentType;// 上传文件的类型
private String pictureFileName; // 上传文件名
private String savePath; // 文件保存路径
public SecHandAuthorization getSecHandAuthorization() {
return secHandAuthorization;
}
public void setSecHandAuthorization(
SecHandAuthorization secHandAuthorization) {
this.secHandAuthorization = secHandAuthorization;
}
public void setSecHandService(SecHandService secHandService) {
this.secHandService = secHandService;
}
@Override
public SecHandAuthorization getModel() {
// TODO Auto-generated method stub
return secHandAuthorization;
}
// addSecHand
public String addSecHand() throws Exception {
String picture_name = null;
if (picture.length() != 0) {
picture_name= getSavePath() + "\\" + getPictureFileName();
FileOutputStream fileOutputStream = new FileOutputStream(
picture_name);
FileInputStream fileInputputStream = new FileInputStream(
getPicture());
int len = 0;
byte[] b = new byte[1024];
while ((len = fileInputputStream.read(b)) > 0) {
fileOutputStream.write(b, 0, len);
}
}
String curdate = "";
Date dt = new Date(); // 获取当前系统时间,作为发布时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式
curdate = sdf.format(dt);
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
int userId = (Integer) session.getAttribute("userId");
// User
User user = new User();
user.setUserId(userId);
SecHand secHand = new SecHand();
secHand = secHandAuthorization.getSecHand();
// 图片保存路径
secHand.setSechandPicture(picture_name);
// 发布者
secHand.setUser(user);
// 发布时间
secHand.setSechandDate(curdate);
secHandService.addSecHand(secHand);
sechand_result = "/findAllSecHandAction.action";
return "sechand_add";
}
// findSecHand
public String findAllSecHand() {
System.out.println("页数" + secHandAuthorization.getPage());
List seclist = secHandService.findAllSecHand(
secHandAuthorization.getPage(), 15);
// 放入session
session.put("seclist", seclist);
// sechand_result="sechand_findall";
return "sechand_findall";
}
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.session = arg0;
}
public File getPicture() {
return picture;
}
public void setPicture(File picture) {
this.picture = picture;
}
public String getPictureContentType() {
return pictureContentType;
}
public void setPictureContentType(String pictureContentType) {
this.pictureContentType = pictureContentType;
}
public String getPictureFileName() {
return pictureFileName;
}
public void setPictureFileName(String pictureFileName) {
this.pictureFileName = pictureFileName;
}
public String getSavePath() throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getSechand_result() {
return sechand_result;
}
public void setSechand_result(String sechand_result) {
this.sechand_result = sechand_result;
}
}
(7)JSP页面就不贴出来啦,欢迎大家批评指正