剛剛看了一下,感受講的很清晰,這個對Java web有一點了解而後又想學習web
strut2的同窗 我想是很適合的了。apache
糾結,不知道本學期糾結的J2EE項目,該不應用 Struts2 ,session
無論了,學習一下以備不時之需。。app
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<!-- struts2
的
action
必須放在一個指定的包空間下定義
-->
<
package
name
=
"default"
extends
=
"struts-default"
>
<!--
定義處理請求
URL
爲
login.action
的
Action -->
<
action
name
=
"login"
class
=
"org.qiujy.web.struts.action.LoginAction"
>
<!--
定義處理結果字符串和資源之間的映射關係
-->
<
result
name
=
"success"
>
/success.jsp
</
result
>
<
result
name
=
"error"
>
/error.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
|
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<!-- struts2
的
action
必須放在一個指定的包空間下定義
-->
<
package
name
=
"qiujy"
extends
=
"struts-default"
>
<!--
定義處理請求
URL
爲
login.action
的
Action -->
<
action
name
=
"login"
class
=
"org.qiujy.web.struts2.action.LoginAction"
>
<!--
定義處理結果字符串和資源之間的映射關係
-->
<
result
name
=
"success"
>
/success.jsp
</
result
>
<
result
name
=
"error"
>
/error.jsp
</
result
>
</
action
>
</
package
>
<
package
name
=
"my"
extends
=
"struts-default"
namespace
=
"/manage"
>
<!--
定義處理請求
URL
爲
login.action
的
Action -->
<
action
name
=
"backLogin"
class
=
"org.qiujy.web.struts2.action.LoginAction"
>
<!--
定義處理結果字符串和資源之間的映射關係
-->
<
result
name
=
"success"
>
/success.jsp
</
result
>
<
result
name
=
"error"
>
/error.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
|
<
struts
>
<
include
file
=
"struts-default.xml"
/>
<
include
file
=
"struts-user.xml"
/>
<
include
file
=
"struts-book.xml"
/>
<
include
file
=
"struts-shoppingCart.xml"
/>
......
</
struts
>
|
<
struts
>
......
<
constant
name
=
"struts.custom.i18n.resources"
value
=
"messages"
/>
</
struts
>
|
package
org.qiujy.web.struts2.action;
import
com.opensymphony.xwork2.ActionSupport;
/**
*
@author
qiujy
*
@version
1.0
*/
public
class
LoginAction
extends
ActionSupport
{
private
String
userName
;
private
String
password
;
private
String
msg
;
//
結果信息屬性
/**
*
@return
the
msg
*/
public
String getMsg() {
return
msg
;
}
/**
*
@param
msg
the
msg
to
set
*/
public
void
setMsg(String msg) {
this
.
msg
= msg;
}
/**
*
@return
the
userName
*/
public
String getUserName() {
return
userName
;
}
/**
*
@param
userName
the
userName
to
set
*/
public
void
setUserName(String userName) {
this
.
userName
= userName;
}
/**
*
@return
the
password
*/
public
String getPassword() {
return
password
;
}
/**
*
@param
password
the
password
to
set
*/
public
void
setPassword(String password) {
this
.
password
= password;
}
/**
*
處理用戶請求的
excute()
方法
*
@return
結果導航字符串
*
@throws
Exception
*/
public
String execute()
throws
Exception{
if
(
"test"
.equals(
this
.
userName
) &&
"test"
.equals(
this
.
password
)){
msg
=
"
登陸成功,歡迎
"
+
this
.
userName
;
return
this
.
SUCCESS
;
}
else
{
msg
=
"
登陸失敗,用戶名或密碼錯
"
;
return
this
.
ERROR
;
}
}
}
|
public
String execute()
throws
Exception{
if
(
"test"
.equals(
this
.
userName
) &&
"test"
.equals(
this
.
password
)){
msg
=
"
登陸成功,歡迎
"
+
this
.
userName
;
//
獲取
ActionContext
實例,經過它來訪問
Servlet API
ActionContext context = ActionContext.getContext();
//
看
session
中是否已經存放了用戶名,若是存放了:說明已經登陸了;
//
不然說明是第一次登陸成功
if
(
null
!= context.getSession().get(
"uName"
)){
msg
=
this
.
userName
+
"
:你已經登陸過了
!!!"
;
}
else
{
context.getSession().put(
"uName"
,
this
.
userName
);
}
return
this
.
SUCCESS
;
}
else
{
msg
=
"
登陸失敗,用戶名或密碼錯
"
;
return
this
.
ERROR
;
}
}
|
<form method="post"
action="userOpt!login.action">
|
package
org.qiujy.web.struts2.action;
import
com.opensymphony.xwork2.ActionContext;
import
com.opensymphony.xwork2.ActionSupport;
/**
*
@author
qiujy
*
@version
1.0
*/
public
class
LoginAction
extends
ActionSupport{
private
String
userName
;
private
String
password
;
private
String
msg
;
//
結果信息屬性
/**
*
@return
the
msg
*/
public
String getMsg() {
return
msg
;
}
/**
*
@param
msg
the
msg
to
set
*/
public
void
setMsg(String msg) {
this
.
msg
= msg;
}
/**
|