环境:eclipse3.2+myeclipse5.0m2,在tomcat上顺利运行,为了简便开发,这一个例子我没有写业务逻辑层,而且包名的设置也不太合理,各位可以自行修改.构造好开发环境,把需要的jar导入web project中. 1、创建一个名叫demo数据库,这里用mysql 再创建一个表: create table user ( id int(15) not null auto_increment, username varchar(50) not null, password varchar(50) not null, primary key(id) )type=INNODB; 记得type要设为innodb或bdb类型的,不然的话不支持数据库事务.
2、domain层和DAO层
.
使用MyEclipse的数据Database Explorer工具创建User.hmb.xml、AbstractUser.java、User.java映射文件。具体文件如下: AbstractUser.java文件: package com.test;
/**
* AbstractUser generated by MyEclipse - Hibernate Tools */
public abstract class AbstractUser implements java.io.Serializable {
// Fields
private Integer id;
private String username; private String password; // Constructors
/** default constructor */
public AbstractUser() { } /** full constructor */ public AbstractUser(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } // Property accessors
public Integer getId() {
return this.id; } public void setId(Integer id) { this.id = id; }
public String getUsername() {
return this.username; } public void setUsername(String username) { this.username = username; }
public String getPassword() {
return this.password; } public void setPassword(String password) { this.password = password; } } User.java文件 package com.test; // Generated by MyEclipse - Hibernate Tools
/**
* User generated by MyEclipse - Hibernate Tools */ public class User extends AbstractUser implements java.io.Serializable {
// Constructors
/** default constructor */
public User() { } /** full constructor */ public User(Integer id, String username, String password) { super(id, username, password); } } User.hbm.xml文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd提示:User.java,和User.hbm.xm两个文件放在同一个包文件夹里面。 接下来是创建UserDAO接口,和UserDAO接口的实现UserDAOImp. UserDAO文件: package com.test; "> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="com.test.User" table="user"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="50" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="50" not-null="true" /> </property> </class> </hibernate-mapping>
publicinterface UserDAO {
publicabstractboolean isValidUser(String username,String password);
}
UserDAOImp文件,主要是用来验证用户名和密码正确不:
package com.test;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDAOImp extends HibernateDaoSupport implements UserDAO{
private static String hql = "from User as u where u.username=:userName"+" and u.password=:passWord";
public boolean isValidUser(String username, String password) {
Query query= this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
query.setString("userName", username);
query.setString("passWord", password);
return (query.list().size()>0);
}
}
我把配置文件放在最后才说,即是spring的的配置文件,这里利用spring来管理hibernate的dataSource和事务管理.
3、创建strutsMVC的框架的一些具体实现内容,把struts中的action控制放在spring中去配置,因为可以利用spring中DI或IoC的功能.
新建一个index.jsp文件,如下所示:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="login.jsp">进入登录系统</a>
</body>
</html>
再创建一个login.jsp文件
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>login.jsp</title>
</head>
<body>
<html:form action="/login">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html:html>
登录成功后的success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>success.jsp</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">
-->
</head>
<body>
Welcome!<br/>
<logic:present name="loginForm">
欢迎你,<bean:write name="loginForm" property="username"/>
</logic:present>
</body>
</html:html>
|