Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom view resolver in Spring MVC: Property 'url' is required

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?

like image 562
Blankman Avatar asked Mar 29 '12 20:03

Blankman


People also ask

Which interface should be implemented to create a custom view resolver?

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.

Which property should be set in Springboot for resolving Viewname?

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.

What is the view resolver in Spring MVC Mcq?

Explanation: The view resolver InternalResourceViewResolver maps each view name to an application's directory by means of a prefix and a suffix declaration.

What is the default view resolver in Spring MVC?

2) The InternalResourceViewResolver is also the default view resolver of DispatcherServlet class, which acts as the front controller in the Spring MVC framework.


2 Answers

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

like image 158
Bozho Avatar answered Sep 30 '22 21:09

Bozho


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:

  • A view that returns "hello world".
  • A view resolver that passes all returned view names to the above view.

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.

like image 35
Pablojim Avatar answered Sep 30 '22 23:09

Pablojim