简体   繁体   English

在struts中登录后如何重定向到上一页

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

The are some jsp pages in my project in which they all can be accessed only when there is a user session. 这些是我项目中的一些jsp页面,只有在存在用户会话时,才能全部访问它们。 I have an interceptor for check whether session is in or not. 我有一个拦截器来检查会话是否在。 If the session out, the page is redirected to login page. 如果会话结束,页面将重定向到登录页面。 But after login success i need to be redirected to the page which working earlier. 但是登录成功后,我需要重定向到较早运行的页面。 For eg: If i am working in xxx.jsp page, when the session out, i directed to login page. 例如:如果我在xxx.jsp页面中工作,则在会话结束时,我定向到登录页面。 After successful login i need to redirect to xxx.jsp. 成功登录后,我需要重定向到xxx.jsp。 plz help me. 请帮助我。

In Action.class 在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());

    }

In struts.xml 在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>

In new_login_page.jsp 在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>

In struts.xml 在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>

In ActionRedirect.class 在ActionRedirect.class中

private String backurl;//Getter & Setter Method.

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

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

In struts you use actions for business logic. 在struts中,您将操作用于业务逻辑。 and you write these actions in struts-config.xml. 然后将这些操作写入struts-config.xml中。 in action class you return action mapping to redirect to any jsp page or servlet as you want. 在动作类中,您将返回动作映射以根据需要重定向到任何jsp页面或servlet。 here below i show you example 在下面我给你看例子

In struts-config 在struts-config中

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

</form-beans>

above name attribute is used for input form and type is action class you register with form. 上面的name属性用于输入表单,类型是您在表单中注册的动作类。 Below is action class where you write business logic 以下是您编写业务逻辑的操作类

 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");
}

now come back to struts-config and their are two tags 现在回到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>

you can use both global forward and forward in action tag to redirect where ever you want 您可以同时使用全局转发和转发操作标签来重定向到所需的位置

************updated********************** ************更新**********************

Above is strut1 and below is for struts 2 . 上面是strut1,下面是strut2。 you have defined the xml file like strut-config .Although redirecting process is same but their is very small difference eg in xml file 您已经定义了类似于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> 

here constant tag show that we are in developer mode.this will help to show logs for debuging your application. 这里的constant标签表明我们处于开发人员模式。这将有助于显示调试应用程序的日志。 and result tage is for redirecting to any jsp page you want 结果年龄是用于重定向到您想要的任何jsp页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM