簡體   English   中英

在struts中登錄后如何重定向到上一頁

[英]how to redirect to previous page after login in struts

這些是我項目中的一些jsp頁面,只有在存在用戶會話時,才能全部訪問它們。 我有一個攔截器來檢查會話是否在。 如果會話結束,頁面將重定向到登錄頁面。 但是登錄成功后,我需要重定向到較早運行的頁面。 例如:如果我在xxx.jsp頁面中工作,則在會話結束時,我定向到登錄頁面。 成功登錄后,我需要重定向到xxx.jsp。 請幫助我。

在Action.class中

 try {
     HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String backurl = request.getHeader("referer");
        System.out.println(" Backurl : " + backurl);
        Map session = ActionContext.getContext().getSession();
        if (session.get("cus") != null) {
            return "already_login";
        } else {

            return "not_yet_login";
        }

    } catch (Exception e) {
        logger.info("Error : " + e);
        logger.info("Error Message : " + e.getMessage());

    }

在struts.xml中

<action name="call_Action_Class" class="controller.Action.class">
        <result name="already_login">success.jsp</result>
        <result name="not_yet_login">new_login_page.jsp</result>
</action>

在new_login_page.jsp中

<form action="login_dedirect" method="post" id="logsubmit">

      <h2>Email :</h2>
      <input type="text" name="email"  />
      <h2 >Password :</h2>
      <input type="password" name="passwd" id="pwd" />
      <input type="submit"  value="Login" />
 </form>

在struts.xml中

<action name="login_dedirect" class="controller.ActionRedirect.class">
        <result name="success" type="redirect" >${backurl}</result>
        <result name="fail">new_login_page.jsp</result>
</action>

在ActionRedirect.class中

private String backurl;//Getter & Setter Method.

try{
   System.out.println(" Backurl :"+backurl);

   return "success";
}catch(Exception e){
     return "fail";
}

在struts中,您將操作用於業務邏輯。 然后將這些操作寫入struts-config.xml中。 在動作類中,您將返回動作映射以根據需要重定向到任何jsp頁面或servlet。 在下面我給你看例子

在struts-config中

 <form-beans>
    <form-bean name="Register" type="FormBeans.Register"/>

</form-beans>

上面的name屬性用於輸入表單,類型是您在表單中注冊的動作類。 以下是您編寫業務邏輯的操作類

 public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Register r=(Register)form;
    DBManager db=new DBManager();
    boolean rs=db.checkCustomer(r);
    if(rs){
        HttpSession session =request.getSession(true);
        session.setAttribute("fname", r.getEmail());
        return mapping.findForward(SUCCESS);
    }

    return mapping.findForward("failure");
}

現在回到struts-config,它們是兩個標簽

<action-mappings>
    <action input="/WEB_INF/registeration.jsp" name="Register" path="/register"    scope="session" type="actions.registration" validate="false">
    <forward name="failure" path="/registeration.jsp"/>
    <forward name="success" path="/mainmenu.jsp"/>
     </action>
    </action-mapping>

您可以同時使用全局轉發和轉發操作標簽來重定向到所需的位置

************更新**********************

上面是strut1,下面是strut2。 您已經定義了類似於strut-config的xml文件。盡管重定向過程相同,但是它們之間的差別很小,例如在xml文件中

<struts> 
<constant name="struts.devMode" value="true" /> 
  <package name="helloworld" extends="struts-default"> 

   <action name="hello" 
     class="HelloWorldAction" 
       method="execute"> 
    <result name="success">/HelloWorld.jsp</result> 
   </action> 
  </package> 
  </struts> 

這里的constant標簽表明我們處於開發人員模式。這將有助於顯示調試應用程序的日志。 結果年齡是用於重定向到您想要的任何jsp頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM