Java 如何在struts中登录后重定向到上一页
我的项目中有一些jsp页面,只有在有用户会话时才能访问它们。我有一个拦截器来检查会话是否在。如果会话结束,页面将重定向到登录页面。 但是在登录成功后,我需要被重定向到之前工作的页面。 例如:如果我在xxx.jsp页面中工作,当会话结束时,我将指向登录页面。成功登录后,我需要重定向到xxx.jsp。Java 如何在struts中登录后重定向到上一页,java,session,struts2,interceptor,Java,Session,Struts2,Interceptor,我的项目中有一些jsp页面,只有在有用户会话时才能访问它们。我有一个拦截器来检查会话是否在。如果会话结束,页面将重定向到登录页面。 但是在登录成功后,我需要被重定向到之前工作的页面。 例如:如果我在xxx.jsp页面中工作,当会话结束时,我将指向登录页面。成功登录后,我需要重定向到xxx.jsp。 请帮助我。在struts中,您将操作用于业务逻辑。您可以在struts-config.xml中编写这些操作。在action类中,返回动作映射以重定向到任意jsp页面或servlet。下面我给你举个例子
请帮助我。在struts中,您将操作用于业务逻辑。您可以在struts-config.xml中编写这些操作。在action类中,返回动作映射以重定向到任意jsp页面或servlet。下面我给你举个例子
<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>
在struts配置中
<form-beans>
<form-bean name="Register" type="FormBeans.Register"/>
</form-beans>
<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>
现在回到struts配置,它们有两个标签
<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>
您可以使用“全局转发”和“动作中转发”标记重定向到任何需要的位置
<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>
*************更新************************************
<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>
上面是支柱1,下面是支柱2。
您已经定义了类似于struct config的xml文件。虽然重定向过程是相同的,但它们的差别非常小,例如在xml文件中
<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>
<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>
/HelloWorld.jsp
这里的常量标记显示我们处于开发人员模式。这将有助于显示调试应用程序的日志。结果是重定向到Action.class中所需的任何jsp页面
<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>
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-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>
<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>
<action name="login_dedirect" class="controller.ActionRedirect.class">
<result name="success" type="redirect" >${backurl}</result>
<result name="fail">new_login_page.jsp</result>
</action>
这不是我需要的。。例如:如果我在xxx.jsp页面中工作,当会话结束时,我将指向登录页面。成功登录后,我需要重定向到xxx.jsp该问题表示struts2。这个例子是struts1。