aspose file tools*
The moose likes Struts and the fly likes How to use the ShowView Action? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to use the ShowView Action?" Watch "How to use the ShowView Action?" New topic
Author

How to use the ShowView Action?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I need to have a JSP displayed that's below the WEB-INF folder based on a "failure" action. I understand, now, that I need to front "forwards" to a JSP below the WEB-INF by an action.

I was told that the ShowView action is the way to do get to JSP pages below WEB-INF, but I can't find a decent example of how to use it.

Can somebody explain what I need to do or give me a simple example?

Thanks.

Mike
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I've never heard of a "ShowView" action. I just did a quick check of the javaDoc, and didn't find it there, either.

The best way to display a JSP without any processing beforehand is to create a "forward action mapping". This is an action mapping that does not require you to write an Action class. You just specify which JSP to forward to like this:

<action path="/myAction" forward="/WEB-INF/jsp/myPage.jsp">


Merrill
Consultant, Sima Solutions
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
When I do something like you're referring to, I get the error that the resource is not found (the JSP is under WEB-INF).

<action
. <!-- success action here -->
.
.
<forward name="failure"
redirect="true"
path="/WEB-INF/jsp/jspTest.jsp"/>
</action>

If I remove the redirect="true", it works with the current page (the current page displays the JSP text from the JSP page I want to forward to).

However, with the redirect="true" I get the message that the resource isn't found. The URL line and Tomcat error (JBOSS) all look correct.

So, my basic problem seems to be that when I return "failure" from an action class, I don't seem to know how to forward to a simple JSP page that's under WEB-INF.

Look foward to your reply.

Mike
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When you specify redirect="true", you're telling the forward to send a message back to the browser to redirect to the URL specified. Since your JSP is in /WEB-INF, it isn't directly accessible to the browser, so that's why you get the message. The solution: remove redirect="true" from the forward.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
But, I want to go to another JSP!

I understand that going to a page under WEB-INF means that you need to have the forward "fronted" by an action.

The thing is, I can't seem to figure out how to make that work.

Help!!!

Mike
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Mike,

If you specify the following, the jspTest.jsp will be displayed when you call the URL http://myserver.com/myApp/test.do. This is done without writing an Action class.

<action path="/test" forward="/WEB-INF/jsp/jspTest.jsp" />

Furthermore, if you specify the following forward with any other action, it will be successful:

<forward name="failure"
path="/WEB-INF/jsp/jspTest.jsp"/>


If you specify a forward without redirect=true, Struts uses the ServletContext's RequestDispatcher to forward the request to the specified JSP. The RequestDispatcher does have access to files inside the /WEB-INF directory, so it can display the JSP with no problems.

If you put redirect=true in a forward, it uses the sendRedirect() method on your HttpServletResponse object, which simply sends a message back to the browser to redirect to another URL. Since the /WEB-INF directory is protected from any access by the browser, you will get an error message if you try to redirect to any page under the /WEB_INF directory.

Is this making sense now?
[ May 05, 2006: Message edited by: Merrill Higginson ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If I remove the redirect="true", it works with the current page (the current page displays the JSP text from the JSP page I want to forward to).


Sorry, I didn't notice this statment before.

I think you are being confused by what is showing up in the URL. If you have an action (myAction.do) that can forward to either success.jsp or failure.jsp, the URL that you will see in your browser will still be http://myserver.com/myApp/myAction.do regardless of which page you forward to. The RequestDispatcher hides the forward and does not include it in the URL shown in the browser. However, it does successfully forward to the page.

After all, hiding the JSP file and making it inaccessible directly to the user is the whole reason for putting it inside the /WEB-INF directory in the first place.

If the above behavior is not what you want, the solution is simple: Take your JSPs out of the /WEB-INF directory. Then you can use redirect=true all you want, and the URL will always show which JSP is being displayed.
[ May 05, 2006: Message edited by: Merrill Higginson ]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
You're terrific!!!

THANK YOU!!!

Yes, it was the redirect=true that was messing me up.

Thanks again!!!

Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use the ShowView Action?