Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify complete path of view file in spring controller instead of using view resolver

Is it possible to specify complete path of a view file in controller without making use of view resolver?

Suppose I have just one file, say a XML file in my views and for a particular request I want to serve that XML file. By using view resolver, I am not able to find a way to pick up such a file and server it directly like me serve a jsp. So, for such a case can I do something in which I specify complete path in controller like we used to do in servlet's getRequestDispatcher?

My view resolver is currently configured for JSP only, I am not able to find a view using view resolvers to handle this situation, my view resolver is like as given below:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
    <value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
    <value>.jsp</value>
</property>

Currently I am using getRequestDispatcher in controller for this? Is there any spring based alternative?

like image 315
Abhi Avatar asked Feb 04 '26 02:02

Abhi


1 Answers

Spring MVC allows you to create a request handling method with different return types, see the reference guide. Instead of a String return a View from your method which points directly to your resource. When you return a View instance Spring MVC will not consult a ViewResolver but simply use the view as is. For you case you probably want to use an InternalResourceView.

@RequestMapping
public View yourRequestHandlingMethod() {
    return new InternalResourceView("path/to/resource");   
}
like image 110
M. Deinum Avatar answered Feb 06 '26 03:02

M. Deinum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!