*
The moose likes Spring and the fly likes How dispatcher-servlet.xml works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How dispatcher-servlet.xml works" Watch "How dispatcher-servlet.xml works" New topic
Author

How dispatcher-servlet.xml works

Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi All,

i am completely new to spring,

i understood that urlMapping in dispatcher servlet will tell which controller to handle using <prop key="testing.htm">indexController</prop>.

In controller we will mention the logical name. Using that, the viewresolver adds the p:prefix and p:suffix to the logical name and shows the view.

Now my question is, if i have view files in different directory, how will i mention the correct view resolver for the request.

If my understanding or question is wrong please correct me.

Thanks In Advance,

Bennet Xavier. X
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You can have multiple view resolvers.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi,

i tried that,



this is what i added in the dispatcher-servlet.xml

the testing.jsp file is in WEB-INF directory, but its giving resource not found 404 error

SEVERE: PWC6117: File "D:\tech\Spring\build\web\WEB-INF\jsp\testing.jsp" not found this is error i am getting in server console.
Thanks,
Bennet

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
You should specify order of viewResolver1 to 1, and order of viewResolver to 2.
yusuf nazir
Greenhorn

Joined: May 13, 2009
Posts: 12
Hi,

Are you using a simpleformcontroller or abstractformcontroller?

depending on that i can give you a proper solution
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi,

i got that, once i change the order value as mentioned, its working fine.

it showing the file in /WEB-INF/

then the files in /WEB-INF/jsp are not working. 404 RESOURCE NOT FOUND

i think it checks for the viewresolver which has lowest order value and assigns, thats it.

its not taking the other viewresolver in the dispatcher-servlet.xml


Hi yusuf nazir,

i am using SimpleFormController,

if i am using abstractformcontroller, what i need to do.

thanks,

Bennet Xavier. X
yusuf nazir
Greenhorn

Joined: May 13, 2009
Posts: 12
Hi Bennet,

When i build spring app i use the a views file to handle the views.
for example, I have a springweb-views.xml


as you can see the xml file will handle the getting the right jsp. All you have to do is call the view name, either "continue" or "anotherView".
this xml file is placed besides the servlet and web.xml files.

this should at least help to resolve your "no resources found" error.

keep us posted if that was what you want or not.
Bennet Xavier
Ranch Hand

Joined: Jun 19, 2008
Posts: 162
Hi yusuf nazir,

if you are using a separate file for view. Then do i need map in web.xml or any-other place.

i tried modifying the dispatcherservlet.xml with what ever you posted, its not working.

my dispatcher servlet.xml look as mentioned below.



how will i call this jsp, i mean what name in the url i have to mention. Just testing.htm or else the url mentioned in the bean you have given(continue or anotherView).

Thanks
Bennet.

yusuf nazir
Greenhorn

Joined: May 13, 2009
Posts: 12
Hi bennet,

I made the neccesary changes to dispatcher-servlet.xml


ok now i created another file called spring-views.xml. place this in the same folder where the dispatcher-servlet.xml is located.


best thing to do is to try to build is simple as possible.

spring follows a very simple flow. I'll try to explain it.
If I'm not mistaken you;re project folder is as follow:

When you're application starts up it will look in the web.xml. The web.xml has a welcome-file-list that tells spring where to start.
the welcome-file-list has a lot of addresses, but only the first acceptable one is used on implementation.
In this case it's our redirect.jsp




the redirect.jsp tells spring which url to parse to generate the requested page.
here we tell the application to always start with the index.htm url.
so spring will now activate the right controller and the is the indexcontroller because index.htm is mapped with indexcontroller in your dispatcher servlet.



your indexcontroller is simpleformcontroller as you said so it should be something like this:


This is a very simpleformcontroller that just renders a page and does nothing else. As you see constructor has been defined for loading a bean on implementation of the controller. This is also configured in your dispacther-servlet.xml where you give your commandclass. A simpleform controller is meant to get information from the page when the form is submitted. the information is thus stored in the commanclass.

you will also notice that the dispachter servlet has a formview defined as "index". so spring will load up the corresponding jsp. This is configured in the spring-views.xml

By the way. I use the following tools: eclipse europa 3.3, maven 2.0 and deploy on tomcat 5.5
Do post what you use to build your app.

hope this helps

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How dispatcher-servlet.xml works
 
Similar Threads
[Security:090398]Invalid Subject: weblogic :java.lang.SecurityException: [nested exception is java.l
dataSourceResourceLoader to load velocity Template from db
Switching to spring 3 MVC
about spring framework
Doubt regarding how to specify servlet mapping URL patterns in web.xml