java 类
BaseDao Class
package com.power.base;import java.util.List;import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.mapping.Collection;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class BaseDao extends HibernateDaoSupport implements IBaseDao{
/**
 * @param str
 * @return
 */
public void save(Object obj){
getHibernateTemplate().save(obj);
}
public void saveOrUpdate(Object obj){
getHibernateTemplate().saveOrUpdate(obj);
}

public void create(Object entity) {
this.create(entity);

}
public void delete(Object entity) throws Exception {
// TODO Auto-generated method stub

}
public void deleteAll(Class clazz) throws Exception {
// TODO Auto-generated method stub

}
public void deleteAll(Collection entities) throws Exception {
// TODO Auto-generated method stub

}
public List find(String queryString, Object param) throws Exception {
// TODO Auto-generated method stub
return null;
}
public List find(String queryString, Object[] params) throws Exception {
// TODO Auto-generated method stub
return null;
}
public Criteria getCriteria(Class clazz) throws Exception {
// TODO Auto-generated method stub
return null;
}
public Query getQuery(String sql) throws Exception {
// TODO Auto-generated method stub
return null;
}
public int getTotalCount(String hql) throws Exception {
// TODO Auto-generated method stub
return 0;
}
public int getTotalPage(int totalCount, int pageSize) {
// TODO Auto-generated method stub
return 0;
}
public Object loadByKey(Class clazz, String keyName, Object keyValue) {
// TODO Auto-generated method stub
return null;
}
public Session openSession() {
// TODO Auto-generated method stub
return null;
}
public void update(Object entity) {
// TODO Auto-generated method stub

}
public List find(String queryString) throws Exception {
return this.getHibernateTemplate().find(queryString);
}}BaseAction
package com.power.base;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;public class BaseAction extends ActionSupport{


 private ApplicationContext context;
  
 protected Object getBean(String name) {
 context = getWebApplicationContext();
     return context.getBean(name);
 }

protected WebApplicationContext initWebApplicationContext(ActionServlet servlet) throws IllegalStateException {
return super.initWebApplicationContext(servlet);
} public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
return super.execute(mapping, actionForm, request, response);
}}
IUserDao接口
package com.power.user.dao;import com.power.base.IBaseDao;public interface IUserDao extends IBaseDao{
public boolean isUser(String username, String password);}
UserDao
import java.util.ArrayList;
import java.util.List;import com.power.base.BaseDao;
import com.power.user.dao.IUserDao;public class UserDaoImpl extends BaseDao implements IUserDao{
public boolean isUser(String username, String password){
boolean b = false;
List userlist = new ArrayList();
String sql = "from Usertable u where u.name = '"+username+"' and u.password = '"+password+"'";
try {
userlist = this.find(sql);
} catch (Exception e) {
e.printStackTrace();
}
        if (userlist.size()>0){
         b = true;
        }
        return b;
}
}
IUserService
package com.power.user.service;public interface IUserService {
public boolean isUser(String username, String password);}
UserServiceImpl
package com.power.user.service.spring;import com.power.user.dao.IUserDao;
import com.power.user.service.IUserService;public class UserServiceImpl implements IUserService{
private IUserDao userDao;
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao UserDao) {
userDao = UserDao;
}
public boolean isUser(String username, String password){
return this.userDao.isUser(username, password);
}
}
LoginForm
package com.power.user.struts;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;import com.power.base.BaseForm;public class LoginForm extends BaseForm{
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.username = "";
this.password = "";
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        return errors;
}}
LoginAction
package com.power.user.struts;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import com.power.base.BaseAction;
import com.power.user.service.IUserService;public class LoginAction extends BaseAction{
    //private IUserService userService = (IUserService)getBean("userService");
    
public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse reponse){
ActionForward forward = null;
IUserService userService = (IUserService)getBean("userService");
try{
LoginForm form = (LoginForm)actionForm;
String username = form.getUsername();
String password = form.getPassword();
boolean b = userService.isUser(username,password);
if(b){
forward = mapping.findForward("success");
}else{
forward = mapping.findForward("failed");
}
}catch(Exception e){
e.printStackTrace();
forward = mapping.findForward("false");
}
return forward;
}

解决方案 »

  1.   

    login.jsp
    <%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="struts-html.tld" prefix="html" %><%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <html:base/>
        <title>login.jsp</title>
        <SCRIPT language="javascript" src=""></SCRIPT>
      </head>
      
      <body>
       <html:form action="/Login.do" method="post">
          <table border="0" height="70%">
        <tr height="43%">
              <td width="30%"></td>
              <td width="20%"></td>
              <td width="20%"></td>
              <td width="30%"></td>
            </tr>
            <tr height="5%">
              <td width="30%"></td>
              <td width="20%" align="right">Login:</td>
              <td width="20%" align="left"><html:text property="username" /></td>
              <td width="30%"></td>
            </tr>
            <tr height="5%">
              <td width="30%"></td>
              <td width="20%" align="right">Password:</td>
              <td width="20%" align="left"><html:password property="password" /></td>
              <td width="30%"></td>
            </tr>
            <tr height="4%">
              <td width="30%"></td>
              <td width="20%" align="right" width="100%"><html:submit/></td>
              <td width="20%" align="right" width="100%"><html:reset/></td>
              <td width="30%"></td>
            </tr>
            <tr height="43%">
              <td width="30%"></td>
              <td width="20%"></td>
              <td width="20%"></td>
              <td width="30%"></td>
            </tr>
          </table>
        </html:form>
      </body>
    </html>
    如果大家还需要什么我可以贴给大家。
    最后问题是
    BaseAction中
    ApplicationContext context =getWebApplicationContext();
    LoginAction中
    private IUserService userService = (IUserService)getBean("userService");
    任何一个设置为全局变量就找不到userService.
    现在跟踪时,可以得到userDao中的b,在userService向loginAction返回isUser时报错
    错误是Source not found for VirtualNativeMethodInvoker.invoke(Object, Object[]) line: not available
    请大家帮忙一下。
      

  2.   

    多谢兄弟支持,没有办法,我觉得错误应该在Action获得service时出错,现在是能得到service,但是userService向loginAction返回isUser(username,password)时,发生错误。boolean b = userService.isUser(username,password);userService是这样获得的IUserService userService = (IUserService)getBean("userService");请大家看一下BaseAction
    package com.power.base;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionServlet;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.struts.ActionSupport;public class BaseAction extends ActionSupport{


     private ApplicationContext context;
      
     protected Object getBean(String name) {
     context = getWebApplicationContext();
         return context.getBean(name);
     }

    protected WebApplicationContext initWebApplicationContext(ActionServlet servlet) throws IllegalStateException {
    return super.initWebApplicationContext(servlet);
    } public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
    return super.execute(mapping, actionForm, request, response);
    }}
    我怀疑是这里的错误,由于大多是配置文件,我跟不进去,所以不知道什么错误。
    请指教。
      

  3.   

    private IUserService userService = (IUserService)getBean("userService");改为:private IUserService userService = (IUserService)context.getBean("userService");
      

  4.   

    首先谢谢帮忙
    我的LoginActin继承的BaseAction
    BaseAction中
    private ApplicationContext context;
      
     protected Object getBean(String name) {
     context = getWebApplicationContext();
         return context.getBean(name);
     }
    所以LoginAction中
    private IUserService userService = (IUserService)getBean("userService");
    无须再定义context了。
      

  5.   

    String sql = "from Usertable u where u.name = '"+username+"' and u.password = '"+password+"'";
    try {
    userlist = this.find(sql);
    } catch (Exception e) {
    e.printStackTrace();
    }应该是这里抛出异常了吧
      

  6.   

    勇敢的心很谢谢你,也许我们喜欢同一个电影。
    我跟踪过,不是Dao里面的错误。
    执行到userServiceImpl里面的public boolean isUser(String username, String password){
    return this.userDao.isUser(username, password);
    }
    并且已经返回b后才出错。lingbing5719(凤凰涅磐) 真的很抱歉,虽然这些都是我自己随便写的,但是在公司,不能发给你,如果你需要什么我可以帖给你,并且告诉你怎么配置。
      

  7.   

    那有点遗憾,我坐的和你的结构不一样 我刚开始学的步态东 感觉你的就够比较合适一些,能说一下  你是用什么软件做的吗?
    我使用的是myeclipse
    但是在create hibernate mapping 选项中不知道怎么配置,我用的是myeclipse4.1的网上有一些资料的和我的界面是不一样的 好像是4.0的  很郁闷 
    但是我可以实现功能  
    就是觉得自己 的结构不太对劲
    请你多指点
      

  8.   

    学框架不是为了用她的技术而是学习他框架的好处. 3层结构最妙的地方在于根据不同的层次写出很规范的代码!对以后的维护有很大的好处. 你写的出问题的地方就展示出你运用框架的不正确性 1)第一你不应该在DAO层写业务逻辑,这样出问题你就找不到问题到底是出在那里. if (userlist.size()>0){
             b = true;
            }
    这些代码你应该写到Service层去.
    2)我觉得你出错的地方应该是在Hibernate的配置文件里,你没有贴出来.你可以仔细看看
      

  9.   

    http://www.blogjava.net/oksonic/archive/2005/11/06/18370.aspx
    我是按照上面的那个例子作的  请高手看看他这个结构有什么不合理的
    给一点指导 小弟在这先礼过去了 嘿嘿
      

  10.   

    请问struts+hibernate+spring问题棱是没找到hibernate的身影。而且你的spring 用得相当不充分。
      

  11.   

    我用的也是myeclipese,我用的是sql server数据库,当然数据库没有关系,你配置一下,映射文件会自动生成的,明天详细介绍给你.
    我没有用什么hibernate配置文件,我只是用的生成的映射文件,两个类文件AbstractUser和User类文件和user.hbm.xml三个文件,这三个文件是自动生成的,不会有问题的,因为查询结果都出来了,userlist.size()的结果是0,可以返回结果的.
    public boolean isUser(String username, String password){
                      System.out.println("========"+this.userDao.isUser(username, password);+"======");
    return this.userDao.isUser(username, password);
    }
    在console里面都能够打出来,========false=======
    你们看可能是什么问题?
      

  12.   

    由于是在家,刚才那个我没有用什么hibernate配置文件,我只是用的生成的映射文件,两个类文件AbstractUser和User类文件和user.hbm.xml三个文件写错了,是用的生成的映射文件,两个类文件AbstractUsertable和Usertable类文件和usertable.hbm.xml三个文件,user是关键字,可不能建这样的表,lingbing5719(凤凰涅磐) 如果你经常在线,我可以帮你配置环境,会省很多力,以前从来没碰过这种问题,由于spring版本不一样了,所以现在遭遇这种问题.
      

  13.   

    我按照http://www.blogjava.net/oksonic/archive/2005/11/06/18370.aspx建立的也好用,只使不知道UserDAO.javapublic interface UserDAO {   public abstract boolean isValidUser(String username, String password);} UserDAOImp.javaimport java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.test.Hibernate.SessionFactory;public class UserDAOImp extends HibernateDaoSupport implements UserDAO {    private SessionFactory sessionFactory;    private static String hql = "from User u where u.username=? ";    public boolean isValidUser(String username, String password) {       List userList = this.getHibernateTemplate().find(hql, username);       if (userList.size() > 0) {           return true;       }       return false;    }}这种方式的写法是否合理,如果方便你能不能给说一下DAO的作用和原理,不怕你笑话 这个我没整明白,嘿嘿,麻烦你啦
    还有我的关于DAO的配置到写在"/WEB-INF/applicationContext.xml"里你可以看看那个网页的代码。我就是按照他做的,贴代码太麻烦,偷个懒,麻烦你看一下啦
      

  14.   

    你applicationContext.xml配置不对吧,好像没配置action啊