全部博文(89)
分类: Java
2009-11-10 10:57:50
Spring MVC helps in building flexible and loosely coupled web applications. The Model-view-controller design pattern helps in seperating the business logic, presentation logic and navigation logic. Models are responsible for encapsulating the application data. The Views render response to the user with the help of the model object . Controllers are responsible for receiving the request from the user and calling the back-end services.
The figure below shows the flow of request in the Spring MVC Framework.
When a request is sent to the Spring MVC Framework the following sequence of events happen.
01.
import
javax.servlet.http.HttpServletRequest;
02.
import
javax.servlet.http.HttpServletResponse;
03.
04.
import
org.springframework.web.servlet.ModelAndView;
05.
import
org.springframework.web.servlet.mvc.AbstractController;
06.
07.
public
class
HelloWorldController
extends
AbstractController {
08.
09.
private
String message;
10.
11.
@Override
12.
protected
ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws
Exception {
13.
return
new
ModelAndView(
"welcomePage"
,
"welcomeMessage"
, message);
14.
}
15.
16.
public
void
setMessage(String message) {
17.
this
.message = message;
18.
}
19.
20.
}
The HelloWorldController class has a message property that is set thru the setter injection. The HelloWorldController class should override the handleRequestInternal() method to process the request. After processing the request the handleRequestInternal() method returns a ModelAndView object back to the DispatcherServlet. 01.
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
web-app
xmlns:xsi
=
""
xmlns
=
""
xmlns:web
=
"/web-app_2_5.xsd"
xsi:schemaLocation
=
" /web-app_2_5.xsd"
id
=
"WebApp_ID"
version
=
"2.5"
>
03.
<
servlet
>
04.
<
servlet-name
>dispatcher
servlet-name
>
05.
<
servlet-class
> org.springframework.web.servlet.DispatcherServlet
servlet-class
>
06.
<
load-on-startup
>1
load-on-startup
>
07.
servlet
>
08.
<
servlet-mapping
>
09.
<
servlet-name
>dispatcher
servlet-name
>
10.
<
url-pattern
>*.htm
url-pattern
>
11.
servlet-mapping
>
12.
<
welcome-file-list
>
13.
<
welcome-file
>redirect.jsp
welcome-file
>
14.
welcome-file-list
>
15.
web-app
>
Here the servlet name is dispatcher. By default the DispatcherServlet will look for a file name dispatcher-servlet.xml to load the Spring MVC configuration. This file name is formed by concatenating the servlet name ("dispatcher") with "-servlet.xml". Here we user the the url-pattern as ".htm" inorder to hide the implementations technology to the users.
The redirect.jsp will be invoked first when we execute the Spring web application. This is the only jsp file outside the WEB-INF directory and it is here to provide a redirect to the DispatcherServlet. All the other views should be stored under the WEB-INF directory so that they can be invoked only through the controller process.
To create a bean configuration file right click the WebContent folder and select New -> Other. The following dialog box appears.
Enter the file name as "dispatcher-servlet.xml" and click the Finish button.
Now the Spring bean configuration file is created, we need to configure the Controller and the ViewResolver classes. The following code shows how to do this.
|
1.
<
bean
name
=
"/welcome.htm"
class
=
"com.vaannila.HelloWorldController"
>
2.
<
property
name
=
"message"
value
=
"Hello World!"
/>
3.
bean
>
Here the name attribute of the bean element indicates the URL pattern to map the request. Since the id attribute can't contain special characters like "/" , we specify the URL pattern using the name attribute of the bean element. By default the DispatcherServlet uses the BeanNameUrlHandlerMapping to map the incoming request. The BeanNameUrlHandlerMapping uses the bean name as the URL pattern. Since BeanNameUrlHandlerMapping is used by default, you need not do any seperate configuration for this.
We set the message attribute of the HelloWorldController class thru setter injection. The HelloWorldController class is configured just like an another JavaBean class in the Spring application context, so like any other JavaBean we can set values to it through Dependency Injection(DI).
The redirect.jsp will redirect the request to the DispatcherServlet, which inturn consults with the BeanNameUrlHandlerMapping and invokes the HelloWorldController. The handleRequestInternal() method in the HelloWorldController class will be invoked. Here we return the message property under the name welcomeMessage and the view name welcomePage to the DispatcherServlet. As of now we only know the view name, and to find the actual view to invoke we need a ViewResolver.
The ViewResolver is configured using the following code.
01.
<
bean
id
=
"viewResolver"
02.
class
=
" org.springframework.web.servlet.view.InternalResourceViewResolver"
>
03.
<
property
name
=
"prefix"
>
04.
<
value
>/WEB-INF/jsp/
value
>
05.
property
>
06.
<
property
name
=
"suffix"
>
07.
<
value
>.jsp
value
>
08.
property
>
09.
bean
>
Here the InternalResourceViewResolver is used to resolve the view name to the actual view. The prefix value + view name + suffix value will give the actual view location. Here the actual view location is /WEB-INF/jsp/welcomePage.jsp
The following library files are needed to run the example.
01.
antlr-runtime-3.0
02.
commons-logging-1.0.4
03.
org.springframework.asm-3.0.0.M3
04.
org.springframework.beans-3.0.0.M3
05.
org.springframework.context-3.0.0.M3
06.
org.springframework.context.support-3.0.0.M3
07.
org.springframework.core-3.0.0.M3
08.
org.springframework.expression-3.0.0.M3
09.
org.springframework.web-3.0.0.M3
10.
org.springframework.web.servlet-3.0.0.M3
To execute the example run the redirect.jsp file. The following page will be displayed.