Struts+DAO框架搭建完成!(源码)
今天做了這個(gè)Struts+DAO框架,可以說是又向SSH邁了一步。
?
做的過程中又發(fā)現(xiàn)了一些問題,但是在我和其他人的努力下一起解決了。這個(gè)問題是MyEclipse的Tomcat的啟動問題。(已經(jīng)解決并發(fā)帖解答了)
好了,下面切入正題:
Struts+DAO框架實(shí)現(xiàn)用戶登錄。(連接MYSQL數(shù)據(jù)庫)
環(huán)境:MyEclipse
步驟:
1.新建WebProject,命名為LoginProject,點(diǎn)擊確定
2.給項(xiàng)目加入Struts支持,Add Struts Capabilities....,選Struts1.2,Base package for new classes:命名為cn.mldn.lxh.struts,完成
3.src下新建一個(gè)類,名為PersonVO,放在org.lxh.vo,完成,寫入代碼如下:
// 只包含setter和getter方法的類
package org.lxh.vo ;
import java.util.* ;
public class PersonVo
{
?// 表中所有字段
?private String id ;
?private String name ;
?private String password ;
?// 此屬性用于保存全部錯(cuò)誤信息
?private List errors ;
?public boolean invalidate()
?{
??boolean flag = true ;
??// 驗(yàn)證ID
??if(this.id==null||"".equals(this.id))
??{
???flag = false ;
???errors.add("ID不能為空!") ;
??}
??else
??{
???// 進(jìn)行長度驗(yàn)證:3~10位
???if(this.id.length()<3||this.id.length()>10)
???{
????flag = false ;
????errors.add("ID的長度應(yīng)為3~10位!") ;
???}
??}
??// 驗(yàn)證密碼
??if(this.password==null||"".equals(this.password))
??{
???flag = false ;
???errors.add("密碼不能為空!") ;
??}
??else
??{
???// 進(jìn)行長度驗(yàn)證:3~10位
???if(this.password.length()<3||this.password.length()>10)
???{
????flag = false ;
????errors.add("密碼的長度應(yīng)為3~10位!") ;
???}
??}
??return flag ;
?}
?public void setErrors(List errors)
?{
??this.errors = errors ;
?}
?public List getErrors()
?{
??return this.errors ;
?}
?// 生成getter和setter方法
?public void setId(String id)
?{
??this.id = id ;
?}
?public void setName(String name)
?{
??this.name = name ;
?}
?public void setPassword(String password)
?{
??this.password = password ;
?}
?public String getId()
?{
??return this.id ;
?}
?public String getName()
?{
??return this.name ;
?}
?public String getPassword()
?{
??return this.password ;
?}
};
4.src下新建一個(gè)類,名為DAOFactory,包名為org.lxh.factory,完成,寫入代碼:
// 取得DAO實(shí)例的工廠類
package org.lxh.factory ;
import org.lxh.dao.* ;
import org.lxh.daoimpl.* ;
public class DAOFactory
{
?public static PersonDAO getPersonDAOInstance()
?{
??return new PersonDAOImpl() ;
?}
};
5.src下新建類,名為DateBaseConnection,包名org.lxh.dbc,完成,寫入代碼:
// 本類只用于數(shù)據(jù)庫連接及關(guān)閉操作
package org.lxh.dbc ;
import java.sql.* ;
public class DataBaseConnection
{
?// 屬性
?// 定義數(shù)據(jù)庫操作的常量、對象
?// 數(shù)據(jù)庫驅(qū)動程序
?private final String DBDRIVER?= "com.mysql.jdbc.Driver" ;
?// 數(shù)據(jù)庫連接地址
?private final String DBURL??= "jdbc:mysql://localhost:3306/people" ;
?// 數(shù)據(jù)庫用戶名
?private final String DBUSER??= "root" ;
?// 數(shù)據(jù)庫連接密碼
?private final String DBPASSWORD?= "hicc" ;
?// 聲明一個(gè)數(shù)據(jù)庫連接對象
?private Connection conn???= null ;
?// 在構(gòu)造方法之中連接數(shù)據(jù)庫
?public DataBaseConnection()
?{
??try
??{
???// 加載驅(qū)動程序
???Class.forName(DBDRIVER) ;
???// 連接數(shù)據(jù)庫
???conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;?
??}
??catch (Exception e)
??{
???System.out.println(e) ;
??}
?}
?// 返回一個(gè)數(shù)據(jù)庫連接
?public Connection getConnection()
?{
??/// 返回連接對象
??return this.conn ;
?}
?// 關(guān)閉數(shù)據(jù)庫連接
?public void close()
?{
??try
??{
???this.conn.close() ;???
??}
??catch (Exception e)
??{
??}
?}
};
6.src下新建類,名為PersonDAOImpl,包名為org.lxh.daoimpl,完成。寫入代碼如下:
// 具體實(shí)現(xiàn)DAO接口的類
package org.lxh.daoimpl ;
// 需要連接數(shù)據(jù)庫
// 需要對VO的內(nèi)容進(jìn)行具體的驗(yàn)證
import java.sql.* ;
import org.lxh.dao.* ;
import org.lxh.dbc.* ;
import org.lxh.vo.* ;
public class PersonDAOImpl implements PersonDAO
{
?public boolean isLogin(PersonVo pv)
?{
??boolean flag = false ;
??// 在此處成具體的數(shù)據(jù)庫驗(yàn)證
??// 聲明一個(gè)數(shù)據(jù)庫操作對象
??PreparedStatement pstmt?= null ;
??// 聲明一個(gè)結(jié)果集對象
??ResultSet rs???= null ;
??// 聲明一個(gè)SQL變量,用于保存SQL語句
??String sql????= null ;
??// DataBaseConnection為具體的數(shù)據(jù)庫連接及關(guān)閉操作類
??DataBaseConnection dbc?= null ;
??// 連接數(shù)據(jù)庫
??dbc = new DataBaseConnection() ;
??// 編寫SQL語句
??sql = "SELECT name FROM person WHERE id=? and password= ?" ;
??try
??{???
???// 實(shí)例化數(shù)據(jù)庫操作對象
???pstmt = dbc.getConnection().prepareStatement(sql) ;
???// 設(shè)置pstmt的內(nèi)容,是按ID和密碼驗(yàn)證
???pstmt.setString(1,pv.getId()) ;
???pstmt.setString(2,pv.getPassword()) ;
???// 查詢記錄
???rs = pstmt.executeQuery() ;
???// 判斷是否有記錄
???if(rs.next())
???{
????// 如果有記錄,則執(zhí)行此段代碼
????// 用戶是合法的,可以登陸
????flag = true ;
????pv.setName(rs.getString(1)) ;
???}
???// 依次關(guān)閉
???rs.close() ;
???pstmt.close() ;
??}
??catch(Exception e)
??{
???System.out.println(e) ;
??}
??finally
??{
???// 最后一定要保證數(shù)據(jù)庫已被關(guān)閉
???dbc.close() ;
??}
??return flag ;
?}
7.src下新建PersonDAO,包名org.lxh.dao
// 本接口定義本項(xiàng)目中所操作person表的全部方法
package org.lxh.dao ;
// 使用PersonVo類
import org.lxh.vo.* ;
public interface PersonDAO
{
?// 需要一個(gè)登陸驗(yàn)證的方法
?public boolean isLogin(PersonVo pv) ;
}
8.WebRoot下新建文件,命名為數(shù)據(jù)庫創(chuàng)建腳本.sql,完成寫入內(nèi)容,如下:
DROP TABLE person ;
CREATE TABLE person
(
?id varchar(20) not null primary key ,
?name varchar(20) not null ,
?password varchar(20)
) ;
INSERT INTO person (id,name,password) VALUES ('lxh','LiXingHua','ffffff') ;
INSERT INTO person (id,name,password) VALUES ('mldn','mole','mldn') ;
-- 提交事務(wù)
commit ;
9.WebRoot下新建兩個(gè)JSP頁面,分別命名為,login和login_success。完成,寫入代碼:
login.jsp
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.util.*"%>
<%@ 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" %>
<html>
<head>
?<title>登陸</title>
</head>
<body>
<center>
?<h1>登陸范例――Struts實(shí)現(xiàn)</h1>
?<hr>
?<br>
?<br>
?<!-- 加入更加詳細(xì)的錯(cuò)誤提示 -->
?<html:errors/>
?
?<html:form action="login.do" method="post">
?<table>
??<tr>
???<td colspan="2">用戶登陸</td>
??</tr>
??<tr>
???<td>用戶名:</td>
???<td><html:text property="id"/></td>
??</tr>
??<tr>
???<td>密 碼:</td>
???<td><html:password property="password"/></td>
??</tr>
??<tr>
???<td colspan="2">
???<html:submit value="登陸"/>
???<html:reset value="重置"/>
???</td>
??</tr>
?</table>
?</html:form>
?</center>
</body>
</html>
login_success.jsp
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
?<title>登陸</title>
</head>
<body>
<center>
?<h1>登陸范例――Struts實(shí)現(xiàn)</h1>
?<hr>
?<br>
?<br>
?<h2>登陸成功</h2>
?<h3>歡迎<font color="red" size="15">
??${person.name}
?</font>光臨!!!</h3>
</center>
</body>
</html>
10.插入ActionForm和Form的方法。在src下建立,在菜單空白處點(diǎn)右鍵,選自定義透視圖,添加方法,add name和password。
11.修改Struts配置文件,最終代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
? <data-sources />
? <form-beans >
??? <form-bean name="loginForm" type="cn.mldn.lxh.struts.form.LoginForm" />
? </form-beans>
? <global-exceptions />
? <global-forwards />
? <action-mappings >
??? <action
????? attribute="loginForm"
????? input="/login.jsp"
????? name="loginForm"
????? path="/login"
????? scope="request"
????? type="cn.mldn.lxh.struts.action.LoginAction">
????? <forward name="suc" path="/login_success.jsp"></forward>
?? </action>
? </action-mappings>
? <message-resources parameter="cn.mldn.lxh.struts.ApplicationResources" />
</struts-config>
12.在資源文件ApplicationResources.properties中寫入代碼:實(shí)現(xiàn)錯(cuò)誤提示。
?# Resources for parameter 'cn.mldn.lxh.struts.ApplicationResources'
# Project P/LoginProject
id.null = <li>\u7528\u6237ID\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
password.null = <li>\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff01\uff01\uff01
error.login = <li>\u9519\u8bef\u7684\u7528\u6237\u540d\u6216\u5bc6\u7801\uff01\uff01\uff01
13.編輯LoginForm.java,代碼如下:
/**
?* MyEclipse Struts
?* Creation date: 12-14-2006
?*
?* XDoclet definition:
?* @struts.form name="loginForm"
?*/
public class LoginForm extends ActionForm {
?/*
? * Generated fields
? */
?/** password property */
?private String password;
?/** name property */
?private String id;
?/*
? * Generated Methods
? */
?/**
? * Method validate
? * @param mapping
? * @param request
? * @return ActionErrors
? */
?public ActionErrors validate(ActionMapping mapping,
???HttpServletRequest request) {
??// TODO Auto-generated method stub
??ActionErrors errors = new ActionErrors() ;
??if(this.id==null||"".equals(this.id))
??{
???errors.add("name",new ActionMessage("id.null")) ;
??}
??if(this.password==null||"".equals(this.password))
??{
???errors.add("password",new ActionMessage("password.null")) ;
??}
??return errors;
?}
?/**
? * Method reset
? * @param mapping
? * @param request
? */
?public void reset(ActionMapping mapping, HttpServletRequest request) {
??// TODO Auto-generated method stub
?}
?/**
? * Returns the password.
? * @return String
? */
?public String getPassword() {
??return password;
?}
?/**
? * Set the password.
? * @param password The password to set
? */
?public void setPassword(String password) {
??this.password = password;
?}
?public String getId() {
??return id;
?}
?public void setId(String id) {
??this.id = id;
?}
}
14.編輯LoginAction.java,代碼如下:
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package cn.mldn.lxh.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.lxh.factory.DAOFactory;
import org.lxh.vo.PersonVo;
import cn.mldn.lxh.struts.form.LoginForm;
/**
?* MyEclipse Struts
?* Creation date: 12-14-2006
?*
?* XDoclet definition:
?* @struts.action path="/login" name="loginForm" input="/form/login.jsp" scope="request" validate="true"
?*/
public class LoginAction extends Action {
?/*
? * Generated Methods
? */
?/**
? * Method execute
? * @param mapping
? * @param form
? * @param request
? * @param response
? * @return ActionForward
? */
?public ActionForward execute(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response) {
??LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
??String id = loginForm.getId() ;
??String password = loginForm.getPassword() ;
??PersonVo per = new PersonVo();
??per.setId(id) ;
??per.setPassword(password) ;
??System.out.println(DAOFactory.getPersonDAOInstance().isLogin(per));
??if(DAOFactory.getPersonDAOInstance().isLogin(per))
??{
???// 用戶ID和密碼合法
???request.setAttribute("person",per) ;
??}
??else
??{
???// 錯(cuò)誤,要回到login.jsp頁面
???ActionMessages errors = new ActionMessages() ;
???errors.add("error",new ActionMessage("error.login")) ;
???super.saveErrors(request, errors) ;
???return mapping.getInputForward() ;
??}
??return mapping.findForward("suc");
?}
}
15.啟動MyEclipse中的Tomcat。運(yùn)行項(xiàng)目。完成
施楊出品!!!
總結(jié)
以上是生活随笔為你收集整理的Struts+DAO框架搭建完成!(源码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到虫子咬手是什么意思
- 下一篇: 梦到自己穿白鞋好不好