Use spring tag in XSLT - java

I have a XSL/XML parser to produce jsp/html code.
Using MVC model I need to accees spring library in order to perform i18n translation.
Thus, given the xml
<a>
...
<country>EN</country>
...
</a>
and using <spring:message code="table_country_code.EN"/> tag, choose based on the browser language, the transalation into England, Inglaterra, etc...
However, the XSL do not support <spring:message> tag.
The idea is to have a XSLT with something like this
<spring:message code="table_country_code.><xsl:value-of select="country"/>"/>`
to have the final code <spring:message code="table_country_code.EN"/> and be recognized in the final JSP/HTML based on i18n translation.
I also tried to create the spring tag in Java when I make a parse to create the XML but I sill have the same error.
The prefix "spring" for element "spring:message" is not bound.
[EDIT]
I saw some questions here, like using bean:spring but still have the same problem.
any pointers?

XSLT has to be namespace well formed XML, so you need to declare the namespace and you can not use < in attribute values.
Spring 3 - Accessing messages.properties in jsp
suggests the namespace should be
http://www.springframework.org/tags
so presumably you want an XSLT code of
<spring:message
xmlns:spring="http://www.springframework.org/tags"
code="table_country_code.{country}"
/>
where {} is an attribute value template that evaluates the XPath country

Related

JAVA DOM: duplicated attributes

I'm using the DOM library for JAVA and some entries XHTML encounter this problem:
[Fatal Error] tree.xml:238:185: Attribute "itemprop" was already specified for element "span".
This is the XHTML part with problems:
<span class='fn' itemprop='author' itemscope='itemscope' itemtype='http://schema.org/Person' itemprop='name'>Rodrigo</span>
Exists some option to allow duplicate attributes in DOM?
Thanks!
My understanding is that the Microdata specification only allows one itemprop per HTML element, meaning that the DOM library you're using is properly marking it as invalid markup. If you want to specify multiple values, they need to be space-separated, like this:
<span class='fn' itemprop='author name' itemscope='itemscope' itemtype='http://schema.org/Person'>Rodrigo</span>
Incidentally, the class attribute works the same way.

How to keep date data in a Struts form using html tags

I´m developing a Java EE application and I'm using Struts.
When I use html tags like
<input size="10px" id="start_date" name="start_date" type="text">
into my form, the ValidateError method (located in the FormBean class) doesn't maintain these dates after the error advise, returning and empty form.
I need to use this html tags rather than <forms:calendar...> (Struts tags) because we are using jQuery framework to improve the appearance.
Options :
You can use value=${param['start_date']} in input tag
You can still use struts tag and query element in jQuery using name or any other attribute.

How can I reuse code in JSP?

For example, a user will be rendered throughout my application as
<div class="user">
<p class="username">${user.name}</p>
<p class="karma">${user.karma}</p>
<img src="/users/${user.id}"/>
</div>
How can I reuse this code block?
Note - my code is running within a tag, so I can't use tags for this (or any JSP) otherwise I get a Scripting elements are disallowed here error.
Edit
I'm trying to use a tag file, but getting PropertyNotFoundException.
This is my tag file called 'user.tag':
<%#tag description="User" pageEncoding="UTF-8" %>
<a href="../user/showUser.do?userId=${user.id}">
<p>${user.name}</p>
<img class='avatar' src='${user.avatarUrl}' alt=""/>
</a>
And usage inside a jsp:
Where job.poster is a java bean with id, name, and avatarUrl properties.
If I add
<%#attribute name="user" %>
to the tag file, then I get an exception
Property 'id' not found on type java.lang.String
Since JSP 2.0, there is yet another kind of tags: Tag files. Tag files are JSP custom tags written as a JSP template itself, which seems to be what you want.
http://fforw.de/post/creating-jsp-layouts-with-page-tags/ shows how to use such a tag file as general layout solution. Using them as component should be even easier.
You should be able to use tag files within tag files; this works for me in a JSP 2.2 container:
<%-- mytag.tag --%>
<%#tag description="demo code" pageEncoding="UTF-8"%>
<%#taglib prefix="cust" tagdir="/WEB-INF/tags" %>
<%#attribute name="message"%>
<cust:mytag2 message="${message}" /><%-- uses mytag2.tag --%>
If that fails, you can use the include directive: <%#include file="/WEB-INF/jspf/fragment.jspf" %>
Note that the spec says about tags:
Directive Available? Interpretation/Restrictions
======================================================================
page no A tag file is not a page. The tag directive must
be used instead. If this directive is used in a
tag file, a translation error must result.
So, fragment.jspf must not have a any elements that are not supported in tags, including a page directive.
For the example you have given it sounds like some templating framework is needed, to display the user badge on each screen. At its simplest level this may just be a jsp:include which always includes your "UserBadge.jsp".
If you are running on a web framework e.g. JSF you may use Facelet templates or write a custom component for this. So the answer depends on what framework you have. Breaking it down to just JSP and JSTL - the included JSP or a javax.servlet.jsp.tagext.Tag would certainly reduce the duplication.
Always be careful to follow the DRY Principle... Don't Repeat Yourself!
I feel sometimes creating a .tag file is overkill (especially if it's only for one page), and I've wanted what you describe for years, so I wrote my own, simple solution. See here:
https://stackoverflow.com/a/25575120/1607642
Why don't you use a custom tag or jsp functions.
Thanks,

How do we get the absolute path to the applications root directory using Spring?

I have an app that may run at http://serverA/m/ or http://serverA/mobile/. I have a shared header with a search form that needs to go to http://serverA/installationName/search.
However, if I use <form action="/search"> it goes to the root of the server, not the tomcat application.
If I use <form action="search"> it goes to a path relative to the current page. (i.e http://serverA/m/someOtherPage/search
I've tried <c:url value="search"> and <c:url value="/search"> but neither of them seem to work.
In intelliJ, <c:url value="/search"> gives me "Cannot resolve controller URL /search" even though I have a controller defined with #RequestMapping("/search")
<form action="<c:url value="/search" />" />
Using <c:url> is the way. Ignore what the IDE tells you. They are not good at that. Just try to run it.
Bozho is right. I have used HTML BASE tag too:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
If you can put this tag in a few places (ideally in only one JSP) you can get your code cleaner.
You can (apart from other responders hints) also use Spring JSP tag (spring:url) which is modeled after the JSTL c:url tag (see Bozhos reply). The tld reference:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url
And the bottom of this mvc:resources block for an example use:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
you will not be able to imbed the c:url tag directly in the attribute, if your form tag is a jsp tag (perhaps, <sf:form>).
In that situation I do the following:
<c:url var="someName" value="some uri value"/>
<sf:form path="${someName}" ...>

Disable Struts converting HTML tags into entities

Hi I'm using Struts 1.2 and I installed TinyMCE. The problem is that struts is converting all HTML tags into entities. How do I disable or configure this to allow only selected tags?
Use the filter parameter (taglib).
<bean:write name="someField" filter="false"/>
Thanks, I was looking for this but in Struts 2, and this helped, it's similar:
<s:property value="variable" escape="false" />
I don't think that is a correct response for struts 2. At least on the later versions
escape property does not exist.
There are escapes for html, csv , javascript and xml which needs to be explicitly mentioned.
example for html, it has to be escapeHtml
<s:property value="%{somefield}" escapeHtml="false"/>

Categories