@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") %>