<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirect-action">Logon!input</result>
</global-results>
|
名字 | 说明 |
chain | 用来处理Action链 |
dispatcher | 用来转向页面,通常处理JSP,这是默认的结果类型 |
freeMarker | 处理FreeMarker模板 |
httpHeader | 用来控制特殊的Http行为 |
redirect | 重定向到一个URL |
redirect-action | 重定向到一个Action |
stream | 向浏览器发送InputSream对象,通常用来处理文件下载 |
velocity | 处理Velocity模板 |
xslt | 处理XML/XLST模板 |
plaintext | 显示原始文件内容,例如文件源代码 |
tiles | 结合Tile使用 |
private String nextAction;
public String getNextAction() {
return nextAction;
}
|
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-action">${nextAction}</result>
</action>
|
package
org.qiujy.domain;
public
class
User {
private
String
userName
;
private
String
password
;
/**
*
@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;
}
}
|
package
org.qiujy.web.struts2.action;
import
org.qiujy.domain.User;
import
com.opensymphony.xwork2.ActionContext;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UserAction
extends
ActionSupport{
//
定义用于封装请求参数的模型对象
private
User
user
=
new
User();
private
String
msg
;
//
结果信息属性
/**
*
@return
the
user
*/
public
User getUser() {
return
user
;
}
/**
*
@param
user
the
user
to
set
*/
public
void
setUser(User user) {
this
.
user
= user;
}
/**
*
@return
the
msg
*/
public
String getMsg() {
return
msg
;
}
/**
*
@param
msg
the
msg
to
set
*/
public
void
setMsg(String msg) {
this
.
msg
= msg;
}
/**
*
处理用户请求的
login()
方法
*
@return
结果导航字符串
*
@throws
Exception
*/
public
String login()
throws
Exception{
String userName =
user
.getUserName();
String password =
user
.getPassword();
if
(
"test"
.equals(userName) &&
"test"
.equals(password)){
msg
=
"
登录成功,欢迎
"
+ userName;
//
获取
ActionContext
实例,通过它来访问
Servlet API
ActionContext context = ActionContext.getContext();
//
看
session
中是否已经存放了用户名,如果存放了:说明已经登录了;否则说明是第一次登录成功
if
(
null
!= context.getSession().get(
"uName"
)){
msg
= userName +
"
:你已经登录过了
!!!"
;
}
else
{
context.getSession().put(
"uName"
, userName);
}
return
this
.
SUCCESS
;
}
else
{
msg
=
"
登录失败,用户名或密码错
"
;
return
this
.
ERROR
;
}
}
public
String regist()
throws
Exception{
//
将用户名,密码添加到数据库中
//...
msg
=
"
注册成功。
"
;
return
this
.
SUCCESS
;
}
}
|
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<
package
name
=
"my"
extends
=
"struts-default"
namespace
=
"/manage"
>
<
action
name
=
"userOpt"
class
=
"org.qiujy.web.struts2.action.UserAction"
>
<
result
name
=
"success"
>
/success.jsp
</
result
>
<
result
name
=
"error"
>
/error.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
|
<%@
page
language
=
"java"
pageEncoding
=
"UTF-8"
%>
<
html
>
<
head
>
<
title
>
用户登录页面
</
title
>
</
head
>
<
body
>
<
h2
>
用户入口
</
h2
>
<
hr
>
<
form
action
=
"manage/userOpt!login.action"
method
=
"post"
>
<
table
border
=
"1"
>
<
tr
>
<
td
>
用户名:
</
td
>
<
td
>
<
input
type
=
"text"
name
=
"user.userName"
/>
</
td
>
</
tr
>
<
tr
>
<
td
>
密码:
</
td
>
<
td
>
<input type="password" name="user.password"/>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
>
<
input
type
=
"submit"
value
=
"
确定
"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
html
>
|
public
String regist()
throws
Exception{
//
将用户名,密码添加到数据库中
//...
//msg = "
注册成功。
";
if
(
true
){
throw new
java.sql.SQLException(
"
没有数据库驱动程序
"
);
}
return this.SUCCESS;
}
|
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<
struts
>
<
package
name
=
"my"
extends
=
"struts-default"
namespace
=
"/manage"
>
<!--
定义全局处理结果
-->
<
global-results
>
<!--
逻辑名为
sql
的结果,映射到
/exception.jsp
页面
-->
<
result
name
=
"sql"
>
/exception.jsp
</
result
>
</
global-results
>
<
global-exception-mappings
>
<!--
当
Action
抛出
SQLException
异常时,转入名为
sql
的结果
-->
<
exception-mapping
exception
=
"java.sql.SQLException"
result
=
"sql"
/>
</
global-exception-mappings
>
<
action
name
=
"userOpt"
class
=
"org.qiujy.web.struts2.action.UserAction"
>
<
result
name
=
"success"
>
/success.jsp
</
result
>
<
result
name
=
"error"
>
/error.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
|
<%@
page
language
=
"java"
pageEncoding
=
"utf-8"
%>
<%@
taglib
uri
=
"/struts-tags"
prefix
=
"s"
%>
<
html
>
<
head
>
<
title
>
异常信息
</
title
>
</
head
>
<
body
>
<
h2
>
出现异常啦
</
h2
>
<
hr
/>
<
h3
style
=
"color:red"
>
<!--
获得异常对象
-->
<
s:property
value
=
"exception.message"
/>
</
h3
>
<
br
/>
<!--
异常堆栈信息
-->
<
s:property
value
=
"exceptionStack"
/>
</
html
>
|