GeeCON Prague 2014*
The moose likes Struts and the fly likes Get Context Path from JSP Using No Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Get Context Path from JSP Using No Java Code" Watch "Get Context Path from JSP Using No Java Code" New topic
Author

Get Context Path from JSP Using No Java Code

James Lee
Ranch Hand

Joined: Oct 16, 2006
Posts: 36
Hi all,

What's a better way to get context path in view (JSP)? We know we can use request.getContextPath() but putting Java code in view is not recommended so is there a tag available to perform that task?

Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you use the Struts html:xxx tags, you don't have to worry about the context path because Struts automatically includes it. Example:

translates the link to:
/myapp/jsp/mpage.jsp

You can also generate a URL variable that includes the context path using the c:url tag. Once you have the URL in a variable, you can then reference it with a cut tag.
[ December 17, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
James Lee
Ranch Hand

Joined: Oct 16, 2006
Posts: 36
Thanks Merrill.

Output of <s:url value="/..."/> is also prefixed by context path.

The problem with <s:url .../> is request parameters are also appended to the output. Here is the detail:

Action: LoginAction.java
The result (view) of the action is login.jsp
In login.jsp, there is one line code: <s:url value="/css/mycss.css"/>

Source of the page generated by .../LoginAction.action?lang=en is:
/<context path>/css/mycss.css?lang=en

Could anyone explain why? How to let output not include ?lang=en

Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Try this:

[ December 18, 2007: Message edited by: Merrill Higginson ]
Bushra Binte
Ranch Hand

Joined: Nov 07, 2006
Posts: 60
Hi,Can any tell me which tag lib is used.I mean what 's' is in <s:url> tag.thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That thread's over a year old... :/

The "s" prefix is normally used for the Struts 2 taglib.
Bushra Binte
Ranch Hand

Joined: Nov 07, 2006
Posts: 60
yeah,i know that the thread is very old.I am also facing similiar problem.

the problem: In the login page i try to include the css file which is not in the same directory as the login.jsp(It is direct under the root directory of the application).so in the jsp i put css inclusion as below:

&lt;link rel="stylesheet" type="text/css" href="/resources/mycss.css"/&gt;

It is not included.but when i move the mycss.css file to the root directory of the application,i see the styles applied on the page.I dont wont to use a scriptlet . liek the stuff like this in the login.jsp

&lt;% String contextPath = request.getContextPath();%&gt;

&lt;link rel="stylesheet" type="text/css" href="${contextPath}/resources/mycss.css"/&gt;

Could you help in this please
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

First off, I believe you could just use ${request.contextPath}. The tag-based way to do it would be to construct a URL: <link rel... type... href="<s:url value="/"/>/resources/mycss.css"/> (or use JSTL; really doesn't matter in this case I think).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

${pageContext.request.contextPath}


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yeah, that :)
Bushra Binte
Ranch Hand

Joined: Nov 07, 2006
Posts: 60
thanks all for replies and time.
 
GeeCON Prague 2014
 
subject: Get Context Path from JSP Using No Java Code