简体   繁体   English

从struts2应用程序中的jsp重定向

[英]redirect from jsp in struts2 application

I am creating a web application on tomcat in framework struts2. 我在框架struts2中在tomcat上创建一个Web应用程序。 I am using intercepters to maintain user session by saving user object in session. 我正在使用拦截器通过在会话中保存用户对象来维护用户会话。 Intercepters work fine in order to check user object in session when there is call to action but there are some jsp as well in my application and these jsp can be called directly by url. 拦截器工作正常,以便在有调用操作时检查会话中的用户对象,但在我的应用程序中也有一些jsp,这些jsp可以直接通过url调用。 In this case i want to redirect user to login page if there is no user object in session. 在这种情况下,如果会话中没有用户对象,我想将用户重定向到登录页面。 Currently i am using sendRedirect method i jsp to redirect to login page. 目前我正在使用sendRedirect方法i jsp重定向到登录页面。 Could you please tell me if these is any better way of doing this? 你能告诉我这些是否有更好的方法吗?

Yes; 是; don't directly access JSP pages. 不要直接访问JSP页面。 Doing so is counter to what's appropriate in an MVC app. 这样做与MVC应用程序中适当的相反。

<html>
    <head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=XmlAction">
    </head>
<body>

</body>
</html>

In Url you can mention your action name which in turn call your jsp result page directly. 在Url中,您可以提及您的操作名称,然后直接调用您的jsp结果页面。

"Wrap" those JSP pages with some simple actions like: 使用一些简单的操作“包装”这些JSP页面,例如:

The struts.xml way: struts.xml方式:

<action name="example">
    <result>example.jsp</result>
</action>

or the Convention plugin way, create a class like this: 或者使用Convention插件方式,创建一个这样的类:

class ExampleAction extends ActionSupport {
    @Override
    public String execute() {
        return "example"; // provided example.jsp is in /WEB-INF/content/
    }
}

And then whenever you were linking to example.jsp , use example.action 然后,无论何时链接到example.jsp ,都要使用example.action

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

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