structs中exception得处理
来源:互联网 发布:php 上海时区 编辑:程序博客网 时间:2024/04/29 18:33
昨天用了两种方法来实现Structs抛出异常,并且做界面转发.
两种方法各有好处.
1.用structs自定义得类org.apache.struts.util.ModuleException来做.
----------------------------------------------------------------
struct-config.xml中的设置
<action-mappings>
<action path="/login"
type="com.maesinfo.carmen.framework.security.LoginAction"
scope="request" name="loginForm" validate="true"
input="/login.jsp">
<forward name="Success" path="/success.jsp" />
<forward name="Failure" path="/login.jsp" redirect="true" />
</action>
<action-mappings>
<global-exceptions>
<exception key="global.error.invalidlogin" path="/login.jsp"
scope="request"
type=" org.apache.struts.util.ModuleException "/>
</global-exceptions>
<message-resources
parameter="com.maesinfo.carmen.struts.ApplicationResources" />
------------------------------------------------------------------
properties中得设置:
global.error.invalidlogin = 登入失败!请重新登入.
------------------------------------------------------------------
Action中得设置:
try {
customer = serviceImpl.authenticate(user);
} catch( BaseException be){
throw new ModuleException("global.error.invalidlogin");
}
在catch BaseException 得时候抛出ModuleException让struct中得
org.apache.struts.util.ModuleException捕获.最后得到
exception key="global.error.invalidlogin" 转发给longin.jsp
2.自己做类得Handler来捕获
----------------------------------------------------------------
struct-config.xml中的设置
<action-mappings>
<action path="/login"
type="com.maesinfo.carmen.framework.security.LoginAction"
scope="request" name="loginForm" validate="true"
input="/login.jsp">
<exception key="global.error.invalidlogin" path="/login.jsp"
handler="com.maesinfo.carmen.framework.exceptions.InvalidLoginExceptionHandler"
type="com.maesinfo.carmen.framework.exceptions.InvalidLoginException" />
<forward name="Success" path="/success.jsp" />
<forward name="Failure" path="/login.jsp" redirect="true" />
</action>
</action-mappings>
----------------------------------------------------------------
InvalidLoginExceptionHandler:
package com.maesinfo.carmen.framework.exceptions;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
public class InvalidLoginExceptionHandler extends ExceptionHandler {
public ActionForward execute(Exception ex, ExceptionConfig config,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
ActionMessages errors = new ActionMessages();
ActionForward forward = null;
ActionMessage error = null;
String property = null;
/*
* Get the path for the forward either from the exception element or
* from the input attribute.
*/
String path = null;
if (config.getPath() != null) {
path = config.getPath();
} else {
path = mapping.getInput();
}
// Construct the forward object
forward = new ActionForward(path);
if ( ex instanceof InvalidLoginException ) {
processInvalidLoginException((InvalidLoginException) ex, config,
request, forward, property, errors);
List exceptions = ((InvalidLoginException) ex).getExceptions();
if (exceptions != null && !exceptions.isEmpty()) {
int size = exceptions.size();
Iterator iter = exceptions.iterator();
while (iter.hasNext()) {
// All subexceptions must be InvalidLoginException
InvalidLoginException subException = (InvalidLoginException) iter.next();
processInvalidLoginException(subException, config, request,
forward, property, errors);
}
}
} else {
error = new ActionMessage(config.getKey());
property = error.getKey();
storeException(request, property, error, forward,
config.getScope(), errors);
}
return forward;
}
private void processInvalidLoginException(InvalidLoginException ex,
ExceptionConfig config, HttpServletRequest request,
ActionForward forward, String property, ActionMessages errors) {
String messageKey = ex.getMessageKey();
ActionMessage error = null;
Object[] exArgs = ex.getMessageArgs();
if (exArgs != null && exArgs.length > 0) {
// If there were args provided, use them in the ActionError
error = new ActionMessage(messageKey, exArgs);
} else {
// Create an ActionError without any arguments
error = new ActionMessage(messageKey);
}
storeException(request, property, error, forward, config.getScope(),
errors);
}
protected void storeException(HttpServletRequest request, String property,
ActionMessage error, ActionForward forward, String scope,
ActionMessages errors) {
errors.add(property, error);
if ("request".equals(scope)) {
request.setAttribute(Globals.ERROR_KEY, errors);
} else {
request.getSession().setAttribute(Globals.ERROR_KEY, errors);
}
}
}
-----------------------------------------------------
Action中不做任何得catch操作:
customer = serviceImpl.authenticate(user);
--------------------------------------------------------
在service中:调用一下方法:
public User authenticate(User user) throws InvalidLoginException,DatastoreException {
List results = DaoFactory.getUserDao().findByExample(user);
if(results.isEmpty()) {
System.out.println("InvalidLoginException出错");
InvalidLoginException ie = new InvalidLoginException() ;
ie.setMessageKey("global.error.invalidlogin");
throw ie;
}
return (User)results.iterator().next();
}
-----------------------------------------------------------------------
实现得效果同上边第一种方法一样.
两种方法各有好处.
1.用structs自定义得类org.apache.struts.util.ModuleException来做.
----------------------------------------------------------------
struct-config.xml中的设置
<action-mappings>
<action path="/login"
type="com.maesinfo.carmen.framework.security.LoginAction"
scope="request" name="loginForm" validate="true"
input="/login.jsp">
<forward name="Success" path="/success.jsp" />
<forward name="Failure" path="/login.jsp" redirect="true" />
</action>
<action-mappings>
<global-exceptions>
<exception key="global.error.invalidlogin" path="/login.jsp"
scope="request"
type="
</global-exceptions>
<message-resources
parameter="com.maesinfo.carmen.struts.ApplicationResources" />
------------------------------------------------------------------
properties中得设置:
global.error.invalidlogin = 登入失败!请重新登入.
------------------------------------------------------------------
Action中得设置:
try {
customer = serviceImpl.authenticate(user);
} catch(
}
在catch BaseException 得时候抛出ModuleException让struct中得
org.apache.struts.util.ModuleException捕获.最后得到
exception key="global.error.invalidlogin" 转发给longin.jsp
2.自己做类得Handler来捕获
----------------------------------------------------------------
struct-config.xml中的设置
<action-mappings>
<action path="/login"
type="com.maesinfo.carmen.framework.security.LoginAction"
scope="request" name="loginForm" validate="true"
input="/login.jsp">
<exception key="global.error.invalidlogin" path="/login.jsp"
<forward name="Success" path="/success.jsp" />
<forward name="Failure" path="/login.jsp" redirect="true" />
</action>
</action-mappings>
----------------------------------------------------------------
package com.maesinfo.carmen.framework.exceptions;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
public class InvalidLoginExceptionHandler extends ExceptionHandler {
public ActionForward execute(Exception ex, ExceptionConfig config,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
ActionMessages errors = new ActionMessages();
ActionForward forward = null;
ActionMessage error = null;
String property = null;
/*
* Get the path for the forward either from the exception element or
* from the input attribute.
*/
String path = null;
if (config.getPath() != null) {
path = config.getPath();
} else {
path = mapping.getInput();
}
// Construct the forward object
forward = new ActionForward(path);
if (
processInvalidLoginException((InvalidLoginException) ex, config,
request, forward, property, errors);
int size = exceptions.size();
Iterator iter = exceptions.iterator();
while (iter.hasNext()) {
// All subexceptions must be InvalidLoginException
InvalidLoginException subException = (InvalidLoginException) iter.next();
processInvalidLoginException(subException, config, request,
forward, property, errors);
}
}
} else {
error = new ActionMessage(config.getKey());
property = error.getKey();
storeException(request, property, error, forward,
config.getScope(), errors);
}
return forward;
}
ExceptionConfig config, HttpServletRequest request,
ActionForward forward, String property, ActionMessages errors) {
String messageKey = ex.getMessageKey();
ActionMessage error = null;
Object[] exArgs = ex.getMessageArgs();
if (exArgs != null && exArgs.length > 0) {
// If there were args provided, use them in the ActionError
error = new ActionMessage(messageKey, exArgs);
} else {
error = new ActionMessage(messageKey);
}
storeException(request, property, error, forward, config.getScope(),
errors);
protected void storeException(HttpServletRequest request, String property,
ActionMessage error, ActionForward forward, String scope,
ActionMessages errors) {
errors.add(property, error);
if ("request".equals(scope)) {
request.setAttribute(Globals.ERROR_KEY, errors);
} else {
request.getSession().setAttribute(Globals.ERROR_KEY, errors);
}
}
}
-----------------------------------------------------
Action中不做任何得catch操作:
customer = serviceImpl.authenticate(user);
--------------------------------------------------------
在service中:调用一下方法:
public User authenticate(User user) throws InvalidLoginException,DatastoreException {
List results = DaoFactory.getUserDao().findByExample(user);
if(results.isEmpty()) {
System.out.println("InvalidLoginException出错");
InvalidLoginException ie = new InvalidLoginException() ;
}
return (User)results.iterator().next();
}
-----------------------------------------------------------------------
实现得效果同上边第一种方法一样.
- structs中exception得处理
- 在structs中处理jsp页面的中文乱码问题
- Java中Exception的处理
- Java中Exception的处理
- oracle中exception的处理
- structs中国际化小结
- structs中使用 DelegatingActionProxy
- structs中国际化小结
- structs中文乱码处理----filter
- Java多层应用中Exception的处理
- .Net 中ObjectDataSource 的Exception 处理
- VC++中unhandled exception的处理
- Exception---Android中处理崩溃异常
- Qt中处理First chance exception异常
- java 线程中unchecked exception处理
- Python中Exception的异常处理
- 对话框中菜单得空闲处理
- structs
- 突破磁盘低级检测实现文件隐藏
- 单元测试
- 十大IT危机处理高手
- 一次曲折的安全检测
- 国外FlashFxp解密代码
- structs中exception得处理
- Nmap的高级用法指南
- X Window研究笔记(13)
- 两正点电荷产生电场线
- Checked Exception与Runtime Exception 的区别
- 创业
- 通用磁盘格式文件系统研究
- 信息安全的职业生涯
- finding the registered URL protocols