i18n of URL in a java project - url

I am trying to internationalise the urls of my web application. Basically I want to be able to set the url of, let's say, a link to www.mysite.com/france/inscription when the preferred language is fr and www.mysite.com/signup when the preferred language is en.
Until now I have beenusing a jtsl choose to manually set the url:
<c:choose>
<c:when test="${pageContext.request.locale.language eq 'fr'}">
<a class="trialButton" href="<c:url value="/france/inscription?type=0"/>"><fmt:message key='features_trial'/></a>
</c:when>
<c:otherwise>
<a class="trialButton" href="<c:url value="/subscribe?type=0"/>"><fmt:message key='features_trial'/></a>
</c:otherwise>
</c:choose>
Ideally I would like to be able to get a localised url directly from my resource bundle, I tried to do:
<a class="trialButton" href="<c:url value=<fmt:message key='features_trial_url'/>/>"><fmt:message key='features_trial'/></a>
But eclipse doesn't accept it.
Do you know a way to do something like that?

You can't use a JSP tag inside an attribute of another JSP tag. But all JSTL tags printing a value have an attribute var allowing to store the value inside an attribute instead of printing it. So you can simply use
<fmt:message key='features_trial_url' var"theUrl"/>
<a class="trialButton" href="<c:url value='${theUrl}'>"><fmt:message key='features_trial'/></a>

Related

Correctly handling s:url tag with parameters -security implications?

In my .jsp page I have
<s:url id="ecu" value="ecu-info-detail">
<s:param name="id" value="%{id}"></s:param>
<s:param name="ecuName" value="name"></s:param>
<s:param name="ajax" value="true"></s:param>
</s:url>
which I use in a sj:a tag thus;
<li class="vMenuItem"><sj:a href="%{ecu}" requestType="GET" targets="detail"><s:property value="description"/></sj:a></li>
when this link is clicked the url generated is in the format;
my.domain.com?id="01"&ecuName="foo"&ajax="true"
so the parameters ecuName and ajax do not get set on the action. Now I can get around this by simply adding the attribute escapeAmp="true" to the s:url definition. However, because the Struts 2 default is to escape the ampersand and because I cannot be the only person using multiple s:param tags, I am concerned that escapeAmp="true" may not be the correct way of handling this situation and I'm creating a security hole.
Is this the correct way to deal with this situation, and if not, how should I deal with it?
Regards
Posting answer as per the discussion in the comment section.Same issue was reported in the plugin development team url paramerters escaped.
Same issue was closed with a discussion that we need to escape that html in order for this to work with Jquery tags.For more details please read the filed bug ticket.

How to have multiple links from one image in rails

I have one image from which I have to have 7 different links based on the part of the image clicked. How do I go about it using rails?
For future reference, you're talking about "image maps" - there's various tutorials for these, eg
http://www.onextrapixel.com/2009/04/30/how-to-create-multiple-links-on-a-single-image-with-image-map/
As far as i know this isn't really a rails question, as image maps are a totally front-end thing: like js, they only relate to the page as seen in the browser. As far as rails is concerned, when you click on a link it doesn't care whether that link was on an image map or not.
So, it's just some raw html that you would put in your view template.
I suppose you could make the image maps data-driven though, so that for example you store an image filename in the db and then save any map data in the db too, in an image_maps table. It's only worth doing this if you want to make the image maps user-editable, like flicker does for example. (flickr lets you draw a rectangle on an existing photo and tag it with some text)
just use url_for to generate the needed url
<img usemap="image_link" src="..." alt="" width="235" height="32" style="display: block;" />
<map name="image_link">
<area href="<%= url_for root_path %>" alt="Home" coords="0,0,189,19" shape="rect" />
<area href="<%= url_for :controller => "post", :action => "all" %>" alt="All posts" coords="190,0,386,22" shape="rect" />
<area href="<%= url_for new_resource1_path %>" alt="New resource1" coords="387,0,657,23" shape="rect" />
</map>
the root_path is defined as the home page and resource1 as a resource in config/routes.rb.
This is more of a javascript/flash type thing. You can absolutely position divs with links if you know exactly where the links need to go and you are not looking for an area for the user to click because this will just generate a link with text in a specific position.
Sounds to mee like you will need to make a flash app or do javascript workaround. This is not a Rails type issue so your solution won't come from Ruby/Rails.

Creating a controller / action href

While Grails has several tags to generate either resource links or anchor tags, what about a plain href? Say I'm coding some AJAX functions on my own and I want to say something like
$.post("<g:href controller='mycontroller' action='update' />');
I think createLink is what you're looking for:
<a href="${createLink(controller:'mycontroller', action:'update')}" />

How can I get a localized message in a grails GSP tag attribute?

In a grails GSP, I want a toolTip to be localized:
<a href="..." title="localizedMessage">
As the toolTip is written in the attribute title, I can't use <g:message> here. Actually I already have a service for localized messages, but when I call it from the GSP, the service's messageSource is null, so getting that to work would be a solution as well.
You can invoke the tag directly:
<a href="..." title="${message(code:'your.localized.message.code')}">
I think this should work too:
<a href="..." title="${g.message(code:'your.localized.message.code')}">

Creating navigation links in Struts 2

Is there some way to replicate rails' "link-to-unless-current"?
Ie. if i have a list of links in my navigation, and I want to mark the current page with a different css style (or even have it be plain text, not a link)?
Now I just made a method in my action-class (getPage()) which returns a name that I assign for each action, and test for that when building my navigation... Works but not pretty, and I have to manually set the page name (couldn't struts somehow automaticalyy get it from the context).
Using Struts 2, Tiles, JSP + Struts taglibs.
To get the current url you you can use the Url tag without any params, actions, etc:
<s:url var="currenturl" includeParams="get" escapeAmp="false"/>
Then assuming you construct your navigation Url something like:
<s:url var="url" action="someAction" escapeAmp="false">
<s:param name="id" value="id"/>
</s:url>
Then you can then use a normal if tag to test whether the current url matches on that you've constructed.
<s:if test="#url eq #currenturl">
</s:if>
For example, to change the class, you can do a conditional inline to your anchor definition:
class="current"</s:if>>XXX
Use the includeParams="get" if the parameters in your URL are meaningful for navigation otherwise exclude that and use an if like:
<s:if test="#url.startsWith(#currenturl)">

Resources