webwork对错误消息的支持:

对错误消息的支持:

在WebWork 2中, 有两类错误消息: 字段错误消息和活动错误消息. 字段错误消息代表某一控件的问题并显示在控件中. 许多标签支持显示这种消息. 另一方面活动错误消息代表活动执行的问题. 在Web应用中很多事物会导致错误, 尤其是在依赖于外部资源的应用中如数据库, 远程Web服务, 或其他在活动执行期间可能不可用的资源. 能否优雅的处理错误并向用户提供有用的消息通常是用户体验好与坏的区别.

当这种错误发生时, 把消息从表单控件中分离出来单独显示更为合适. 下例中, 我们将创建一个可以用列表现活动错误消息的定制组件. 该组件可以用于全部显示这类错误消息的表单中.
下面的活动将处理网站上的广告: 免费的电子证书. 它将尝试发送证书邮件, 但会抛出异常.

活动类:
[code]
package example;

import com.opensymphony.xwork.ActionSupport;

public class AddUser extends ActionSupport {

private String fullname;
private String email;

public String execute() throws Exception {
// we are ignoring field validation in this example

try {
MailUtil.sendCertificate(email, fullname);
} catch (Exception ex) {
// there was a problem sending the email
// in a real application, we would also
// log the exception
addActionError("We are experiencing a technical problem and have contacted our support staff. " +
"Please try again later.");
}

if (hasErrors()) {
return ERROR;
} else {
return SUCCESS;
}
}

public String getFullname() {
return fullname;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}
[/code]

Jsp页面:
[code]
<%@ taglib uri="webwork" prefix="ui" %>

<html>
<head><title>custom component example</title></head>

<!-- don't forget this -->
<link rel ="stylesheet" type="text/css" href="/webwork-example/template/xhtml/styles.css" title="Style">

<body>

<ui:form action="AddUser.action" method="POST">
<table>
<ui:component template="action-errors.vm" />
<ui:textfield label="Full Name" name="fullname" />
<ui:textfield label="Email" name="email" />
<ui:submit name="submit" value="Send me a free E-Certificate!" />
</table>
</ui:form>

</body>
</html>
[/code]
HTML输出(提交前):
[code]
<html>
<head><title>custom component example</title></head>
<link rel ="stylesheet" type="text/css" href="/webwork-example/template/xhtml/styles.css" title="Style">

<body>

<form action="AddUser.action" method="POST" />

<table>


<tr>
<td align="right" valign="top"><span class="label">Full Name:</span></td>
<td>
<input type="text" name="fullname" value="" />
</td>
</tr>
<tr>
<td align="right" valign="top"><span class="label">Email:</span></td>
<td>
<input type="text" name="email" value="" />
</td>
</tr>

<tr>
<td colspan="2">
<div align="right">
<input type="submit" name="submit" value="Send me a free E-Certificate!"/>
</div>
</td>
</tr>


</table>
</form>

</body>
</html>
[/code]
下面的模版将循环全部活动错误并显示在列表中.
模版(action-errors.vm)
[code]
#set ($actionErrors = $stack.findValue("actionErrors"))

#if ($actionErrors)
<tr>
<td colspan="2">
<span class="errorMessage">The following errors occurred:</span>
<ul>
#foreach ($actionError in $actionErrors)
<li><span class="errorMessage">$actionError</span></li>
#end
</ul>
</td>
</tr>
#end
[/code]
HTML输出(提交之后):
[code]
<html>
<head><title>custom component example</title></head>
<link rel ="stylesheet" type="text/css" href="/webwork-example/template/xhtml/styles.css" title="Style">

<body>

<form action="AddUser.action" method="POST" />

<table>

<tr>
<td colspan="2">
<span class="errorMessage">The following errors occurred:</span>
<ul>
<li class="errorMessage">
We are experiencing a technical problem and have contacted our
support staff. Please try again later.
</li>
</ul>
</td>
</tr>

<tr>
<td align="right" valign="top"><span class="label">Full Name:</span></td>
<td>
<input type="text" name="fullname" value="Sample User" />
</td>
</tr>
<tr>
<td align="right" valign="top"><span class="label">Email:</span></td>
<td>
<input type="text" name="email" value="user@example.com" />
</td>
</tr>

<tr>
<td colspan="2">
<div align="right">
<input type="submit" name="submit" value="Send me a free E-Certificate!"/>
</div>
</td>
</tr>


</table>
</form>

</body>
</html>

[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值