2010-01-05 141 views
2

我有一個用Struts 1編寫的遺留應用程序。我被要求添加的唯一功能是保護一些操作。目前任何用戶都可以做他/她想要的任何事情。這個想法是允許所有用戶看到的數據,但塊修改操作,即修改用戶應該登錄的數據。使用Struts實現Struts 2攔截器1

我知道Struts2有攔截器,所以我可以將它們附加到所需的操作並轉發用戶登錄頁面需要時。但是我怎麼能在Struts 1應用程序中做類似的事情呢?

我的第一個想法是創建自己的抽象Action類:

public class AuthenticatedAction { 
    public ActionForward execute(
     ActionMapping mapping, 
     ActionForm form, 
     HttpServletRequest theRequest, 
     HttpServletResponse theResponse) { 
      if (!logged) { 
       // forward to log in form 
      } else { 
       doExecute(mapping, form, request, response); 
      } 
     } 

public abstract ActionForward doExecute(
    ActionMapping mapping, 
    ActionForm form, 
    HttpServletRequest theRequest, 
    HttpServletResponse theResponse); 
} 

然後改變需要從

extends Action 

身份驗證

extends AuthenticatedAction 

再加入登錄表單的所有行動,登錄操作(執行身份驗證並將此狀態放入會話中)並更改JS P標題圖塊顯示驗證塊,例如「你是(未登錄)/」,登錄/註銷。我猜這應該可以解決問題。

  1. 如果這不能解決問題,請解釋我爲什麼。
  2. 有沒有更好的(更像攔截器一樣優雅)的方式來做到這一點?

在此先感謝您。

回答

2

我想你可以使用過濾器來實現你的要求。 你可以在你的web.xml中聲明一個過濾器,並提供一個適用的url模式。

< filter> 
< filter-name>filterName< /filter-name> 
< filter-class>filter.class</filter-class> 

< filter-mapping> 
< filter-name>filterName< /filter-name> 
< url-pattern>*.*< /url-pattern> 
< /filter-mapping> 

一個相關鏈接here

+0

男孩我很希望我不需要這個答案,但是我的生活是這樣的。感謝您提供它! :) – Art 2012-08-27 06:55:35

0

我覺得好辦法是創建過濾器類做在web.xml中 條目現在您的郝曉紅做編碼像下面

public void doFilter(ServletRequest request, ServletResponse response, 

    FilterChain chain) throws IOException, ServletException { 
    System.out.println("coming from globester filtersss 2"); 
    HttpServletRequest requesth = (HttpServletRequest) request; 
     HttpServletResponse responseh = (HttpServletResponse) response; 
     HttpSession session = requesth.getSession(); 

     String ss=requesth.getParameter("cat"); 

     if(ss!=null && ss.equalsIgnoreCase("trues")){ 

     }else{ 
     responseh.sendRedirect("searchSanta.jsp?cat=trues"); 
     } 
}