I would like to use different controllers in spring with different application configurations.
For example lets say I have one configuration using jsf/primefaces and another one using thymeleaf (for no special reason). Is it possible to map these configurations independently to different controllers?
Not sure but may be You can do it in two ways buy either returning view in your controller method as
#RequestMapping("/view1")
public String thymleaf(){
return "thymleaf.html";
}
#RequestMapping("/view2")
public String jspView(){
return "jspView.html";
}
or by configuring it in xml
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="viewNames" value="thymeleaf/*" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="viewNames" value="jsp/*" />
<property name="suffix" value=".jsp" />
</bean>
Related
I am getting javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'welcome.message' for locale 'en_US' error, when I use tag.
my configuration is in /WEB-INF/dispatcher-servlet.xml and looks like this
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="cookieResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
<property name="cookieName" value="my-locale-cookie"/>
<property name="cookieMaxAge" value="3600"/>
</bean>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
my messages are in src/main/resources/messages.properties and messages_en_US.properties and messages_fr.properties.
I was using JSP + JSTL but I'm boring of c:if, c:choose, ...
So, I want my JSP pages to be rendered with both JSP and Thymeleaf (I plan to remove all JSTL as soon as possible). I am using the Spring MVC framework:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="2" />
</bean>
In my controller, I just return the jsp without extenion.
return "folder/page";
Can my JSP pages be renderd first with the JSP resolver and then with the Thymeleaf resolver? If yes, how?
It seems that it is very complicated to chain JSP and Thymeleaf. So, I want to use the Internal resolver for JSP files and Thymeleaf template resolver for HTML files. How can I do it?
According to this post on the Thymeleaf forum, you have two solutions.
First solution :
Remove the suffix property in your bean declaration (<property name="suffix" value=".html" /> and <property name="suffix" value=".jsp" />) and pass the suffix in the return value of your controllers, e.g. :
#RequestMapping("/view1")
public String thymeleafView(){
return "mythymeleafview.html";
}
#RequestMapping("/view2")
public String jspView(){
return "myjspview.html";
}
Second solution :
Add the viewNames property to the resolvers. The value is the name of a folder which contains views depending on their extension. So you will have one folder for JSP files and another for HTML (thymeleaf) files, e.g. :
Configuration
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="viewNames" value="thymeleaf/*" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="viewNames" value="jsp/*" />
<property name="suffix" value=".jsp" />
</bean>
Controller
#RequestMapping("/view1")
public String thymeleafView() {
return "thymeleaf/mythymeleafview";
}
#RequestMapping("/view2")
public String jspView() {
return "jsp/myjspview";
}
Project folder
WEB-INF/views/jsp/myjspview.jsp
WEB-INF/views/thymeleaf/mythymeleafview.jsp
Both solutions work but have some contraints. You have to specify one way or another whether you want to resolve with JSP or Thymeleaf.
The "perfect" solution to chain JSP and Thymeleaf — which would consist in trying to resolve the view with JSP when it cannot be resolved with Thymeleaf or vice versa — is not possible, and Daniel Fernández (Thymeleaf team) explained why in this same post :
Thymeleaf allows you to create whichever ITemplateResolver implementation you wish, including some that might not allow to determine whether the template exists or not before actually reading it. [...] So, there is no way for Thymeleaf to be sure whether a template will be resolvable or not before trying to process the template. And that's why the ThymeleafViewResolver has to resort to the "viewNames" property.
Alternatively, two servlets works fine. The key is to keep servlet configuration minimal and include an appConfig.xml for database and other services (this avoids massive duplication of configuration)
Web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>AssessmentAdmin</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentAdmin</servlet-name>
<url-pattern>/xz/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AssessmentAdminTL</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AssessmentAdminTL</servlet-name>
<url-pattern>/xztl/*</url-pattern>
</servlet-mapping>
........
servlet for jsp:
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- <property name="viewNames" value="jsp/*" />-->
<property name="suffix" value=".jsp" />
</bean>
..........
<import resource="applicationContext.xml" />
</beans>
servlet for thymeleaf
<mvc:annotation-driven />
<!-- Thymeleaf -->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<import resource="applicationContext.xml" />
Tried it and it works fine
Here is the answer based on #Igd response
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="viewNames" value="*.jsp" />
</bean>
<!-- Thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="templateMode" value="HTML5" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewNames" value="redirect*" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="*.html" />
</bean>
And i use this for the mapping:
#RequestMapping("/view1")
public String thymeleafView(){
return "mythymeleafview.html";
}
#RequestMapping("/view2")
public String jspView(){
return "myjspview.jsp";
}
according to #Athanor's answer,we may have another choice.
we use property "viewNames" to control which resolver the template select
<!-- jsp -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="order" value="1" />
<property name="viewNames" value="*admin/*,*packer/*,*courier/*,/" />
</bean>
<!-- thymeleaf -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false"/>
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="*thymeleaf/*" />
<property name="order" value="2" />
</bean>
and the controller
#RequestMapping(value="/test")
public ModelAndView dboxPrint(Model model){
ModelAndView modelAndView = new ModelAndView("thymeleaf/dbox_print");
return modelAndView;
}
I got some interesting problem with spring viewresolver
I have set-up simple spring mvc-thymeleaf application
spring-servlet.xml is as follows:
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:annotation-driven />
<context:component-scan base-package="thymeleafexamples.thvsjsp" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="Messages" />
</bean>
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="index,*th" />
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".jsp" />
<property name="viewNames" value="*jsp" />
<property name="order" value="2" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="Messages" />
</bean>
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="index,*th" />
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".jsp" />
<property name="viewNames" value="*jsp" />
<property name="order" value="2" />
</bean>
and web.xml as
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
when i use controller with reqestmapping as same of my jsp i.e. subscribejsp
redirect worked well but when i change controller requestmapping to 'subscribe'
it got error like
javax.servlet.ServletException: Could not resolve view with name 'redirect: /subscribe' in servlet with name 'spring'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
My controller looks as belows:
#Controller
public class SubscribeJsp {
private static final Logger log = Logger.getLogger(SubscribeJsp.class);
public SubscribeJsp() {
super();
}
#ModelAttribute("allTypes")
public SubscriptionType[] populateTypes() {
return new SubscriptionType[] { SubscriptionType.ALL_EMAILS, SubscriptionType.DAILY_DIGEST };
}
#RequestMapping(value="/subscribe",method=RequestMethod.GET)
public String showSubscription(final Subscription subscription) {
return "subscribejsp";
}
#RequestMapping(value="/subscribesave", params={"save"})
public String subscribe(final Subscription subscription, final BindingResult bindingResult, final ModelMap model) {
if (bindingResult.hasErrors()) {
return "subscribejsp";
}
log.info("JUST ADDED SUBSCRIPTION: " + subscription);
model.clear();
return "redirect:subscribe";
}
}
You should use an absolute path for a redirection unless you are sure you really want to look for url relative to current path. So you you simply write your redirection that way :
return "redirect:/subscribe";
And you should remove the line
<property name="viewNames" value="*jsp" />
from the InternalResourceViewResolver in your spring-servlet.xm file. It blocks it from correctly finding views (including redirection ...) not ending in jsp and does not help in anything.
thanks Serge Ballesta, solution works by removing
but i have used two different view resolvers one for jsp and for thymeleaf.
and i want them two works accordingly. Actually i do not want to kept jsp pages and
thymeleaf enabled html pages in two different folders neither i can't return two
view names as jsp/mypage.jsp or thymelef/th.html from my controller. Any suggestiong to
solve this problem.
this will help in this situation but with this problem that i have to use same name for jsp and controller mapping names
keep the:
<property name="viewNames" value="index,*th" />
delete the:
<property name="viewNames" value="*jsp" />
You should correct your return statement from return "redirect:subscribe"; to return "redirect:/subscribe";
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/html/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="order" value="1" />
</bean>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value=""/>
<property name="suffix" value=".jsp"/>
<property name="order" value="2" />
</bean>
I have all html pages so it works fine with viewResolver but now I added jsp so created another bean with InternalResourceViewResolver. Somehow it doesn't seem to fall through jsp view resolver & gives exception SEVERE: ResourceManager : unable to find resource '/users.html' in any resource loader. when trying to access user.jsp
Controller code is
#RequestMapping("/layout")
public String getUserPage(ModelMap modelMap) {
return "users";
}
I think you should add prefix's value:
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="2" />
Spring Docs says :
The contract of a view resolver specifies that a view resolver can
return null to indicate the view could not be found. Not all view
resolvers do this, however, because in some cases, the resolver simply
cannot detect whether or not the view exists. For example, the
InternalResourceViewResolver uses the RequestDispatcher internally,
and dispatching is the only way to figure out if a JSP exists, but
this action can only execute once. The same holds for the
VelocityViewResolver and some others. Check the Javadoc for the view
resolver to see whether it reports non-existing views. Thus, putting
an InternalResourceViewResolver in the chain in a place other than the
last, results in the chain not being fully inspected, because the
InternalResourceViewResolver will always return a view!
for your code to work; Please change the order of jspViewResolver from 2 to 1 like below,
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value=""/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1" />
as the higher the order property, the later the view resolver is positioned in the chain.
I am using InternalResourceViewResolver for resolving file names. Till now I was using only JSP so it was completely fine. Now for few requests I need to send xml files residing in my WEB-INF folder, but not getting idea how to exclude or include support these XML files
My controller is like this:
#RequestMapping(value = "/sitemap/sitemap_index.xml", method = RequestMethod.GET)
public String viewXmlSitemapIndex(ModelMap model) {
return "/other/sitemap_index"; //sitemap_index.xml is actual xml file residing in other folder inside WEB-INF
}
And in dispatcherservlet I have defined InternalResourceViewResolver like this:
<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>
I guess you should use a ContentNegotiatingViewResolver like this
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="ignoreAcceptHeader" value="true" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="prefixJson" value="true" />
</bean>
</list>
</property>
</bean>
Here you can define multiple view resolver and you can choose the one to use for the XML
I hope this can help
Angelo