You are here: Home / Topics / Use ViewResolver in Spring MVC

Use ViewResolver in Spring MVC

Filed under: Spring on 2023-08-27 17:24:38

view resolver is a class in spring framework which allows us to configure urls dynamically

 

ctrl + shift + T to open type in eclipse, search *Viewresolver you will find InternalResourceViewResolver this class will do it automatically.

Make a bean of this class in mainController-servlet.xml file (In previous topics we have covered this topic).


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


Now you donot need to pass the complete url in your controller method. You just need to pass the name of your view. Prefix and suffix will handle all the request.


code of Shop.java will be;

package first.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Shop {


@RequestMapping("/shop")
public String visitShop() {
 return "shop";
}
}

 

This will return the view which is in /WEB-INF/views/shop.jsp  

hope you got the idea of ViewResolver.

Thanks and meet you soon with good topic ...........

Take care.


About Author:
M
Mr. Dubey     View Profile
Founder of MCQ Buddy. I just like to help others. This portal helps students in getting study material free. Share your stuff here so that others can get benefitted.