Maky-chan
Maky-chan

Reputation: 39

Redirect jsp to another jsp (Struts1)

This is the first time for me to work with Struts1. I want to redirect from a jsp to another jsp. For example: if the user not in session then redirect it to login.jsp; else go to menu.jsp.

I tried to make a tile: isLogged.jsp that contains:

<logic:present name="user">
    <jsp:forward page="menu.do"/>
</logic:present>
<logic:notPresent name="user">
    <jsp:forward page="login.do"/>
</logic:notPresent>

But it don´t redirect and the page is in blank (all empty in the code).

I do call the tile in all the pages of the site inside of the .

What am I doing wrong?

Upvotes: 1

Views: 5852

Answers (1)

Paul Vargas
Paul Vargas

Reputation: 42010

There are several solutions 1:

  • Using ActionForwards

    If you have in your struts-config.xml:

    <action-mappings>
      <action path="/login" forward="/login.jsp"/>
      <action path="/menu" forward="/menu.jsp"/>
    </action-mappings>
    

    You can try (as in your code):

    <logic:present name="user">
      <jsp:forward page="menu.do" />
    </logic:present>
    <logic:notPresent name="user">
      <jsp:forward page="login.do" />
    </logic:notPresent>
    
  • Using the JSP's paths

    <logic:present name="user">
      <jsp:forward page="/menu.jsp" />
    </logic:present>
    <logic:notPresent name="user">
      <jsp:forward page="/login.jsp" />
    </logic:notPresent>
    
  • Using logic:redirect

    <logic:present name="user">
      <logic:redirect page="/menu.jsp" />
    </logic:present>
    <logic:notPresent name="user">
      <logic:redirect page="/login.jsp" />
    </logic:notPresent>
    
  • Using global-forwards

    If you have in your struts-config.xml:

    <global-forwards>
      <forward name="login" path="/login.jsp"/>
      <forward name="menu" path="/menu.jsp"/>
    </global-forwards>
    

    You can try:

    <logic:present name="user">
      <logic:forward name="menu" />
    </logic:present>
    <logic:notPresent name="user">
      <logic:forward name="login" />
    </logic:notPresent>
    

Notes

  1. The following examples assume that your files are in the root folder of your context (e.g. in the WebContent folder if you are using an standar eclipse project). If you have your files in another path, for example, in the WEB-INF folder, you should just change it to that.

Upvotes: 4

Related Questions