I'm trying create a simple view resolver that returns hello world regardless of what view you want (as a starting point).
I have this so far:
public class MyViewResolver extends AbstractTemplateView {
@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
doRender(model, request, response);
}
protected void doRender(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
PrintWriter writer = response.getWriter();
writer.write("hi from my resolver!");
}
}
Now I am getting this error:
2012-03-29 16:51:28.855:WARN:/:unavailable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewResolver' defined in ServletContext resource [/WEB-INF/application-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'url' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
I have implemented whatever the AbstractTemplateView required, not sure what url property it is asking for?
Also, where is the name of the view that is passed to this viewresolver?
Update
So I added:
@Override
public boolean isUrlRequired() {
return false;
}
And now I am just getting an error like:
HTTP ERROR 404
Problem accessing /home/index. Reason:
NOT_FOUND
My application-context.xml has:
<bean id="viewResolver" class="com.example.MyViewResolver">
</bean>
What am I missing something?
The two interfaces which are important to the way Spring handles views are ViewResolver and View . The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.
Thymeleaf view resolver works by surrounding the view name with a prefix and suffix. The default values of prefix and suffix are 'classpath:/templates/' and '. html', respectively. Spring Boot also provides an option to change the default value of prefix and suffix by setting spring.
Explanation: The view resolver InternalResourceViewResolver maps each view name to an application's directory by means of a prefix and a suffix declaration.
2) The InternalResourceViewResolver is also the default view resolver of DispatcherServlet class, which acts as the front controller in the Spring MVC framework.
You extend (indirectly) AbstractUrlBasedViewResolver
, so it's logical that a URL is required to resolve the view. However, if in your case it is not required, you can override the isUrlRequired()
method and return false
You have mixed up two concepts in Spring MVC. In Spring MVC you need both a view resolver and a view.
In your question you want two things:
To create the view: Extend: org.springframework.web.servlet.View Your implementation can then write "hello world" to the http response.
To create the view resolver: Extend: org.springframework.web.servlet.ViewResolver Your implementation should always te return your previously created view.
You can see that the viewresolver base class is where you have acceess to the returned view name. See: https://fisheye.springsource.org/browse/spring-framework/spring-webmvc/src/main/java/org/springframework/web/servlet/ViewResolver.java
This is the most basic way to answer your question. Spring offers many implementations of these classes for more specialised purposes that may suit your use case.
Hope this helps. Let me know if you need any more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With