Redirect from jsp in struts2 application

I am creating a tomcat web application in the struts2 framework. I use interceptors to save a user session by storing the user object in the session. Interceptors work fine to check a user object in a session when there is a call to action, but there is jsp in my application, and these jsps can be called directly via url. In this case, I want to redirect the user to the login page if there is no user object in the session. I am currently using the sendRedirect method of i jsp to redirect to the login page. Could you tell me if this is the best way to do this?

+4
source share
3 answers

Yes; Do not have direct access to JSP pages. This is contrary to what is appropriate in an MVC application.

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

In Url, you can specify your action name, which in turn will directly call your jsp results page.

+1
source

Wrap these JSP pages with a few simple steps, for example:

The struts.xml method:

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

or plug-in path to the Convention, create the class as follows:

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

And then whenever you linked to example.jsp use example.action

0
source

Source: https://habr.com/ru/post/1402018/


All Articles