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
posted
0
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 ]
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
posted
0
Try this:
[ December 18, 2007: Message edited by: Merrill Higginson ]
Bushra Binte
Ranch Hand
Joined: Nov 07, 2006
Posts: 60
posted
0
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: 12612
posted
0
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
posted
0
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:
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
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 opinionated walrus
Marshal