You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Max Pimm <ma...@alwayssunny.com> on 2007/06/21 14:19:58 UTC

s2: Base url in jsp's

I am used to defining a base element in my pages but can't find the way 
to define this with struts 2. All suggestions welcome.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: s2: Base url in jsp's

Posted by Zarar Siddiqi <za...@gmail.com>.
If you're just trying to get the context path, you can just use
${pageContext.request.contextPath}.  That way you can just write:

<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/css/myStyles.css" />

If you're using Freemarker, its even easier, just use ${base} like this:

<link rel="stylesheet" type="text/css" href="${base}/css/myStyles.css" />

You can't just specify "css/myStyles.css" as the value of the href tag as
that will be interpreted as a relative path.  Actually, you could even do
that, just make sure all your actions are only one level deep (can't use
/user/blah.action etc.)

If you find a better way, do post the solution. I'm curious.

Zarar



maxmil wrote:
> 
> Thanks for the respones.
> 
> Perhaps i didn't explain myself properly. What i'm really looking for is 
> a way to define the context root url as the base url.
> 
> My motives for doing this is to allow me to share code across my 
> application. An example would be referencing a style sheet in a header 
> include. For examples i could use the line
> 
> <link rel="stylesheet" type="text/css" href="css/myStyles.css" />
> 
> in any jsp, irrespective of the request url that i am calling it from.
> 
> Up till now the only solution that i've found is to generate all href 
> values using a combination of s:url and s:properties, like so.
> 
> <s:url id="myStylesUrl" value="/css/myStyles.css" />
> <link rel="stylesheet" type="text/css" href="<s:property 
> name="myStylesUrl" />" />
> 
> This generates a context relative url and works fine but is a bit 
> cumbersome if i have to do it every time i need to generate a url 
> outside struts tags.
> 
> max
> 
> 
> This works fine but h 
> 
> Musachy Barroso wrote:
>> Or the scriptletless version:
>>
>> <base id="docBase" href='<s:property value="%{#context[@
>> org.apache.struts2.StrutsStatics@HTTP_REQUEST].requestURL}"/>' >
>>
>> musachy
>>
>> On 6/21/07, Van Riper <va...@gmail.com> wrote:
>>>
>>> On 6/21/07, Max Pimm <ma...@alwayssunny.com> wrote:
>>> > I am used to defining a base element in my pages but can't find the 
>>> way
>>> > to define this with struts 2. All suggestions welcome.
>>>
>>> I don't think you need struts 2 support to do this. I'm using webwork
>>> 2.2 currently and starting to monitor this list again in preparation
>>> for migrating our webapp codebase to struts 2. We also use Sitemesh
>>> for response decoration. So, our Sitemesh decorator sets the base
>>> element for our response during the execution of sitemesh response
>>> filter. The way we do it in JSP land is:
>>>
>>> <base id="docBase" href="<%=request.getRequestURL()%>"/>
>>>
>>> No webapp framework support required. We give the base element an "id"
>>> because we have some javascript functions that need to access the base
>>> document  URL dynamically. This makes it easy to do the element lookup
>>> by id via Javascript elsewhere.
>>>
>>> Cheers, Van
>>>
>>> Mike Van Riper
>>> van_riper@dev.java.net
>>>
>>> Silicon Valley Web Developer JUG
>>> https://sv-web-jug.dev.java.net
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/s2%3A-Base-url-in-jsp%27s-tf3958382.html#a11279336
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: s2: Base url in jsp's

Posted by Roberto Nunnari <ro...@supsi.ch>.
Why don't you use JSP EL?

<c:url var="stylesheetURL" value="/css/stile.css"/>
<link href="${stylesheetURL}" rel="stylesheet" type="text/css" />

Best regards.

--
Robi



Max Pimm wrote:
> Thanks for the respones.
> 
> Perhaps i didn't explain myself properly. What i'm really looking for is 
> a way to define the context root url as the base url.
> 
> My motives for doing this is to allow me to share code across my 
> application. An example would be referencing a style sheet in a header 
> include. For examples i could use the line
> 
> <link rel="stylesheet" type="text/css" href="css/myStyles.css" />
> 
> in any jsp, irrespective of the request url that i am calling it from.
> 
> Up till now the only solution that i've found is to generate all href 
> values using a combination of s:url and s:properties, like so.
> 
> <s:url id="myStylesUrl" value="/css/myStyles.css" />
> <link rel="stylesheet" type="text/css" href="<s:property 
> name="myStylesUrl" />" />
> 
> This generates a context relative url and works fine but is a bit 
> cumbersome if i have to do it every time i need to generate a url 
> outside struts tags.
> 
> max
> 
> 
> This works fine but h
> Musachy Barroso wrote:
>> Or the scriptletless version:
>>
>> <base id="docBase" href='<s:property value="%{#context[@
>> org.apache.struts2.StrutsStatics@HTTP_REQUEST].requestURL}"/>' >
>>
>> musachy
>>
>> On 6/21/07, Van Riper <va...@gmail.com> wrote:
>>>
>>> On 6/21/07, Max Pimm <ma...@alwayssunny.com> wrote:
>>> > I am used to defining a base element in my pages but can't find the 
>>> way
>>> > to define this with struts 2. All suggestions welcome.
>>>
>>> I don't think you need struts 2 support to do this. I'm using webwork
>>> 2.2 currently and starting to monitor this list again in preparation
>>> for migrating our webapp codebase to struts 2. We also use Sitemesh
>>> for response decoration. So, our Sitemesh decorator sets the base
>>> element for our response during the execution of sitemesh response
>>> filter. The way we do it in JSP land is:
>>>
>>> <base id="docBase" href="<%=request.getRequestURL()%>"/>
>>>
>>> No webapp framework support required. We give the base element an "id"
>>> because we have some javascript functions that need to access the base
>>> document  URL dynamically. This makes it easy to do the element lookup
>>> by id via Javascript elsewhere.
>>>
>>> Cheers, Van
>>>
>>> Mike Van Riper
>>> van_riper@dev.java.net
>>>
>>> Silicon Valley Web Developer JUG
>>> https://sv-web-jug.dev.java.net


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: s2: Base url in jsp's

Posted by Max Pimm <ma...@alwayssunny.com>.
Correction... the last two lines of code in my previous mail should have 
been (getting myStylesUrl by value not name!)

<s:url id="myStylesUrl" value="/css/myStyles.css" />
<link rel="stylesheet" type="text/css" href="<s:property 
value="myStylesUrl" />" />

not

<s:url id="myStylesUrl" value="/css/myStyles.css" />
<link rel="stylesheet" type="text/css" href="<s:property 
name="myStylesUrl" />" />

Max Pimm wrote:
> Thanks for the respones.
>
> Perhaps i didn't explain myself properly. What i'm really looking for 
> is a way to define the context root url as the base url.
>
> My motives for doing this is to allow me to share code across my 
> application. An example would be referencing a style sheet in a header 
> include. For examples i could use the line
>
> <link rel="stylesheet" type="text/css" href="css/myStyles.css" />
>
> in any jsp, irrespective of the request url that i am calling it from.
>
> Up till now the only solution that i've found is to generate all href 
> values using a combination of s:url and s:properties, like so.
>
> <s:url id="myStylesUrl" value="/css/myStyles.css" />
> <link rel="stylesheet" type="text/css" href="<s:property 
> name="myStylesUrl" />" />
>
> This generates a context relative url and works fine but is a bit 
> cumbersome if i have to do it every time i need to generate a url 
> outside struts tags.
>
> max
>
>
> This works fine but h
> Musachy Barroso wrote:
>> Or the scriptletless version:
>>
>> <base id="docBase" href='<s:property value="%{#context[@
>> org.apache.struts2.StrutsStatics@HTTP_REQUEST].requestURL}"/>' >
>>
>> musachy
>>
>> On 6/21/07, Van Riper <va...@gmail.com> wrote:
>>>
>>> On 6/21/07, Max Pimm <ma...@alwayssunny.com> wrote:
>>> > I am used to defining a base element in my pages but can't find 
>>> the way
>>> > to define this with struts 2. All suggestions welcome.
>>>
>>> I don't think you need struts 2 support to do this. I'm using webwork
>>> 2.2 currently and starting to monitor this list again in preparation
>>> for migrating our webapp codebase to struts 2. We also use Sitemesh
>>> for response decoration. So, our Sitemesh decorator sets the base
>>> element for our response during the execution of sitemesh response
>>> filter. The way we do it in JSP land is:
>>>
>>> <base id="docBase" href="<%=request.getRequestURL()%>"/>
>>>
>>> No webapp framework support required. We give the base element an "id"
>>> because we have some javascript functions that need to access the base
>>> document  URL dynamically. This makes it easy to do the element lookup
>>> by id via Javascript elsewhere.
>>>
>>> Cheers, Van
>>>
>>> Mike Van Riper
>>> van_riper@dev.java.net
>>>
>>> Silicon Valley Web Developer JUG
>>> https://sv-web-jug.dev.java.net
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: s2: Base url in jsp's

Posted by Max Pimm <ma...@alwayssunny.com>.
Thanks for the respones.

Perhaps i didn't explain myself properly. What i'm really looking for is 
a way to define the context root url as the base url.

My motives for doing this is to allow me to share code across my 
application. An example would be referencing a style sheet in a header 
include. For examples i could use the line

<link rel="stylesheet" type="text/css" href="css/myStyles.css" />

in any jsp, irrespective of the request url that i am calling it from.

Up till now the only solution that i've found is to generate all href 
values using a combination of s:url and s:properties, like so.

<s:url id="myStylesUrl" value="/css/myStyles.css" />
<link rel="stylesheet" type="text/css" href="<s:property 
name="myStylesUrl" />" />

This generates a context relative url and works fine but is a bit 
cumbersome if i have to do it every time i need to generate a url 
outside struts tags.

max


This works fine but h 

Musachy Barroso wrote:
> Or the scriptletless version:
>
> <base id="docBase" href='<s:property value="%{#context[@
> org.apache.struts2.StrutsStatics@HTTP_REQUEST].requestURL}"/>' >
>
> musachy
>
> On 6/21/07, Van Riper <va...@gmail.com> wrote:
>>
>> On 6/21/07, Max Pimm <ma...@alwayssunny.com> wrote:
>> > I am used to defining a base element in my pages but can't find the 
>> way
>> > to define this with struts 2. All suggestions welcome.
>>
>> I don't think you need struts 2 support to do this. I'm using webwork
>> 2.2 currently and starting to monitor this list again in preparation
>> for migrating our webapp codebase to struts 2. We also use Sitemesh
>> for response decoration. So, our Sitemesh decorator sets the base
>> element for our response during the execution of sitemesh response
>> filter. The way we do it in JSP land is:
>>
>> <base id="docBase" href="<%=request.getRequestURL()%>"/>
>>
>> No webapp framework support required. We give the base element an "id"
>> because we have some javascript functions that need to access the base
>> document  URL dynamically. This makes it easy to do the element lookup
>> by id via Javascript elsewhere.
>>
>> Cheers, Van
>>
>> Mike Van Riper
>> van_riper@dev.java.net
>>
>> Silicon Valley Web Developer JUG
>> https://sv-web-jug.dev.java.net
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: s2: Base url in jsp's

Posted by Musachy Barroso <mu...@gmail.com>.
Or the scriptletless version:

<base id="docBase" href='<s:property value="%{#context[@
org.apache.struts2.StrutsStatics@HTTP_REQUEST].requestURL}"/>' >

musachy

On 6/21/07, Van Riper <va...@gmail.com> wrote:
>
> On 6/21/07, Max Pimm <ma...@alwayssunny.com> wrote:
> > I am used to defining a base element in my pages but can't find the way
> > to define this with struts 2. All suggestions welcome.
>
> I don't think you need struts 2 support to do this. I'm using webwork
> 2.2 currently and starting to monitor this list again in preparation
> for migrating our webapp codebase to struts 2. We also use Sitemesh
> for response decoration. So, our Sitemesh decorator sets the base
> element for our response during the execution of sitemesh response
> filter. The way we do it in JSP land is:
>
> <base id="docBase" href="<%=request.getRequestURL()%>"/>
>
> No webapp framework support required. We give the base element an "id"
> because we have some javascript functions that need to access the base
> document  URL dynamically. This makes it easy to do the element lookup
> by id via Javascript elsewhere.
>
> Cheers, Van
>
> Mike Van Riper
> van_riper@dev.java.net
>
> Silicon Valley Web Developer JUG
> https://sv-web-jug.dev.java.net
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

Re: s2: Base url in jsp's

Posted by Van Riper <va...@gmail.com>.
On 6/21/07, Max Pimm <ma...@alwayssunny.com> wrote:
> I am used to defining a base element in my pages but can't find the way
> to define this with struts 2. All suggestions welcome.

I don't think you need struts 2 support to do this. I'm using webwork
2.2 currently and starting to monitor this list again in preparation
for migrating our webapp codebase to struts 2. We also use Sitemesh
for response decoration. So, our Sitemesh decorator sets the base
element for our response during the execution of sitemesh response
filter. The way we do it in JSP land is:

<base id="docBase" href="<%=request.getRequestURL()%>"/>

No webapp framework support required. We give the base element an "id"
because we have some javascript functions that need to access the base
document  URL dynamically. This makes it easy to do the element lookup
by id via Javascript elsewhere.

Cheers, Van

Mike Van Riper
van_riper@dev.java.net

Silicon Valley Web Developer JUG
https://sv-web-jug.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org