Struts2使用范例 基于MyEclipce SunJDK 1.6+Hibernate

@Struts2 使用范例

MyEclipce+SunJDK 1.6+Hibernate+Struts2

在这里插入图片描述

//src/struts.xml

<?xml version="1.0" encoding="UTF-8" ?> /page/success.jsp /index.jsp

// /src/pers/zhy/action/AccountAction.java


package pers.zhy.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import pers.zhy.bean.Account;
import pers.zhy.dao.CheckDao;
import pers.zhy.dao.impl.CheckDaoImpl;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AccountAction extends ActionSupport //implements ModelDriven{
{
private Account a;
CheckDao cd = new CheckDaoImpl();
public Account getA() {
return a;
}
public void setA(Account a) {
this.a = a;
}

//先判断,再返回
public String execute() {
boolean res = false;
res =cd.Check(a);
if (res) {
HttpServletRequest r = ServletActionContext.getRequest();
HttpSession s = r.getSession();
s.setAttribute(“user”, a.getName());
return SUCCESS;
} else {
return LOGIN;
}
}

/* public Account getModel() {
return this.a;
}*/

}

//src/pers/zhy/bean/Account.java

package pers.zhy.bean;

public class Account {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


//src/pers/zhy/bean/acc.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>

//src/pers/zhy/dao/CheckDao.java


package pers.zhy.dao;

import pers.zhy.bean.Account;

public interface CheckDao {
boolean Check(Account a);
}


//src/pers/zhy/dao/impl/CheckDaoImpl.java


package pers.zhy.dao.impl;
import java.util.List;
import javax.swing.border.EmptyBorder;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import pers.zhy.bean.Account;
import pers.zhy.dao.CheckDao;

public class CheckDaoImpl implements CheckDao {

public boolean Check(Account c) {
	Boolean flag=false;
	Configuration cf=new Configuration();
	SessionFactory sf=null;
	Session s=null;

	try {
		Transaction transaction=null;
		sf=cf.configure().buildSessionFactory();
		s=sf.openSession();
		transaction=s.beginTransaction();
		
		sf=cf.buildSessionFactory();
		
		String hql="from Account a where name=? and password=?";
		Query query = s.createQuery(hql);
		query.setParameter(0, c.getName());
		query.setParameter(1,c.getPassword());
		List list = query.list();
		
		if(!list.isEmpty())
			flag=true;

        transaction.commit();
	} catch (HibernateException e) {
		/*if(transaction!=null){
            transaction.rollback();
    }//throw new RuntimeException(e.getMessage());

*/
e.printStackTrace();

	}finally{
		s.close();
	}
	return flag;
}

}


hibernate.hbm.xml


//<?xml version='1.0' encoding='UTF-8'?>

org.hibernate.dialect.MySQLDialect jdbc:mysql://localhost:3306 XXXXXXXXXXXX XXXXXXXXXXXX com.mysql.jdbc.Driver true

//WebRoot/page/success.jsp
<%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

***

jsp


<title>My JSP 'info.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
${a.name},您好!<%=session.getAttribute("user") %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值