webwork对错误消息的支持:

阅读更多
对错误消息的支持:

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

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

活动类:
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;
    }

}


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


custom component example








    
    
    

HTML输出(提交前):

custom component example




Full Name:
Email:

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

#if ($actionErrors)

    
        The following errors occurred:
        
    #foreach ($actionError in $actionErrors)
  • $actionError
  • #end
#end

HTML输出(提交之后):

custom component example




The following errors occurred:
  • We are experiencing a technical problem and have contacted our support staff. Please try again later.
Full Name:
Email:

你可能感兴趣的:(Webwork,活动,Web,UI,JSP)