How to use multiple ViewResolvers in Spring? - spring

I am working on a web app where I have most of my pages making use of apache tiles (2.1.2), but a few of them need to just be plain jsps.
I am having a problem in that both an InternalResourceViewResolver and a UrlBasedViewResolver will try to resolve the view no matter what, so that no matter which ordering I use, it will either fail on the plain JSP pages, or on the tiles pages.
Here is the config:
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
<property name="order" value="0"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1"/>
</bean>
To make it more clear what I am trying to do, I need to be able to have view states like this:
<view-state id="someState" view="/someDir/foo"><!--render foo.jsp -->
<transition on="foo" to="bar"/>
</view-state>
<view-state id="someState" view="something.core"><!--render tile defintion named 'something.core' -->
<transition on="foo" to="bar"/>
</view-state>
Does anyone know how to configure things so that I can get it to render tiles definitions and plain jsps?

As you say, you cannot chain these together. The javadoc for both states clearly that they must both be at the end of the resolver chain.
I suggest that if you really need to use these togather, then you write a simple custom implementation of ViewResolver which takes the view name, and decides which of your two "real" view resolvers to delegate to. This assumes that you can tell which resolver to call based on the view name.
So you'd define a custom ViewResolver like this:
public class MyViewResolver implements ViewResolver {
private ViewResolver tilesResolver;
private ViewResolver jspResolver;
public void setJspResolver(ViewResolver jspResolver) {
this.jspResolver = jspResolver;
}
public void setTilesResolver(ViewResolver tilesResolver) {
this.tilesResolver = tilesResolver;
}
public View resolveViewName(String viewName, Locale locale) throws Exception {
if (isTilesView(viewName)) {
return tilesResolver.resolveViewName(viewName, locale);
} else {
return jspResolver.resolveViewName(viewName, locale);
}
}
private boolean isTilesView(String viewName) {
.....
}
}
You'd need to implement the isTilesView method to decide which resolver to delegate to.
In the XML config, define this new view resolver, and make sure it appears before the other ones.
<bean class="MyViewResolver">
<property name="tilesResolver" ref="tilesViewResolver"/>
<property name="jspResolver" ref="viewResolver"/>
</bean>

I've just solved the same problem by splitting the *-servlet.xml config file in two; in my case the main application uses Tiles, but I want QUnit tests to be simple JSPs.
app-servlet.xml contains only the Tiles view resolver, tests-servlet.xml only contains the JSP view resolver and web.xml mappings are dispatching requests to the correct servlet basing on the URL.
<servlet-mapping>
<servlet-name>app</servlet-name> <!-- will reach app-servlet.xml -->
<url-pattern>/foo</url-pattern> <!-- will use "foo" Tile -->
<url-pattern>/bar</url-pattern> <!-- will use "bar" Tile -->
</servlet-mapping>
<servlet-mapping>
<servlet-name>tests</servlet-name> <!-- will reach tests-servlet.xml -->
<url-pattern>/foo-test</url-pattern> <!-- will use foo-test.jsp -->
<url-pattern>/bar-test</url-pattern> <!-- will use bar-test.jsp -->
</servlet-mapping>

It looks like you're on the right track, but the thing to bear in mind is that some view resolvers behave as if they have always resolved the view. You need to make sure to put such resolvers last in your ordering. I believe the Tiles view is one such.
Edit: whoops... yes, the other poster is correct, both of these resolvers will do 'always match' so you can't use them both in a chain. Another alterative would be to try to extend the TilesView to do a simple JSP render if it cant find a configured tile view.

Yes you can use any number of view resolver in your your project.
So you can use both 'tiles View resolver' and 'Internal view resolver' in same project. .
you have to configure a ContentNegotiatingViewResolver . .
and give order value in your view resolvers.
<property name="order" value="int Value here" />
like I have given tiles view resolver 2 and internalviewresolver 3. .It will first check in tiles definitions if a view is not found in tiles it will be checked in InternaiViewResolver
here is some configurations that works for me.
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
</map>
</property>
<property name="parameterName" value="accept"></property>
<property name="favorParameter" value="true"></property>
<property name="defaultContentType" value="text/html"></property>
<property name="viewResolvers">
<list>
<ref bean="tilesViewResolver" />
<ref bean="internalViewResolver" />
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<!-- Configures the Tiles layout system -->
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/layouts/layouts.xml</value>
<!-- Scan views directory for Tiles configurations -->
<value>/WEB-INF/views/**/views.xml</value>
</list>
</property>
</bean>
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView">
<property name="order" value="3" />
</bean>
<bean id="internalViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

I resolved this issue by simply adding tiles definition for plain jsp's layout, like this:
<definition name="plain-jsp.layout" template="/WEB-INF/layouts/plainJsp.jspx" >
<put-attribute name="content" value=""/>
</definition>
Then you just can use this layout as template for including your simple jsp files.
<definition name="catalog/details" extends="plain-jsp.layout">
<put-attribute name="content" value="/WEB-INF/views/catalog/details.jspx"/>
</definition>
And layout template file:
<html xmlns:tiles="http://tiles.apache.org/tags-tiles"
xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:output doctype-root-element="HTML"/>
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:directive.page pageEncoding="UTF-8" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
</head>
<body>
<div id="content">
<tiles:insertAttribute name="content"/>
</div>
</body>
</html>

Related

How to apply spring-mobile-device interceptor to welcome-file?

I,m using SpringMVC and i have different design for each device type Desktop/Tablet/Mobile
<!-- START of MOBILE -->
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean
class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
<mvc:interceptors>
<!-- Resolve the device which has generated the request -->
<bean
class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor" />
<!-- User's site preference -->
<bean
class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
<!-- Redirects users to the device specific site -->
<bean
class="org.springframework.mobile.device.switcher.SiteSwitcherHandlerInterceptor"
factory-method="urlPath">
<constructor-arg value="/m" />
<constructor-arg value="/t" />
<constructor-arg value="/" />
</bean>
</mvc:interceptors>
<!-- Device aware view resolving -->
<bean id="liteDeviceDelegatingViewResolver"
class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
<constructor-arg>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</constructor-arg>
<property name="mobilePrefix" value="m/" />
<property name="tabletPrefix" value="t/" />
<property name="normalPrefix" value="/" />
<property name="enableFallback" value="true" />
</bean>
<!-- END of MOBILE -->
I have /m /t and / folders views in /WEB-INF/views/ folder for each device and all works fine, but this configuration not applies only to welcome page, ie I open page with phone and see desktop-version of login.jsp (/WEB_INF/views/login.jsp but not /WEB-INF/views/m/login.jsp)
<welcome-file-list>
<welcome-file>/WEB-INF/views/login.jsp</welcome-file>
</welcome-file-list>
What I have to do to change to fix it.
Just don't specify page... and handle 'home' request by controller :)
modify your web.xml to:
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
or just remove tag
now, your HomeController should look like this:
#Controller
public class HomeController {
#RequestMapping("/")
public String home(SitePreference sitePreference, Model model) {
return "login";
}
}
hope this helps.

Which spring view resolver plays nice with angularjs?

I'm writing a webapp using angularjs and spring mvc as a REST service provider and as a partial view provider(I'm also using angular-ui-router so that I can have multiple nested partials). I currently don't have any use for template languages since I plan on doing everything in angular, however every single view resolver I've tried has some type of template language which clashes with angular and either crashes the application and/or fills my logs with errors.
First I tried using InternalResourceViewResolver but no luck as it seems that it only expects .jsp files and won't show anything else.
Then I tried using Thymeleaf. Thymeleaf follows the XML standard which forced me to rewrite most of my html to follow the xml requirements, and after I'd done that it died upon encountering a && inside an ng-show directive. So no luck with that either.
Then I tried Velocity. I've had most luck with velocity so far. It serves up html files nicely, doesn't stop upon encountering parse errors and allows me to serve up partial views the same way InternalResourceViewResolver does. However upon encountering angular variables prefixed by $ Velocity tries to parse them as VTL variables and fills my logs with messages like
velocity - Null reference [template 'clients/createOrEdit.html', line 1, column 65] : $invalid cannot be resolved.
Everything keeps working as it should but I'm not the one to just leave errors be, and I've found no way of disabling VTL.
That's my current experience with view resolvers.
I've also had an idea to treat .html files as static resources(which they kinda are before angular does it's magic) using mvc:resources but without any view resolver my application failed to start even if I set the main layout.html to be the welcome-file in web.xml
My question is. What should I use as a view resolver so that it plays nice with angularjs, and if I should even use view resolvers?
EDIT: I'm trying to use the ContentNegotiatingViewResolver and I get:
DEBUG ContentNegotiatingViewResolver - Requested media types are [text/html] based on Accept header types and producible media types [*/*])
DEBUG ContentNegotiatingViewResolver - No acceptable view found; returning null
DEBUG DispatcherServlet - Could not complete request
javax.servlet.ServletException: Could not resolve view with name 'layout.html' in servlet with name 'springDispatcherServlet'
webapp-config.xml (contextconfig in dispatcher servlet)
<mvc:annotation-driven />
<!-- Resources -->
<mvc:resources location="/libs/" mapping="/libs/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- Angular application data -->
<mvc:resources location="/WEB-INF/appjs/" mapping="/appjs/**" />
<!-- View locations -->
<mvc:resources location="/WEB-INF/html/" mapping="/**"/>
<!-- Controllers -->
<context:component-scan base-package="com.mrplow.controller" />
<!-- Views -->
<util:map id="contentMediaTypes">
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
</util:map>
<!-- <util:list id="defaultViews"> -->
<!-- <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> -->
<!-- </util:list> -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
p:order="1"
p:ignoreAcceptHeader="false"
p:defaultContentType="text/html"
p:mediaTypes-ref="contentMediaTypes" />
LayoutController.java
#Controller
#RequestMapping("/")
public class LayoutController {
#RequestMapping
public String getIndexPage() {
return "layout";
}
}
In order to use static resource(html,css,img,js) in spring, use a directory structure that looks like the following:
src/
package/
LayoutController.java
WebContent/
WEB-INF/
static/
html/
layout.html
images/
image.jpg
css/
test.css
js/
main.js
web.xml
springmvc-servlet.xml
#Controller
public class LayoutController {
#RequestMapping("/staticPage")
public String getIndexPage() {
return "layout.htm";
} }
<!-- in spring config file -->
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
layout.html
<h1>Page with image</h1>
<img src="/static/img/image.jpg"/>
Note you have to mention /static/img/image.jpg not just /image.jpg ..Same applies for css and js.
OR
You can also use content negotiating view resolver as shown below:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="rss" value="application/rss+xml" />
<entry key="html" value="text/html"/>
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
Spring MVC will use “ContentNegotiatingViewResolver” (order=1) to return a suitable view (based on file extension declared in “mediaTypes” property), if not match, then use “InternalResourceViewResolver” (order=2) to return a default JSP page.
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now from your jsp you can redirect to your static html page as well
#Controller
public class LayoutController {
#RequestMapping("/index")
public String getIndexPage() {
return "index";
}
}
index.jsp
<form:form method="GET" action="/static/html/layout.html">
Now try to access your service through http://yourapp.com//index show the form action mentioned above.It will show the layout.html
click on a button or submit in jsp page to invoke the layout.html page
I think ContentNegotiatingViewResolver is the best view resolver, because you will be able to integrate it with Jackson2 to response the data in JSON, XML or HTML text among others responses types that you need.
For example, look this approach.
http://hillert.blogspot.com.es/2011/01/rest-with-spring-contentnegotiatingview.html
For people who look for how to make the solution with ContentNegotiatingViewResolver to work in new version of Spring:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager" ref="contentNegotiationManager"/>
<property name="defaultViews">
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</property>
</bean>
And after that configure extensions what you need in ContentNegotiationManagerFactoryBean:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<props>
<prop key="json">application/json</prop>
<prop key="html">text/html</prop>
<prop key="xml">application/xml</prop>
// and so on
</props>
</property>
<property name="ignoreAcceptHeader" value="true"/>
</bean>

ControllerAdvice with ExceptionHandler and ModelAndView not loading the correct view on screen

I need help on this, please take a look at my code:
#ControllerAdvice
#EnableWebMvc
public class GlobalExceptionController {
#ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex) {
// create the model and view with the tiles View pointing to error jsp page
ModelAndView model = new ModelAndView("pagina.erro");
model.addObject("errCode", ex.getErrCode());
model.addObject("errMsg", ex.getErrMsg());
return model;
}
...
Tiles configuration:
<definition name="pagina.erro" extends="baseLayout">
<put-attribute name="titlepagina" value="Página de Erro" />
<put-attribute name="body" value="/WEB-INF/jsp/error/erro-generico.jsp" />
</definition>
In my Spring configuration xml I have:
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
The page erro-generico.jsp is being loaded because if I put a wrong code in it errors will be shown on eclipse console.
The problem is: the page is not shwowing, the actual page keeps showing on the browser, not even the URL changes.
What could be wrong?
The problem is that the view resolver being used is not compatible with Tiles, it only works for pure JSPs.
It's possible to configure a view resolver for Tiles by following these instructions - 14.3.2. How to integrate Tiles:
bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/defs/general.xml</value>
<value>/WEB-INF/defs/widgets.xml</value>
<value>/WEB-INF/defs/administrator.xml</value>
<value>/WEB-INF/defs/customer.xml</value>
<value>/WEB-INF/defs/templates.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>

Show properties values in views in Spring MVC

I'm struggling to make something veeery simple work. Everything I really need to do is to write a property value in a view, i.e.:
<!DOCTYPE html>
<head>
...
<base href="${properties.config.baseurl}" />
...
</head>
<body>
...
</body>
</html>
My Spring MVC configuration (relevant bit):
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list><value>/WEB-INF/config.properties</value></list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="exposedContextBeanNames">
<list><value>properties</value></list>
</property>
</bean>
/WEB-INF/config.properties file:
config.baseurl = http://localhost:8080/
The view doesn't display anything at all (i.e. <base href="" />), can anyone explain to me why?
Thank you.
You need to use
${properties['config.baseurl']}
rather than
${properties.config.baseurl}
Otherwise it will try to navigate config.baseurl like a bean path, rather than as a string literal.

Access to freemarker variables in html file using FreeMarkerViewResolver

I configured my web application like this link http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch17s04.html
My context
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
<property name="freemarkerSettings">
<props>
<prop key="tag_syntax">square_bracket</prop>
<prop key="auto_import">spring.ftl as spring, echannels.ftl as echannels
</prop>
<prop key="template_update_delay">2147483647</prop>
</props>
</property>
</bean>
<bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html;charset=ISO-8859-1" />
<property name="cache" value="${viewResolver.html.cache}" />
<property name="prefix" value="html/" />
<property name="suffix" value=".html" />
</bean>
I use spring-webflow to link all my page html.
In my page html, i can access to conversion scope variables by using ${name_variable}.
My question is: how could i access to FreeMarker variable "foo" in my html if i defined it in my context:
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
<property name="freemarkerVariables">
<map>
<entry key="foo" value="foo" />
</map>
</property>
</bean>
Example of my HTML
<html>
<head>
<title>Welcome</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<h1>Welcome ${issuerId}</h1>
<form action="?execution=${flowExecutionKey}" method="post" name="defineFeeForm">
<input type="submit" name="_eventId_next" value="Next" /> <br/>
</form>
</body>
I can access to ${issuerId} because i have request.setConversationScope("issuerId", "1234") but i want to access also to foo freemarker variable but when i use ${foo}, i got Expression foo is undefined.
Any ideas?
Thanks to #ddekany , it's worked, there're nothing wrong in my configuration. I confused before. :). Yes, foo is visible simply as ${foo}.
Another remark, if i want to add new variables during running of my application, i can use
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("foo2", "foo2");
freeMarkerConfigurer.getConfiguration().setAllSharedVariables(new SimpleHash(variables, freeMarkerConfigurer.getConfiguration().getObjectWrapper()));
and i can access with ${foo2}.
Thanks again. It's done now.

Resources