spring+hibernate+struts
这是我用spring+hibernate+struts开发的项目的第一个功能action,其中这chkName方法是检测注册的用户在数据库中是否有重名.
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

package news.action;

import news.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.form.UserForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
* MyEclipse Struts
* Creation date: 06-09-2006
*
* XDoclet definition:
* @struts.action path="/user" name="userForm" input="/form/errors.jsp" parameter="method" scope="request" validate="true"
*/

public class UserAction extends DispatchAction {

// --------------------------------------------------------- Instance Variables

// --------------------------------------------------------- Methods
  private UserDAO userDAO;
static{
ApplicationContextFactory.init(
"/news/applicationContext.xml");

   }
public ActionForward chkName(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)throws Exception {

   UserForm userform = (UserForm) form;        
   String name = userform.getName();
  

   userDAO = (UserDAO) ApplicationContextFactory.getApplicationContext().getBean(
"userDAO");

   boolean go=userDAO.chkName(name);
     if(go)
     {

     return (mapping.findForward(
"chkNameFailed"));

     }
         return (mapping.findForward(
"chkNameOk"));
     


  }

}

下边是封装好数据库的DAO文件的实现类UserDAOImpl.java
package news;

import java.util.List;
//import org.apache.commons.logging.Log;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;


public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{

public List getUsers(){
return getHibernateTemplate().findByNamedQuery(
"AllUsers");
}

public User findUserByName(String name) {
return (User) getHibernateTemplate().find(
"from User u where u.name=?",name).get(0);
}

    public void addUser(User user){
                getHibernateTemplate().save(user);
    }
    
  
    public User getUserById(Integer id) {
        return (User) getHibernateTemplate().load(User.class, id);
    }

    public User chkLogin(String name,String password){
     return (User) getHibernateTemplate().find(
"from User u where u.name='"+name+"' and u.password='"+password+"'").get(0);
    }
  
    public User getUser(String username) throws DAOException {
        User user = null;

        
// TODO: Fix this hack
        long userId = 0;
        try {
            userId = Long.parseLong(username);
        } catch (NumberFormatException npe) {
            
// ignore - not an id, really is a username
        }
        
        if (userId != 0) {
            return (User) getHibernateTemplate().get(User.class, new Long(userId));
        }
        
        List users =
            getHibernateTemplate().find(
"from User u where u.username=?",
                                        username);

        if ((users != null) && (users.size() > 0)) {
            user = (User) users.get(0);
        }

        if (user == null) {
          
// log.warn("uh oh, user not found...");
            throw new DAOException(
"User '" + username +
                                  
"' not found in database!");
        }

        return user;
    }
    
    public void removeUser (String name) {
User user = findUserByName(name);
this.getHibernateTemplate().delete(user);
}
    
    public void updateUser(User user) {
    
        getHibernateTemplate().update(user);
    
    }
    
    private static String hql =
"from User u where u.name=? and password=?";

public boolean isValidUser(String username, String password) {

String[] userlist=new String[2];
userlist[0]=username;
userlist[1]=password;
// List userList = this.getHibernateTemplate().find(hql, (Object)username);
List userList = this.getHibernateTemplate().find(hql,userlist);

if (userList.size() > 0) {

return true;

}

return false;

}

private static String hql2=
"from User u where u.name=?";

public boolean chkName(String name){
List userList2=this.getHibernateTemplate().find(hql2,name);

if(userList2.size()>0){

return true;
}

    return false;
}

}
调用的jsp文件:
<%@ page language=
"java"%>
<%@ taglib uri=
"/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri=
"/WEB-INF/struts-html.tld" prefix="html" %>

<html>
<head>
<title>JSP for userForm form</title>
</head>
<body>
<html:form action=
"/user.do?method=chkName">

<bean:message key=
"user.name"/>
<html:text property=
"name"/><html:errors property="name"/><br/>

<html:submit/><html:cancel/>
</html:form>
</body>
</html>

lunzi   2006-06-10 01:33:33 评论:9   阅读:2444   引用:0
3135 interesting things! @2008-08-17 08:16:20  Stinailijen
big dick gay video

de el gay gratis haciendo sexo video
7863 interesting things! @2008-08-14 06:56:17  Oneptanda
generic host processes for win32 has encountered a problem

problem solution writing lesson plan

uva problem set

to problems

addition math problem

lung problems cause depression

problems in mathematics
8860 interesting things! @2008-08-13 04:53:30  NoireeLix
campus parking problem

acceleration practice problem

intel active monitor problems
2468 interesting things! @2008-08-10 07:24:45  catosynchyday
child in kidney problem

fuel pressure regulator problem

2005 expedition ford problem

hysterectomy post problem
4399 interesting things! @2008-08-10 00:38:41  insidonsitymn
problems with 2006 hondas

what health problem do stress cause

problems overpopulation

cenral heating problems

air conditioner problem room

health colon problems an its symtoms

problem dogs

word problems grade 1

problems of industrialization in the 1800s

network browsing problem

straight gay videos

halcott greene county business
9378 interesting things! @2008-08-05 19:26:43  latoffelm
half life 2 joystick problem

ipod shuffle problems with windows

problems with animal cloning

patellar tracking problems

united state environmental problem

paypal security problems

antivirus problems

resistance problems

multiple traveling salesman problem
773 interesting things! @2008-08-05 08:02:34  ToraRarbods
dial up internet problems

problems with back

problem with loading

xp windows update problem

problems in international business
6351 interesting things! @2008-08-04 20:46:57  spaphercemalp
drag and drop problems

realplayer has encountered a problem and needs to close

yoga knee problems

problem neighbor

immoral problem study

puppy ear problems

help problem

varicose veins laser problems

avg version 7 problems

feline neutering problems
7602 please do not read this t @2008-07-09 14:10:46  Cyncsnugs
have a nice day!

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog