6

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?

1
  • to start with - why do you need a new resolver. Then, you should just put a breakpoint and trace what is exactly happening and where is NOT_FOUND returned from. That's what I meant with debugging. Commented Apr 7, 2012 at 10:57

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

8 Comments

why does it compile then if I didn't implement it? or it is throwing an exception and I have to override that exception?
it is compiling because it has a default implementation that returns true. And the properties are checked at runtime
you'd have to debug that. Apart from being a really simply renderer, I don't see a problem
It doesn't seem to be hitting the view resolver code when I am in debug mode
is returing ModelAndView the issue?
|
3

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.

Comments

3

You have started out wrong: instead of a ViewResolver, you are implementing a View. See Spring's documentation on this, there are many base classes you can start from. As for the view name, it will be quite obvious once you get on the right track since the method View resolveViewName(String viewName, Locale locale) is the only one in the ViewResolver interface.

But, judging from your confusion of these concepts, maybe your real question is "help me make a Hello World in Spring MVC". In that case I should really direct you to the samples that come with the Spring MVC distribution, but here's something for a quick start. First of all, you don't need to implement a ViewResolver or even a View. The most basic thing you need is a Controller, which can directly produce the response:

public class MyController implements Controller
{
  public ModelAndView handleRequest(HttpServletRequest request,
     HttpServletResponse response)
  {
    response.getWriter().println("Hello, world!");
    return null;
  }
}

Next, you need a HandlerMapping, this is the simplest variant:

<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

Finally, declare your controller and set its name to the URL you want it to serve:

<bean name="/home/index" class="MyController" />

Comments

1

If you dig into the nested exception you will see

Caused by: java.lang.IllegalArgumentException: Property 'url' is required
    at org.springframework.web.servlet.view.AbstractUrlBasedView.afterPropertiesSet(AbstractUrlBasedView.java:67)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$6.run(AbstractAutowireCapableBeanFactory.java:1504)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1502)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 37 more

So Exception came at AbstractUrlBasedView.java: line 67, if you see the spring source file @fisheye.springsource.com you will find the following code

public void afterPropertiesSet() throws Exception { if (isUrlRequired() && getUrl() == null) { throw new IllegalArgumentException("Property 'url' is required"); } }

/**
 * Return whether the 'url' property is required.
 * <p>The default implementation returns <code>true</code.
 * This can be overridden in subclasses.
 */
protected boolean isUrlRequired() {
        return true;
}

So it is checking for if isUrlRequired and getURL is null it will show the above exception, so by changing the isUrlRequired to false you can avoid the 1st exception

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.