您的位置:首页 > 编程语言 > Java开发

使用struts框架处理异常

2015-01-22 00:00 141 查看
struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- 指定Struts 2配置文件的根元素 -->
<struts>
<!-- 指定全局国际化资源文件 -->
<constant name="struts.custom.i18n.resources" value="mess"></constant>
<!-- 指定国际化编码所使用的字符集 -->
<constant name="struts.i18n.encoding" value="GBK"></constant>
<!-- 设置常量开启动态调用方法 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

<!-- 所有的Action定义都应该放在package下 -->
<package name="frank" extends="struts-default">
<!-- 定义全局的结果映射,当局部没找到的情况下就会在全局中找 -->
<global-results>
<result name="error">error.jsp</result>
<result name="exception">error.jsp</result>
</global-results>
<!-- 全局的异常映射 -->
<global-exception-mappings>
<!-- 定义一个异常映射,也可以定义局部异常映射 -->
<exception-mapping exception="java.lang.Exception" result="exception"></exception-mapping>
</global-exception-mappings>
<!-- 使用通配符*_* -->
<action name="login" class="org.sadhu.app.action.RegistAction">
<!-- 局部的结果映射 -->
<result type="dispatcher" name="success">
<param name="location">welcome.jsp</param>
</result>
</action>
</package>

</struts>

RegisterAction.java

package org.sadhu.app.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.PreResultListener;

/*
* 使用struts框架处理异常
*
*  */
public class RegistAction extends ActionSupport {
private String userName;//账号
private String password;//密码
private String tip;//提示消息
public String getUserName()
{
return this.userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return this.password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getTip()
{
return this.tip;
}
public void setTip(String tip)
{
this.tip = tip;
}
public String execute() throws Exception
{
ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
invocation.addPreResultListener(new PreResultListener() {

@Override
public void beforeResult(ActionInvocation invocation, String resultCode) {
System.out.println("返回的逻辑视图名字为:"+resultCode);
//在返回Result之前加入一个额外的数据。
invocation.getInvocationContext().put("extra",new java.util.Date()
+"由"+resultCode+"逻辑视图名转入");
}
});
if(getUserName().equals("1"))
{
String s = null;
s.toString();
throw new Exception();//抛出一个异常
}
if(getUserName().equals("sadhu") && getPassword().equals("sadhu"))
{
ActionContext.getContext().getSession().put("user", getUserName());
setTip("欢迎,"+getUserName()+",您已经登陆成功!");
return SUCCESS;
}
else
{
return ERROR;
}
}
}

error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆错误</title>
</head>
<body>登陆错误!
<!-- 显示错误消息 -->
<s:property value="exception.message" />
<!-- 显示堆栈信息 -->
<s:property value="exceptionStack" />
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: