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.
Related
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
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 /
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).
I am using spring mvc + spring security to build a simple web application. I implemented the login/logout (spring mvc) but i want to provide also a registration jsp accessible directly from the login.jsp. Basically i just need a simple link from the login.jsp to the regsiter.jsp without passing any parameters or whatever. I just want to ask you what is the best practice to achieve this?
Is there any way to navigate directly between the two jsps without routing the request down to the controller? (or this is not really a spring mvc way?) To be honest my only "problem" is just to have a separate method in the register controller which does nothing just routes the request to the register jsp. I mean:
RegisterController
#Controller
public class RegisterController {
#RequestMapping(value="/view_register.htm", method = RequestMethod.POST)
/** Navigates to the register page */
public String navigateToRegistration(ModelMap model) {
return "register";
}
#RequestMapping(value="/register.htm", method = RequestMethod.POST)
/** Handles request from the registration page and registers the user */
public String registerUser(ModelMap model) {
// hard stuff to register the user
return "welcome";
}
}
WEB-INF/pages/login.jsp
......
<tr>
<td>Don't have an account yet.</td>
<td> Register here
</td>
</tr>
......
WEB-INF/pages/register.jsp
......
<form name='registration' action="<c:url value='register.htm' />"
method='POST'>
......
WEB-INF/web.xml - routing each request to spring mvc
......
mvc-dispatcher
/
WEB-INF/mvc-dispatcher-servlet
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:/applicationContext.xml" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Is my implementation correct? This is the mvc way? I just want to know whether i understand this well and not doing any antipattern stuff. The thing what confuses me is really this additional small method in the regsitration controller which does nothing just navigates from the login to register. Is there at least any naming convention for this kind of methods? Would you recommend rather a ForwardController which could act as a Manager/Dispatcher and would just manage this kind of navigation requests?
Your structure looks correct: One exception is that the /view_register.htm should be a GET, not a POST. Spring MVC uses a front controller pattern, and so every request does go through a controller. When the only thing a request does is to return a view, you can configure your mappings using the ParameterizableViewController instead of coding a controller, but I tend to code a method like you have when there are multiple related requests within a controller. As your app evolves, you might find there is some session setup you will add, and this just keeps all of the registration requests in the same class.
Don't forget to configure security to allow anonymous access to your registration requests.
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