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.
Related
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.
I was wondering if it's possible to somehow conditionally include spring beans depending on some property.
In my applicationContext.xml I have a list of beans that I setup:
<bean id="server1Config" class="... />
<bean id="server2Config" class="... />
<bean id="server3Config" class="... />
...
Then I include them in a list:
<bean class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="server1Config"/>
<ref bean="server2Config"/>
<ref bean="server3Config"/>
...
</list>
</constructor-arg>
</bean>
I want to conditionally include server1Config, server2Config, server3Config, etc depending on whether ${includeServer1} == true, ${includeServer2} == true etc and if possible, only initialize those beans if they are marked for inclusion.
To clarify, it's a ping service checking if servers are online or not, each bean contains special urls. If I have 5 servers running, I'd like to set in my config includeServer1=true ... includeServer5=true ... includeServer6=false, if I shutdown server2, I'd like to change includeServer2=false before shutting down the server to not get bombarded with SMSe telling me server2 is offline.
As your names refer to different stages or enviroments, spring profiles might be helpful to use. You can define beans like this inside your context.xml
<beans profile="dev">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
<jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
</jdbc:embedded-database>
</beans>
<beans profile="production">
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource"/>
</beans>
In the example [1] you see the usage of two profiles called "dev" and "production".
Spring will always load every bean without a profile
Depending on the profiles (yes, you can load multiple profiles at once) all the related beans will be loaded
Loading a profile in Java:
ctx.getEnvironment().setActiveProfiles("dev");
Loading two profiles
ctx.getEnvironment().setActiveProfiles("profile1", "profile2");
Loading from CMD Line declaratively:
-Dspring.profiles.active="profile1,profile2"
Usage in web.xml (can be comma-separated)
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
</servlet>
#Comment: If you want to do it using properties and you are able to use newer spring elements, annotations etc. please have a look at this tutorial [2] to make it work with properties file as you commented below.
[1] http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/
[2] http://kielczewski.eu/2013/11/setting-active-profile-and-property-sources-in-spring-mvc/
This is almost an add-on to #swinkler's answer.
He gave the first part of the solution which is usage of Spring 3.1+ profiles.
The second part would be to use a kind of automatic registration :
<bean class="java.util.ArrayList" id="serverConfigList"/>
<beans profile="server1">
<bean id="server1Config" class="... />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="serverConfigList"/></property>
<property name="targetMethod"><value>add</value></property>
<property name="arguments"><ref local="server1Config/></property>
</bean>
</beans>
That way you create an empy list and only add relevant configs to it.
Your code shouldn't change based on environment. If your aim is to use different settings for each environment then load them as properties at start up.
Refer this for 'external configuration'
This can be done in Spring framework 3.1 onwards using a built in Spring environment profiles.
Here's a few resources:
http://java.dzone.com/articles/spring-31-environment-profiles
Hope this helps.
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>
I just started the spring 3 development and I had use spring 2.5 previously.
I got stuck with the View Resolver. I had the following configuration
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
And yes it works if the jsp pages is in the /WEB-INF/jsp path. The problem that I stumble is that if I had a jsp inside (e.g. /WEB-INF/jsp/prod/Monitor/success.jsp), I cannot get it to resolve the page if I type http://localhost/Project/prod/Monitor/success.html in the browser.
Did i miss anything here. Just for more info, the jsp will show up if the jsp had a controller, but I need it to resolve jsp pages with no controller associate with it.
Add this into context:
<mvc:view-controller path="/prod/Monitor/success.html" view-name="/prod/Monitor/success" />
You can treat these pages as static resources .
see this for details.
I've got my Spring Security custom login form working. It displays errors if the user has input bad credentials, or is expired, etc.
Looking inside spring-security-core-2.0.5.RELEASE.jar, I notice the following files in the org.springframework.security package:
messages.properties
messages_cs_CZ.properties
messages_de.properties
messages_fr.properties
...etc...
and notice that they have the localised versions of the strings.
Setting my browser's preferred language to French doesn't make the French version of the string appear. What am I missing?
PUK
Fixed it:
In my applicationContext.xml I include the Spring Security messages by adding another basename:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>com.myapp.web.my_messages</value>
<value>org.springframework.security.messages</value>
</list>
</property>
</bean>
In my web.xml I added another listener:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>