• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Context Path from JSP Using No Java Code

 
James Lee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
James Lee
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

[ December 18, 2007: Message edited by: Merrill Higginson ]
 
Bushra Binte
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Can any tell me which tag lib is used.I mean what 's' is in <s:url> tag.thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That thread's over a year old... :/

The "s" prefix is normally used for the Struts 2 taglib.
 
Bushra Binte
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${pageContext.request.contextPath}
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that :)
 
Bushra Binte
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all for replies and time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic