<!
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
;
}
}
|