我的博客:http://blog.csdn.net/jinlong0/
我 的 QQ: 277291919
我的Email:
jinlong0@sohu.com
工具:
Eclipse3.3、MyEclipse6.01、Tomcat6.0.10、Properties Editor插件、MSSQL2000
新建工程:名称为 login
一、新建数据库脚本:
/* SQLSERVER 2000 SCRIPTS */
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[users]
GO
drop table [dbo].[users]
GO
CREATE TABLE [dbo].[users] (
[id] [int] NOT NULL ,
[username] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[password] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
二、在工程中加入Struts1.2、Spring1.2、Hibernate3.1框架,这些框架如何去加网上讲的很多了,我就不详细讲解了,加了全部框架后web.xml的配置如下:
web.xml:
[id] [int] NOT NULL ,
[username] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[password] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
二、在工程中加入Struts1.2、Spring1.2、Hibernate3.1框架,这些框架如何去加网上讲的很多了,我就不详细讲解了,加了全部框架后web.xml的配置如下:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=" http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<web-app xmlns=" http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
加载的包的图例:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/jinlong0/ssh.gif)
三、建基本的显示页面
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"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<html:form action="/login">
<html:text property="userName">用户名</html:text><br />
<html:password property="password">密 码:</html:password><br />
<html:submit value="登录"/>
</html:form>
</body>
</html>
<%@ taglib uri=" http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri=" http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<html:form action="/login">
<html:text property="userName">用户名</html:text><br />
<html:password property="password">密 码:</html:password><br />
<html:submit value="登录"/>
</html:form>
</body>
</html>
success.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>success</title>
</head>
<body>
This is Success page. <br>
</body>
</html>
<html>
<head>
<title>success</title>
</head>
<body>
This is Success page. <br>
</body>
</html>
failure.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>failure</title>
</head>
<body>
This is Failure page. <br>
</body>
</html>
四、建立Hibernate与数据库表的映射。
<html>
<head>
<title>failure</title>
</head>
<body>
This is Failure page. <br>
</body>
</html>
四、建立Hibernate与数据库表的映射。
Users表的映射hibernate的xml为Users.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.login.dao.vo.Users" table="users" schema="dbo" catalog="pubs">
<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>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.login.dao.vo.Users" table="users" schema="dbo" catalog="pubs">
<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>
与此对应的Java类为:
Users.java
package com.login.dao.vo;
/**
* Users entity.
*
* @author MyEclipse Persistence Tools
*/
* Users entity.
*
* @author MyEclipse Persistence Tools
*/
@SuppressWarnings("serial")
public class Users implements java.io.Serializable {
public class Users implements java.io.Serializable {
// Fields
private Integer id;
private String username;
private String password;
private String username;
private String password;
// Constructors
/** default constructor */
public Users() {
}
public Users() {
}
/** full constructor */
public Users(String username, String password) {
this.username = username;
this.password = password;
}
public Users(String username, String password) {
this.username = username;
this.password = password;
}
// Property accessors
public Integer getId() {
return this.id;
}
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
this.id = id;
}
public String getUsername() {
return this.username;
}
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
this.username = username;
}
public String getPassword() {
return this.password;
}
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
this.password = password;
}
}
这样我们的基本的表映射就已经做完了。
此示例的源代码下载地址:
http://download.csdn.net/source/323772
Struts2.0+Spring2.0+HIbernate3.1整合用户登录以及增删改查完整示例:http://download.csdn.net/source/326113