Chinaunix首页 | 论坛 | 博客
  • 博客访问: 276366
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 202
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-25 12:05
文章分类

全部博文(76)

文章存档

2016年(7)

2015年(2)

2014年(67)

我的朋友

分类: Java

2014-02-11 11:35:03

1.首先我们要添加Hibernate的环境和struts的环境。(这里不演示了演示)

2.hibernated3.3的架包:


3.struts1.3的架包:


4.web.xml的配置文件:


  Struts1Hibernate3
 
    index.jsp
 
  
 
 
    action
    org.apache.struts.action.ActionServlet
   
      config
      /struts-config.xml
   
   
      debug
      3
   
   
      detail
      3
   
    0
 
 
    action
    *.do
 


5.hibernate的配置文件:


          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "">


   
        org.hibernate.dialect.MySQLDialect
        jdbc:mysql://192.168.1.107:3306/oneself
        root
        123456
        com.mysql.jdbc.Driver
        oneself
   


6.映射实体类:(User)
package com.syj.bean;
/**
 * User entity. @author MyEclipse Persistence Tools
 */
public class User implements java.io.Serializable {

private Integer id;
private String name;
private String pwd;

public User() {
}
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}

// Property accessors
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getPwd() {
return this.pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

}

7.现在我们来写DAO:
BaseDao:
package com.syj.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class BaseDao {
private static SessionFactory sessionFactory = null;
protected Session session = null;
static{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public void getSession(){
session= sessionFactory.openSession();
}
public void closeAll(){
session.close();
session = null;
}
}

UserDao:
package com.syj.dao;
import org.hibernate.Transaction;
import com.syj.bean.User;
public class UserDao extends BaseDao {
/**
* 添加
* @param 
* @return
*/
public int save(User vu){
getSession();
Transaction tx=session.beginTransaction();
int result=1;
try {
session.save(vu);
result=1;
tx.commit();
} catch (Exception e) {
// TODO: handle exception
tx.rollback();
result=-1;
}finally{
closeAll();
}
return result;
}
}

8.service层:
UserService:
package com.syj.service;
import com.syj.bean.User;
import com.syj.dao.UserDao;
public class UserService {
private UserDao userDao=new UserDao();

public boolean isReg(User us){
int result=userDao.save(us);
if (result>0) {
return true;
}
return false;
}
}

9.action层:UserAction:
package com.syj.action;
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.actions.DispatchAction;
import com.syj.bean.User;
import com.syj.service.UserService;

public class UserAction extends DispatchAction{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserService userService=new UserService();
User us=new User();
us.setName(request.getParameter("name"));
us.setPwd(request.getParameter("pwd"));
boolean flag=userService.isReg(us);
if (flag) {
request.getSession().setAttribute("name", us.getName());
return mapping.findForward("success");
}
return mapping.findForward("fail");
}
}

10.接下来我们可以配置struts1的配置文件了。
struts-config.xml:











11.jsp页面:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


 
    注册
 
 
 

   注册帐号:

   注册密码:

   
   

 


12.进入测试:输入。它会进入index.jsp因为是默认的。然后我们输入参数。

13.整个项目的结构:

好了介绍完毕。

阅读(1361) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~