JSTL fmt:message and resource bundle - java

I want to set the "dir" property of my table from resource bundle based on the locale.
Here is snippet:
<fmt:setBundle basename="class.path.to.resource.bundle"/>
<table align=center class="" dir=<fmt:message key="registration.direction"/>>
When the page renders I get this:
<table align=center dir=???registration.direction???>
I have two resource bundles for english and arabic.
registration.direction = ltr -> English
registration.direction = rtl -> Arabic
Please tell what I am doing wrong? The dir should have "ltr" or "rtl" depending on the locale.
Thanks
BR
SC

two things
1) I would add a variable to store the message result in
<fmt:message key="registration.direction" var="direction" />
then
2) I would do the following with your code
<fmt:setBundle basename="class.path.to.resource.bundle"/>
<fmt:message key="registration.direction" var="direction" />
<table align=center class="" dir="${direction}">
Now as far as your resource bundles, typically You should have the following structure for your resource bundles
/foo/bar/MyResourceBundle.properties
/foo/bar/MyResourceBundle_en.properties
/foo/bar/MyResourceBundle_en_US.properties
/foo/bar/MyResourceBundle_<lang>[_COUNTRY[_VAR]].properties
If your bundle is not structured in this fashion that might be some of your problem.
Make sure that all keys that are expected to be available are defined in MyResourceBundle with reasonable defaults.
I'm amending this answer as I'm not sure if my comment got lost in a hide function.
With the fact that you are using Struts 2, I'm under the impression that you're using the i18n interceptor. The interceptor will store the current locale in the sesion variable named WW_TRANS_I18N_LOCALE. As such you should be able to get to it and set the locale for the JSTL tags by using the following:
<fmt:setLocale scope="session" value="${sessionScope.WW_TRANS_I18N_LOCALE}" />
Hope that works for you.

Related

jTidy pretty print custom HTML tag

I'm trying to use JTidy to pretty print a well formed HTML generated by the user:
<div class="component-holder ng-binding ng-scope ui-draggable ui-draggable-handle" data-component="cronos-datasource" id="cronos-datasource-817277">
<datasource name="" entity="" key="" endpoint="" rows-per-page="">
<i class="cpn cpn-datasource"></i>
</datasource>
</div>
This is my config:
Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
tidy.setWraplen(2000);
tidy.setDropProprietaryAttributes(false);
tidy.setDropEmptyParas(false);
tidy.setTrimEmptyElements(false);
But jTidy is removing my AngularJS datasource directive. Is there a way to workarround this issue?
I'm getting this from the log:
line 1 column 191 - Error: <datasource> is not recognized!
line 1 column 191 - Warning: discarding unexpected <datasource>
Removing tidy.setXHTML(true) or setting it to false and adding tidy.setXmlTags(true) actually solve this issue and it start to consider user defined tags, but this is not a good solution because JTidy starts trying to close self enclosing tags.
<!-- this code -->
<img src="anythig.jpg"/>
<div id="anyid"></div>
<!-- will become -->
<img src="anythig.jpg">
<div id="anyid"></div>
</img>
I need a formatter for a text editor. I can't assure what directives our users will define and use. It must be a generic solution which works for any user defined directive
Try setting the following property after your current configuration:
Properties props = new Properties();
props.setProperty("new-blocklevel-tags", "datasource");
tidy.getConfiguration().addProps(props);
See http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags.
I have solved this problem by make some changes in JTidy source
https://github.com/nanndoj/jtidy
I have added a new configuration called dropProprietaryTags
tidy.setDropProprietaryTags(false);
Now it's working fine for me. It's set to true by default so JTidy can work in the old way if the new property is not set to false

JSTL Message Bundle with Localized Parameter in Placeholder

I'm using Spring and JSTL.
I have the following keys in a bundle (messages.properties):
key.withplaceholder= Never {0}.
key.giveup=give up
And I have the following code:
<fmt:message key="key.withplaceholder">
<fmt:param value="lie"/>
</fmt:message>
With this code I can show the message:
Never lie.
But if I want to use the key.giveup to show "Never give up" I don't know how to do it. How can I accomplish this?
Finally, is there a better way to do it? (without having to load a resource bundle in the jsp).
You can use the var attribute to store the formatted message in a page scoped variable instead of printing it out to the response.
<fmt:message key="key.giveup" var="key" />
<fmt:message key="key.withplaceholder">
<fmt:param value="${key}" />
</fmt:message>

Struts2 Tags: Fielderrors tag

I am currently using Struts2 tags for my form, and to show its error messages. My question is that the default markup for showing error messages in Struts2 tags is the usage of <ul> tag. is there anyway I can change this? I want the error messages to be displayed as <span> not a list.
How would I achieve this?
Another option is to change the CSS for UL elements.
This approach works only if you specifically care about appearance, not the DOM itself.
You can override template files which are used for rendering errors. Copy actionerror.ftl and fielderror.ftl files from the simple theme from struts2-core jar to your application and modify them not to use ul/li tags.
The tags render according to their theme. The question then changes to: How do you change the theme? You can change it for the tag
(set theme attribute on the tag to simple), page, request, or generally.
http://struts.apache.org/2.2.1/docs/struts-2-themes.html
Personally I like writing html, that is I don't like any "help" from the struts2 default theme. So in my struts.xml I simply use:
<constant name="struts.ui.theme" value="simple" />
Web developers should know html.
Update:
Generally use YUI reset.css so I probably missed this...
If you extend ActionSupport on the action there is a getFieldErrors() method so you could use <s:property value='fieldError["field_name"]'/> that will return the associated error message string of course without any formatting.
It isn't much less readable than the <s:fielderror/> tag... after all we need to use property tags all the time anyways.
I had same issue I used following code to resolve my issue
<s:if test="fieldErrors.get('email').size() > 0">
<s:property value="fieldErrors.get('email').get(0)"/>
</s:if>
Where email is name of my field. This way we don't have to modify CSS.
Here is a tutorial to show the use of the Struts 2â€ēs ActionError and ActionMessage class.
http://www.mkyong.com/struts2/struts-2-actionerror-actionmessage-example/
ActionError – is used to send error feedback message to user – display via < s:actionerror/ >
<s:if test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:if>
ActionMessage – is used to send information feedback message to user,display via < s:actionmessage/ >
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage/>
</div>
</s:if>

JSTL Core fmt:message Tag Dynamic message using bundle/properties file

I understand the usage of standard fmt:message Tag i.e we define something like this in the JSP:
<fmt:setBundle basename="ResourceBundles.ValidationErrorMessages" var="errorMessages" />
<fmt:message key="${error.value}" bundle="${errorMessages}" />
Suppose error.Value = "MQ2009"
My properties file named "ValidationErrorMessages" has following entry
MQ2009 = MQ time out
Now my requirement is to have something like
MQ2009 = Mq timeout happened for message {messagename}.
Can I define the messagename variable dynamically? I.e at runtime, messagename will be available in request scope and it should be substituted in the properties file.
How can I do this? Do I need a custom tag or does Java EE provides this feature which I am not aware off?
You can define properties like
MQ2009 = Mq timeout happened for {0}
and then
<fmt:message key="MQ2009" var="val" >
<fmt:param value="${valueComingFromSomeParameter}"/>
</fmt:message>
and then
<c:out value="${val}"/>
In you properties file
MQ2009 = Mq timeout happened for message {0}
Then you can nest a <fmt:param value="${messagename}"/> tag withing <fmt:message tag, where messagename is in request scope.

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}" ...>

Categories