Resolving a view that is outside the application container - java

I'm building a Java Spring app, and I'm working around a few constraints in the production environment, due to the fact that content is being published from a CMS. I have my images, js, css and jsp views residing in a static folder. I'm running a Tomcat app server, and i've set up Virtual directory mappings in the server.xml to map the images, css and js, which is working:
<Context debug="1" docBase="/home/content/images" path="/images" reloadable="true"/>
<Context debug="1" docBase="/home/content/css" path="/css" reloadable="true"/>
<Context debug="1" docBase="/home/content/js" path="/js" reloadable="true"/>
<Context debug="1" docBase="/home/content/view" path="/view" reloadable="true"/>
I've run into a problem resolving my jsp views, though. I'm using a Spring URIBasedViewResolver, configured as below:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
The views aren't being found and I am getting 404 errors. Is it possible to resolve views outside the container? All of the examples and tutorials I can find have the views located within the /WEB-INF folder.
Thanks in advance!
Kate

Yes it will be, but for the life of me I cannot remember how. You will probably need to use either a different view resolver or write a custom one. It should not be too hard.

Related

How to directly visit JSP pages with Spring loaded?

I'd like to know how to directly visit JSP pages under some directory of WEB-INF with Spring running on / path without writing any controller for view forwarding.
For example, I have a project myapp structured as follows:
src
WebRoot
`-- WEB-INF
|-- public
| `-- example.jsp
|-- views
Now, I want to visit example.jsp by directly navigating to http://localhost/myapp/public/example without implementing any controller.
What I've tried so far:
added <mvc:resources mapping="/public/**" location="/WEB-INF/public/"/> to my context xml, but it just won't work, the container keeps complaining about HTTP 404 - PAGE NOT FOUND /public/example.jsp.
added an internal resource view resolver to my context xml.
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
but I'm not sure what to do next, I maybe put many JSP pages under /WEB-INF/public, so writing a controller for each of them will be tedious.
Is there any canonical way to do this? please help!
Spring mvc we have one option
<mvc:view-controller path="/" view-name="example"/>.
This redirects the example.jsp page when you type / in the browser
Try something like that in your dispatcher servlet:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
Where the jsp folder contains your jsp files. From controller you can directly use the jsp name. You don't need to mention the jsp folder going by the above definition.

Spring Message Not Displaying

I am trying to do internationalization in Spring-MVC for the first time and I'm having what I assume to be a configuration issue. I have a NLS file that I named NLS_en.properties which I placed in my application's WEB-INF\classes directory. The file contains the following NLS string:
MSG_HELLO = Hello to the Internationalized World
In my application's servlet.xml file I've defined the following beans:
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="WEB-INF/classes/NLS"/>
</bean>
In my JSP file I have the following tag:
<p><spring:message code="MSG_HELLO" text="You should not be seeing this text" />
When the JSP displays, of course, the message I see is the one I should not be seeing, so how do I have to configure my application so that I do see my HELLO message?
ResourceBundleMessageSource basename (as opposed to ReloadableResourceBundleMessageSource) refers by default to the classpath, so you should have it like :
<property name="basename" value="NLS" />
Now, depending on how you build, even if configuring correctly the message source, it may have been erased at the time you run the application.
Do not place resources directly into classes (or any target directory in general). If you use maven place it directly into resources. If you dont use any build framework put it in the root of the source directory.

Spring 4 config problems afer migration from v3 (util:properties location is no longer evaluating spEL)

I had a working spring 3 application which after migration to 4.1.0.RELEASE started to cause problems. Configuration below stopped working:
<jee:jndi-lookup id="dit_properties_path" jndi-name="dit_properties_path" resource-ref="true" />
<util:properties id="systemProperties" location="classpath:system.properties" />
<util:properties id="serverProperties" location="#{dit_properties_path}"/>
After migration it seems that spring is not able to resolve spEL #{dit_properties_path}, and is showing following error:
[PropertiesFactoryBean] [INFO]: Loading properties file from ServletContext resource [/#{dit_properties_path}]
[...]is java.io.FileNotFoundException: Could not open ServletContext resource [/#{dit_properties_path}]
Jndi resource is of type java.lang.String and points to local FS (C:\someFile.properties).
Here's tomcat definition of the resource /server and context/:
<Environment name="dit_properties_path" value="file:C:\someFile.properties"
type="java.lang.String" override="true"/>
and
<ResourceLink name="dit_properties_path"
type="java.lang.String"
global="dit_properties_path" />
Do anyone have any idea how to resolve this issue?
I have several property files in my app which are often accessed by #{propertyFileId['key']} and they have to be loaded dynamically from jndi resource path.
Change from :
<util:properties id="serverProperties" location="#{dit_properties_path}"/>
to:
<util:properties id="serverProperties" location="${dit_properties_path}"/>
solved the problem. Still, can't figure out why given expression doesn't work with hash sign.

css file is not working in Spring Theme module. properties files are loaded properly but css are not working

I configured my confugration file ie. dispatcher-servlet.xml file for Themes using following beans
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean>
<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default" />
</bean>
I have added 3 properties
as theme-black.properties,theme-blue.properties,theme-default.properties under the source directory.
in each properties file I added key-value pair as following
style=style/blue.css
style=style/black.css
style=style/default.css
i put style folder under Web-Content .
problem is this ResourceBundleThemeSource loaded properties file successfully but could not able to load css file.
In JSP file I have added follwing code
<link rel="stylesheet" href="<spring:theme code='style'/>"
type="text/css" />
for changing
<span style="float: right;"> <a href="?theme=default">
default</a> | blue | black
</span>
Please help me out if any issue is there........ please please please > Thanks in Advance
I don't have experience with Spring's theme support, however I spot a little mistake in your URL.
First your URLs are relative. You should always have absolute URLs (starting with /).
Second, when using any URL, you should use <c:url> or <spring:url> so that correct context prefix is used:
<spring:url var="cssUrl">
<jsp:attribute name="value"><spring:theme code="style"/></jsp:attribute>
</spring:url>
It is as simple as specifying the location of your static content (in you application-config.xml), and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/resources/"/>
And next you edit the themes properties file with :
style=resources/style/blue.css
And put blue.css file into: webapp/resources/themes
Now you can load css file using:
"resources/themes/blue.css" in href
Or using style in code of spring:theme tag.
This works for me.
I don't know whether you have put the bean themeChangeInterceptor ref under bean DefaultAnnotationHandlerMapping . If you have done that , then please remove that from there and put that inside the <mvc:interceptors>tag like below
<mvc:interceptors>
<ref bean="themeChangeInterceptor" />
</mvc:interceptors>

Spring - Rewrite one URL to another

I have a Spring 2.5 application that contains a Flash banner. I don't have the source for the Flash component but it has links hardcoded to certain pages that end in .html I want to be able to redirect those .html pages to existing jsp pages. How can I have Spring resolve a few .html pages to .jsp pages?
My project looks like:
WebContent
|
-sample.jsp
-another.jsp
WEB-INF
|
-myapp-servlet.xml
-web.xml
I want localhost:8080/offers.html to redirect to localhost:8080/sample.jsp
Can I do this with Spring? I already have a SimpleUrlHandlerMapping and UrlFilenameViewController defined in the myapp-servlet.xml that has to continue serving the pages it already is.
In my web.xml, I have
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Update
Here is the URL mapper. If I add a controller, how do I return the jsp view that is in the WebContent directory as the view resolver includes the /WEB-INF/jsp directory.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/page1.htm">page1Controller</prop>
<prop key="/page2.htm">page2Controller</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
I think you could benefit from the open source URL Rewriting library made by tuckey.org. The guys at SpringSource endorse this library, since it is set up for you automatically if you use Spring Roo to create a project, so it is of good quality. I have used it successfully in a number of projects.
See here for its homepage. And Skaffman is right, you want it to 'forward' instead of redirect, which is the default behaviour.
Configure it in web.xml like this:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
Then, in WEB-INF/urlrewrite.xml have an element like this:
<rule>
<from>offers.html</from>
<to>offers.jsp</to>
</rule>
I would use OCPsoft PrettyFaces or OCPsoft Rewrite for this:
With PrettyFaces:
create WEB-INF/pretty-config.xml
<url-mapping>
<pattern value="/offers.html" />
<view-id value="/offers.jsp" />
</url-mapping>
With Rewrite:
ConfigurationBuilder.begin()
.addRule(Join.path("/offers.html").to("/offers.jsp"));
I hope this helps.
~Lincoln
Firstly, I'm assuming that when you say "redirect", you really mean "forward". HTTP Redirects would not be appropriate here.
SO given that, here are some things to try:
Can't you just move the JSP files from WebContent into /WEB-INF/jsp/? You wouldn't have to change the ViewResolver definition, then.
You could try to have the controllers return a view name of something like ../../another.jsp, and hope that the servlet container resolves to /WEB-INF/jsp/../../another.jsp to /another.jsp.
The ViewResolver is only consulted if the controllers return the name of a view. Your controllers don't have to return the name of a view, they can return a View object directly, in this case a JstlView. This can point to whichever JSP you like. You can some controllers returning view names, and some returning View objects.
Remove the prefix property from your view resolver. This means you'd also have to change every existing controller, to prefix every view name they return with /WEB-INF/jsp/. Then you could refer to the JSPs under WebContent by name.

Categories

Resources