第二步:實(shí)現(xiàn)controller。在Struts中繼承自Action。調(diào)用Model,實(shí)現(xiàn)數(shù)據(jù)的深層次檢驗(yàn)(email是否存在)和數(shù)據(jù)的插入,程序的跳轉(zhuǎn)等。代碼如下:
SignAction.java
1
/**//**
2
* @author han
3
* soochow university
4
* 實(shí)現(xiàn)Action,controller
5
*/
6
public class SignAction extends Action
{
7
8
public ActionForward execute(ActionMapping mapping, ActionForm form,
9
HttpServletRequest request,HttpServletResponse response
10
) throws Exception
{
11
12
/**//* 參數(shù)說(shuō)明:
13
* ActionMapping 實(shí)現(xiàn)跳轉(zhuǎn),控制頁(yè)面
14
* ActionForm 在viewer中實(shí)現(xiàn)的bean,繼承自ActionForm,獲得form中的數(shù)據(jù)
15
* HttpServletRequest request
16
* HttpServeletRsponse response
17
*/
18
19
ActionErrors errors = new ActionErrors(); //錯(cuò)誤處理,詳細(xì)信息見本blog的《Struts錯(cuò)誤處理》
20
21
/**//*
22
* 得到form中的數(shù)據(jù),因?yàn)閒orm中的數(shù)據(jù)在SignForm中,做一類型轉(zhuǎn)換即可;
23
* 這個(gè)要在struts-config.xml中進(jìn)行說(shuō)明
24
*/
25
SignForm sf = (SignForm) form;
26
27
/**//*
28
* 調(diào)用Model業(yè)務(wù)邏輯。
29
*/
30
SignModel sign = new SignModel();
31
32
sign.setEmail(sf.getEmail());
33
sign.setUserid(sf.getUserid());
34
35
/**//*
36
* 調(diào)用Model的findUserByEmail 和 findUserByUserid 判斷
37
* email和userid是否存在。
38
* 如果存在,則添加出錯(cuò)信息,頁(yè)面跳轉(zhuǎn)到mapping.getInputforward(),需要在
39
* struts-config.xml中定義。
40
* 關(guān)于錯(cuò)誤的詳細(xì)信息請(qǐng)參看本blog的《struts錯(cuò)誤處理》文章。
41
*/
42
//email existed
43
if (sign.findUserByEmail())
{
44
errors.add("email.existed",new ActionError("email.existed", sign.getEmail()));
45
this.saveErrors(request, errors);
46
return mapping.getInputForward();
47
}
48
49
//userid existed
50
if (sign.findUserByUserid())
{
51
errors.add("userid.existed", new ActionError("userid.existed",sf.getUserid()));
52
this.saveErrors(request, errors);
53
return mapping.getInputForward();
54
}
55
56
/**//*
57
* 調(diào)用Model的sendPassword將系統(tǒng)生成的密碼發(fā)送到用戶信箱。
58
* 如果發(fā)生錯(cuò)誤轉(zhuǎn)到mapping.getInputForward(),這個(gè)需要在struts-config.xml中定義
59
* 詳細(xì)信息見后面的struts-config.xml文件說(shuō)明
60
*/
61
if (sign.sendPassword())
{
62
sign.saveNewUser();
63
}else
{ //郵件發(fā)送錯(cuò)誤
64
errors.add("email.send.error", new ActionError("email.send.error", sf.getEmail()));
65
this.saveErrors(request, errors);
66
return mapping.getInputForward();
67
}
68
69
/**//*
70
* 如果注冊(cè)成功,頁(yè)面跳轉(zhuǎn)到mapping.findForward("home")
71
* 這個(gè)需要在struts-config.xml中定義
72
* 詳細(xì)信息見后面的struts-config.xml文件說(shuō)明
73
*/
74
return mapping.findForward("home");
75
76
77
}
78
} 說(shuō)明:
1、SignAction實(shí)現(xiàn)MVC中的controller,通過(guò)mapping參數(shù)來(lái)實(shí)現(xiàn)跳轉(zhuǎn),在controller中調(diào)用了Model中的一些操作,根據(jù)操作結(jié)果來(lái)實(shí)現(xiàn)跳轉(zhuǎn)。
2、程序中包括了Struts的錯(cuò)誤處理(請(qǐng)見本blog的《struts錯(cuò)誤處理》)、資源文件的使用,并且好多處代碼都和struts的配置文件struts-config.xml有聯(lián)系
3、關(guān)鍵代碼處都有詳細(xì)的注釋,若還有疑問(wèn),請(qǐng)您留言,我們共同探討。
第三步:實(shí)現(xiàn)Model,業(yè)務(wù)邏輯。用戶注冊(cè)程序比較簡(jiǎn)單,主要實(shí)現(xiàn)findUserByUserid()、findUserByEmail()、sendPassword()、saveNewUser()等功能。代碼如下:
SignForm.java
1
/**//**
2
* @author han
3
* soochow university
4
* 用戶注冊(cè)的Model
5
*/
6
public class SignModel
{
7
private String email;
8
private String userid;
9
private String MsgBox = "";
10
private String password;
11
private boolean sended;
12
13
14
/**//**
15
* @return Returns the email.
16
*/
17
public String getEmail()
{
18
return email;
19
}
20
/**//**
21
* @param email The email to set.
22
*/
23
public void setEmail(String email)
{
24
this.email = email;
25
}
26
/**//**
27
* @return Returns the userid.
28
*/
29
public String getUserid()
{
30
return userid;
31
}
32
/**//**
33
* @param userid The userid to set.
34
*/
35
public void setUserid(String userid)
{
36
this.userid = userid;
37
}
38
39
//用戶名和email是否存在
40
public boolean findUserByUserid() throws Exception
{
41
String selectsql = "select userid from password where userid = ?";
42
Mysql mysql = new Mysql(selectsql);
43
try
{
44
mysql.setString(1, this.userid);
45
ResultSet rs = mysql.executeQuery();
46
if(!rs.next()) return false;
47
} catch (Exception e)
{
48
throw new Exception("user.select" + e.getMessage());
49
} finally
{
50
mysql.close();
51
}
52
return true;
53
54
}
55
public boolean findUserByEmail() throws Exception
{
56
String selectsql = "select * from password where email = ?";
57
Mysql mysql = new Mysql(selectsql);
58
try
{
59
mysql.setString(1, this.email);
60
ResultSet rs = mysql.executeQuery();
61
if(!rs.next()) return false;
62
} catch (Exception e)
{
63
throw new Exception("user.select" + e.getMessage());
64
} finally
{
65
mysql.close();
66
}
67
return true;
68
}
69
70
public void saveNewUser() throws HibernateException
{
71
Session session = HibernateUtil.currentSession();
72
73
Transaction transaction;
74
try
{
75
transaction = session.beginTransaction();
76
SignForm newUser = new SignForm();
77
newUser.setEmail(this.email);
78
newUser.setUserid(this.userid);
79
80
newUser.setPassword(password);
81
session.save(newUser);
82
transaction.commit();
83
}catch(Exception e)
{
84
//throw new Exception(e);
85
e.printStackTrace();
86
}finally
{
87
HibernateUtil.closeSession();
88
}
89
}
90
91
//發(fā)送email給用戶初始密碼
92
public boolean sendPassword() throws Exception
{
93
94
SendMail sendMail = new SendMail();
95
96
sendMail.setFrom(Constants.FROM);
97
sendMail.setTo(this.email);
98
sendMail.setSubject(Constants.SUBJECT);
99
sendMail.setSmtpHost(Constants.smtpHost);
100
101
// 生成密碼
102
password = new RandStringGenerator(8).generatorString();
103
MsgBox = Constants.WELCOME_MESSAGE_HEAD + "\r用戶名:"+this.userid+"\r密碼:" + this.password + "\r" + Constants.WELCOME_MESSAGE_FOOT;
104
sendMail.setMsgText(MsgBox);
105
106
107
108
try
{
109
sendMail.sendnow();
110
sended = true;
111
}catch (Exception e)
{
112
System.err.println("send mail error:" + e.getMessage());
113
//throw new Exception("send mail error" + e.getMessage());
114
sended = false;
115
}
116
return sended;
117
}
118
} 說(shuō)明:
1、saveNewUser()使用了Hibernate作為持久化工具,關(guān)于Hibernate請(qǐng)參閱相關(guān)資料,也可以留言我們共同討論。
2、sendPassword()使用JavaMail發(fā)送Email,本文件通過(guò)SendMail工具類實(shí)現(xiàn)。
3、密碼生成由RandStringGenerator()工具類生成。
4、工具類可以點(diǎn)擊
這里下載。
第四步:配置struts-config.xml。
<!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>
<global-forwards> 全局轉(zhuǎn)發(fā)
<!-- 對(duì)應(yīng)SignAction.java 中的return mapping.findForward("home"); -->
<forward name="home" path="/index.jsp"/>
<forward name="userhome" path="/user/userhome.jsp"/>
</global-forwards>

<form-beans>
<!-- bean的聲明,這個(gè)在下面的Action設(shè)置中要用到 -->
<form-bean name="signForm" type="user.SignForm" />
</form-beans>
<!-- 設(shè)置Aciton -->
<action-mappings>
<action path="/sign" 對(duì)應(yīng)<form action>中action的值,在signin.jsp中的action=/login.do
name="signForm" 設(shè)置的formBean,名稱對(duì)應(yīng)<from-bean>中的聲明,在SignAction.java中對(duì)應(yīng)輸入?yún)?shù)ActionForm
type="user.SignAction" Action的類
validate="true" 是否驗(yàn)證
scope="request" 作用域 這些應(yīng)該不用解釋了
input="/user/signin.jsp"> 當(dāng)發(fā)生錯(cuò)誤時(shí)定向的頁(yè)面,對(duì)應(yīng)SignAction.java中的mapping.getInputForward()
</action>
</action-mappings>
<!-- ========== 資源文件定義,詳細(xì)信息在《Struts中資源文件使用》中說(shuō)明=========== -->

<message-resources parameter="Application"/>
</struts-config> 第五步:調(diào)試程序。經(jīng)過(guò)上面的說(shuō)明和代碼示例是不是對(duì)Struts中的MVC架構(gòu)有了比較清晰的了解,我們知道在java特別是j2ee的軟件中,需要設(shè)置很多的配置文件,剛開始的時(shí)候非常煩,特別是頻頻出錯(cuò)的時(shí)候,那種感覺(jué)學(xué)java的人應(yīng)該都嘗過(guò)哦!但是當(dāng)你徹底了解了配置文件的確切含義,并且能和代碼中的內(nèi)容進(jìn)行聯(lián)系時(shí),就會(huì)豁然開朗了,就不會(huì)覺(jué)得象走進(jìn)死胡同似的。
有了上面struts-config.xml中的說(shuō)明,相信你已經(jīng)和代碼聯(lián)系起來(lái)了,如果將這個(gè)程序調(diào)試成功,那么你就可以說(shuō)我已經(jīng)對(duì)struts設(shè)計(jì)MVC Web程序入門了,一旦跨進(jìn)了這個(gè)門檻,你會(huì)覺(jué)得學(xué)習(xí)起來(lái)是那么的輕松,一些來(lái)得是那么自然。
好了,希望以上三篇文章能帶你走進(jìn)Struts,理解Struts,特別是熟悉Struts的基本流程,當(dāng)然要想對(duì)一種模式由深入的了解,必須要多加實(shí)踐,從實(shí)踐中來(lái)體驗(yàn)到它的好處。
最后,希望你能徹底了解Struts,能為你所用。如果有什么意見和評(píng)論,請(qǐng)留言,我們共同討論,共同進(jìn)步。
謝謝你的閱讀!