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(); 
 }
-----------------------------------------------------------------------
实现得效果同上边第一种方法一样.
原创粉丝点击