Setting up Tiles with Spring for unprotected resources - spring

I am having problems setting up tiles with a spring mvc in an existing application. Here is my setup info:
All my jsps are under /project/* directory. I am trying to access
JSPs (help files etc.) via static links in other JSPs (like Tiles Test).
I do not have any controllers for these JSPs, and do not want to write controllers for this specific use case (I have tested that it works with controllers), so plz do not suggest writing controllers.
Here are my configurations:
spring-mvc-context.xml entries:
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/spring/tiles-def.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- <property name="prefix" value="/WEB-INF/views/"/> -->
<property name="suffix" value=".jsp"/>
</bean>
tiles-def.xml entry:
<definition name="tilesTest" template="/tilesTest.jsp">
<put-attribute name="title" value="TITLE for TEST-TILES JSP"/>
<put-attribute name="leftMenu" value="MY LEFT MENU"/>
<put-attribute name="body" value="SOME BODY"/>
</definition>
tilesTest.jsp located under /projectWarName/tilesTest.jsp:
<%# taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<html>
<head>Tiles Test Page</head>
<title><tiles:getAsString name="title"/></title>
<body>
<tiles:insertAttribute name="body"/>
</body>
</html>
I get the following error when i try to access my jsp (either through a link from another jsp, or directly via the browser). Can someone plz review my configuration and let me know what's wrong?
root cause
java.lang.NullPointerException
org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:42)
org.apache.tiles.template.GetAsStringModel.resolveAttribute(GetAsStringModel.java:178)
org.apache.tiles.template.GetAsStringModel.start(GetAsStringModel.java:95)
org.apache.tiles.jsp.taglib.GetAsStringTag.doTag(GetAsStringTag.java:306)
org.apache.jsp.tilesTest_jsp._jspx_meth_tiles_005fgetAsString_005f0(tilesTest_jsp.java:128)
org.apache.jsp.tilesTest_jsp._jspService(tilesTest_jsp.java:82)
Thanks

Related

spring mvc project not displaying home page after making changes to configuration files(root.xml,servlet-context.xml)

*HI all,
I am doing java project using spring mvc in STS(spring tool suite).
when I first created spring mvc project from existing template and ran it, it displayed home page like intended. but, after adding few classes to my project( dao classes, service classes, and few controller classes and few view files(.jsp files)) and changed **root.xml as follows***
**
</context:component-scan> -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/jobportal_db" />
<property name="username" value="root" />
<property name="password" value="pswd" />
</bean>**
and servlet-context.xml as follow:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.practice.jobportal.controller" />
its not displaying home page anymore, instead giving me
HTTP Status 404 - /jobportal/ as output.

Spring internationalization

Iam working on a small project, when i try to implement i18n in spring,it not working,even when i try to change default language its not working. here is my spring-servlet.xml code
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="fr" />
<!-- <property name="cookieName" value="myAppLocaleCookie"></property>
<property name="cookieMaxAge" value="3600"></property>-->
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
i have 4 messages.properties in classpath messages.properties,messages_en.properties,messages_fr.properties,messages_de.properties
the default language not changing always its using messages_en.properties if remove messages_en.properties file ,then its using messages.properties.
And my hyperlinks are not working in jsp file
Language : English | French | german
when i use ${locale} in jsp it prints nothing..
plz help me tia..
Try ${requestContext.locale} instead of ${locale}.
And maybe jsp you test, has already '?' character in path, so lands on page with 2x'?' ?

Spring webflow view resolver config: subfolders for JSP Files

Using Spring webflow and in the serlvet-configuration I have this for JSP files:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
and this for flow XML files:
<webflow:flow-registry id="flowRegistry"
flow-builder-services="flowBuilderServices">
<webflow:flow-location-pattern value="/WEB-INF/flows/**/*-flow.xml" />
</webflow:flow-registry>
Due to the /**/, any flow XML file in may they be in sub directory or not is resolved automatically so I need not specify the subfolder in the flow definition.
For JSP ** does seem not work.
Is there a way to do the same? I'd like to use subdirectories, because it makes it look cleaner when don't have to scroll through a heap of JSP files in the package explorer. On the other hand, If I have to specify the subdirectory each time, it's probably prone to getting typos not being detected
I don't think there is a way to specify ant style wildcard in 'prefix' or 'suffix' attribute.
As you said, you can specify the path in the flow definition as below
<view-state id="showSearchCriteria" view="customer/search">
<transition on="lookupCriteriaEntered" to="searchCustomer" />
</view-state>
Though not an elegant solution but you can try jsps in multiple subfolders of jsp folder using tiles as:
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tilesDef.xml</value>
</list>
</property>
</bean>
Define your tilesDef.xml as:
<tiles-definitions>
<definition name="page1" template="/WEB-INF/jsp/subFolder1/page1.jsp"/>
<definition name="page2" template="/WEB-INF/jsp/subFolder2/page2.jsp"/>
</tiles-definitions>
In your controller return as:
new ModelAndView("page1");
or as:
new ModelAndView("page2");
But the drawback of this would be that you cannot have 2 jsps with same name in different folders as the definition name is unique.

Internationalization using Spring ResourceBundleMessageSource and FMT

We are trying to implement internationalization using Spring ResourceBundleMessageSource and FMT. But when we use it in the JSP, pages are displaying value as ???message.key???. Can you please help us in resolving this behavior? Really appreciate your answers.
Below are the configuration:
spring-servlet.xml entry
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>WEB-INF/messages/msgs</value>
</property> </bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName">
<value>locale</value>
</property> </bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
IN the JSP we have added the imported
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:message key="message.key"/>
Also in the war created we have the msgs.properties file under WEB-INF/messages folder.
Really appreciate help in identifying the mistake we are making. Thank you.
I am using the same as you and I am able to retrieve the messages from the properties file with fmt:message. Can you try to change the resource bundle for this:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="fallbackToSystemLocale" value="false"></property>
</bean>
The properties are stored in src/main/resources.
Hope it helps.
Using FMT taglibraries we could not solve the issue. I believe it has got some thing to do with the jstl jar and taglibs which we are using with JBoss 7.1.1 server.
We started using spring tlds for displaying the messages. All is working fine now. Thanks for the help.
Use /WEB-INF/messages/msgs instead of WEB-INF/messages/msgs
Simply, add '/' at the beginning of path.

Internationalization (i18n) working, but accented characters aren't accented

I have a Spring MVC J2EE application that leverages Spring's localeChangeInterceptor and CookieLocaleResolver to render locale-driven support. This is working, however when I try to encode letters with accent marks, the front-end fails to render them as expected.
Here's a snippet from my webmvc-config.xml file:
<!-- Internalization and localization support -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
Along with this I have some message_xx.properties files that contian my tags to render things out. Included are tags like this with accent marks embedded: district.manager.approval=Aprobación del Gerente de Distrito. My beef is that this displays exactly like this on the front-end instead of showing me Aprobación del Gerente de Distrito.
Any idea where I could have gone wrong?
The encoding of .properties files is usually (with few exceptions) expected to be Latin-1. Therefore for presenting Unicode content you need to escape the characters beyond Latin-1 repertoire:
district.manager.approval=Aprobaci\u00F3n del Gerente de Distrito
Or use XML properties files which can be encoded as UTF8.
After poking around a bit, it seems I left out a key detail: this only seems to be happening when I use JSTL <c:set> along with my Spring tags where the encoding does not work properly. As it turns out, when using <c:out>, you need to accompany it with the escapeXml="false" attribute. Here is what I did and it seems to be working appropriately now:
This is set in one page
<c:set var="headerScreenTitle">
<spring:message code='district.manager.review.and.approval' />
</c:set>
This is consumed in an imported page
<c:out value="${headerScreenTitle}" escapeXml="false" />
And it handsomely gives me this:
REVISIÓN Y APROBACIÓN DEL GERENTE DE DISTRITO
Thanks everyone for your responses!
If you use html entities (e.g. &) and use <spring:message code="property.name" /> tag to print values set "htmlEscape" attribute to "false":
<spring:message code="property.name" htmlEscape="false" />

Resources