View from ModelAndView vs View from ViewResolver - java

In Spring MVC, when request commes, DispatcherServlet specifies HandlerMapping that indicates which controller should process request. Controller processes request and returns model with specified View.
And I dont get what happens after that, when ViewResolver starts his work. If View is already specified by Controller, why there is a ViewResolver layer? What does it actually do? Shouldn't View been specified before or paraller with Controller? Or maybe Controller should trigger ViewResolver layer?

The ViewResolver maps view names to actual views.when you have for example:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
you tell spring in controller method when you return a string, add prefix and suffix to the return value and the load that view, lets say you have
#RequestMapping("/")
public String home() {
return "home";
}
ViewResolver will take home and change it to home.jsp and will look at /WEB-INF/views/ to load /WEB-INF/views/home.jsp

Related

No mapping found for HTTP request with URI [/springmvc2k/WEB-INF/views/home.jsp]

I have a Spring project. When I call the URL it is calling the controller and printing the result in console. But the requestmapping method prints the output but not displaying the view / jsp page.
Please find the below link for better understanding.
My Spring Project Structure:
the controller is called and printing 'IN' as output:
The controller i been using.
#Controller
public class BaseController {
#RequestMapping(value="/")
public String getRespnse(Model m) {
System.out.println("IN");
return "home";
}
}
This the view resolver I'm using.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
I cannot able view the jsp output in browser.Please suggest a way to handle.
If you map your DispatcherServlet to url pattern /* in your web.xml it overrides all other url mappings specified. To handle JSPs web containers includes a mapping *.jsp to handle JSPs.
So you mapping overrides the container`s *.jsp servlet and requests for servlets are now going through your DispatcherServlet which doesnt know how to handle JSPs.
Change your url-pattern from /* to /

Spring page redirection good habits

I'm a beginner with Spring and I'm trying to understand how a controller works.
I have several methods annotated with #RequestMapping and everything works fine, but I don't know how to handle simple page requests.
Suppose I have to redirect from one page to another, without making server logic. If I create the url redirecting to PageA.jsp it seems that I always need to define the method catching the request in the Controller, declaring a method that is pratically empty, like this:
#RequestMapping(value="/PageA.jsp")
public String redirectToPageA(){
return "PageA";
}
If I don't follow this approach I get 404 error as the controller can't find the mapping. I don't like this approach very much as it fills my controller with useless empty methods.
Which is the suggested approach?
I suggest using a view resolver.
Read more about it here - 17.5.1 Resolving views with the ViewResolver interface from 17. Web MVC framework.
In that section you have
As an example with JSP as a view technology, you can use the
UrlBasedViewResolver. This view resolver translates a view name to a
URL and hands the request over to the RequestDispatcher to render the
view
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
When returning test as a logical view name, this view resolver
forwards the request to the RequestDispatcher that will send the
request to /WEB-INF/jsp/test.jsp. including the code.
And this answer has the annotation based configuration of a view resolver (except you could use the UrlBasedViewResolver).

Difference between indexcontroller and viewresolver in spring

Im newbie to Spring framework. I started learning few basic things from this link. I was stuck with this topic. What is the difference b/w View and Index. Its not much clear in tat doc
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
You see that indexController extends PrameterizedViewController which is a basic controller that always returns a named view. The advantage of this controller is that the client is not exposed to the concrete view technology but only to the controller url.
ViewResolver is of type InternalViewResolver, this means it will resolve views such as jsp, servlet etc .
So you would use viewResolver to link to another jsp or servlet and the indexController would be used in order for your request to be processed by a #Controller class.

how to leverage on Spring MVC to implement front controller but not using controllers

the title of this post might be confusing.I'll try be best to clarify it.
I Started a project using Spring MVC it works fine no problem.After that i've realized
that i was a bit overkill and found that i needed a front controller dispatcher because
all a wanted was nice urls without extensions.
So instead of implementing a whole new font controller i will like to take advantage of the existing Spring MVC setup. here is an example of a controller
#RequestMapping("/accounts")
public String home() {
return "accounts";
}
#RequestMapping(value="/")
public String Home(){
return "home";
}
as you can see, the return string is the one indicating the view based on the resourceviewresolver
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
now i don't want to use the controller, but need a mechanism to map the request to a physical page.i'm wondering if that could be possible.
How can i do that? i should simply create another dispatcher?
thanks for reading this and helping out.
Spring 3 supports <mvc:view-controller> element for such cases:
<mvc:view-controller path="/" view-name="home" />
<mvc:view-controller path="/accounts" view-name="accounts" />
See also:
MVC Simplifications in Spring 3.0

Possibility to use <c:import> with spring views

I was wondering if there is a possibility that I could use the JSTL function to import spring Views.
so something likes this:
<div id="contentHolder">
<c:import url="/foo.do?bar" />
</div>
In the XML stands:
<!-- Viewresolver -->
<b:bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<b:property name="viewClass"><b:value>org.springframework.web.servlet.view.JstlView</b:value></b:property>
<b:property name="prefix"><b:value>/WEB-INF/jsp/</b:value></b:property>
<b:property name="suffix"><b:value>.jsp</b:value></b:property>
</b:bean>
but with on this way I get the error:
Circular view path [/WEB-INF/jsp/foo/index.jsp]: already dispatched to this view path within the same request [/BAR/WEB-INF/jsp/foo/index.jsp]. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
the following manner also doesn't work:
<jsp:include page= and <%#include file=
I guess this doesn't work because of the Viewresolver, but is there a way I can use it this way instead of using JavaScript by example.
you can try full path of the url

Categories