views:

41

answers:

0

My spring mvc application has one single ContentNegotiatingViewResolver that defines JsonView for rendering json resonses:

<mvc:annotation-driven/>

<context:component-scan base-package="world.domination.test"/>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="com.secondmarket.connector.springmvc.MappingJacksonJsonViewEx"/>
        </list>
    </property>
</bean>

The whole application sits on root url "myapp". Everything works as I need.

The first question is: how to return a static html page when accessing a certain url? Say, when accessing Spring uri /myapp/test I would like to render an html page /TestStuff.html that resides in root webapp folder.

I went ahead and wrote a simple controller:

@Controller
@RequestMapping("test")
public class TestConnector {

    @Autowired
    private RestTemplate tpl;

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        return "/TestStuff.html";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String post(@RequestParam("url") String url, @RequestParam("data") String data) {
        return tpl.postForObject(url, data, String.class, new HashMap<String, Object>());
    }
}

The get() method is supposed to tell Spring to render a TestStuff.html, but instead I get an error saying that the view with name "/TestStuff.html" is missing.

The second question is how to avoid the necessity to put extension to the URL. In my example, when I use /myapp/test instead of /myapp/test.html my ContentNegotiatingViewResolver uses a json view that renders {} (empty curly braces)

Any pointers are highly appreciated.