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

struts2全局结果与异常处理(五)

2013-06-12 13:43 309 查看

一、struts2局部结果与异常

1、以用户登录为例

登录界面login.jsp

<body>
<h3>用户登录</h3>
<form action="login" method = "post">
username:<input type = "text" name = "username"/><br/>
password:<input type = "text" name = "password"/><br/>
<input type = "submit" value = "submit"/>
</form>
</body>
定义自定义的登录异常类

public class LoginException extends Exception
{
private String message;

public LoginException(String message)
{
super(message);
this.message = message;
}
public LoginException()
{
super();
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
对应的处理action:LoginAction

public class LoginAction extends ActionSupport
{
private String username;
private String password;
public String execute()throws Exception
{
//处理异常
if("liusheng".equals(this.username))
{
throw new LoginException("username is error");
}
if("1234".equals(this.password))
{
throw new LoginException("password is error");
}
return SUCCESS;
}
public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
}


局部异常配置

如果不进行异常的配置,当出现异常时,会将错误信息的堆栈信息打印出来,进行配置后将会出现配置的信息页面

<?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>
<package name="struts2" extends="struts-default">
<action name="login" class="com.liusheng.action.LoginAction">
<result name="success">index.jsp</result>
<!-- exception:是对于抛出该处出现的异常进行处理
result:自定义的结果名,有其对应的result进行处理
-->
<exception-mapping result="loginException" exception="com.liusheng.exception.LoginException"></exception-mapping>
<!-- 配置局部异常处理-->
<result name="loginException">exception.jsp</result>
</action>
</package>
</struts>


二、struts2全局结果与异常

定义另一个异常处理类
public class PasswordException extends Exception
{
private String message;
public PasswordException(String message)
{
super(message);
this.message = message;
}
public PasswordException()
{
super();
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
定义出现密码错误时抛出上述定义的异常
public class LoginAction extends ActionSupport
{
private String username;
private String password;
public String execute()throws Exception
{
//处理异常
if(!"liusheng".equals(this.username))
{
throw new LoginException("username is error");
}
if(!"1234".equals(this.password))
{
throw new PasswordException("password is error");
}
return SUCCESS;
}
public String getUsername()
{
return username;
}

public void setUsername(String username)
{
this.username = username;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
}
定义出现密码错误抛出全局异常
<?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>
<package name="struts2" extends="struts-default">
<!-- 配置全局结果 -->
<global-results>
<result name="passwordException">passexception.jsp</result>
</global-results>
<!-- 配置全局异常 -->
<global-exception-mappings>
<exception-mapping result="passwordException" exception="com.liusheng.exception.PasswordException"></exception-mapping>
</global-exception-mappings>
<action name="login" class="com.liusheng.action.LoginAction">
<result name="success">index.jsp</result>
<!-- exception:是对于抛出该处出现的异常进行处理
result:自定义的结果名,有其对应的result进行处理
-->
<exception-mapping result="loginException" exception="com.liusheng.exception.LoginException"></exception-mapping>
<!-- 配置局部异常处理-->
<result name="loginException">exception.jsp</result>
</action>
</package>
</struts>


说明:

对于struts.xml文件的结果配置来说,局部要优于全局。

我们即可以再Action中定义异常与结果,也可以定义全局的异常与结果,局部总是优于全局的,如果定义成全局,那么可以为所有的Action所公用,而局部的异常与结果只能被当前的Action所独享,不能为其他Action所共享。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: