<c:redirect url="home.htm"/>
<c:redirect url="b.jsp">
<c:param name="data" value="jsp_passdata中文傳值"></c:param>
</c:redirect>
他們兩個作用相似
<jsp:forward page="/utils/errorReporter.jsp"/>
<jsp:forward page="/test4.jsp">
<jsp:param name="name" value="powerman"/>
<jsp:param name="address" value=" 北京西大街188號"/>
</jsp:forward>
<fmt:requestEncoding value="big5"/>
posted @
2007-08-29 10:53 華夢行 閱讀(219) |
評論 (0) |
編輯 收藏
關鍵字: Spring ? mvc ioc ????
請求的分發
請求首先到達DispatcherServlet,應用服務器會根據Web應用中web.xml文件定義的url映射將相應的請求分發到DispatcherServlet中
請求的處理
DispatcherServlet會查找相應的HandlerMapping接口的實現類,調用其中的方法:HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception,該方法會返回一個HandlerExecutionChain。返回的HandlerExecutionChain中包含了零個或者是多個Interceptor和一個處理請求的Handler。DispatcherServlet會調用Interceptor中的preHandle() 方法。然后處理Handler,這個Handler相當于Struts中Action,在SpringMVC中默認的實現是Controller接口,是具體處理請求的代碼所駐留的地方。事實上HandlerExecutionChain中的getHandler()返回的是一個Object類型。DispatcherServlet不會直接調用getHandler()返回對象中的方法,DispatcherServlet會查找相應的HandlerAdapter,然后具體通過HandlerAdapter來調用getHandler()返回的handler對象中的方法。就是說我們可以實現自己的HandlerAdapter然后通過IoC注入到DispatcherServlet中,從而可以實現一套自定義的控制器。隨后DispatcherServlet會調用Interceptor中的postHandle()方法。
視圖的處理
DispatcherServlet會期望Hander返回一個ModelAndView,DispatcherServlet會根據所返回的ModelAndView對象所包含的信息進行視圖的渲染。起具體出來流程如下:
首先DispatcherServlet會根據LocaleResolver來識別請求中的Locale,開發人員可以自己實現LocaleResolver接口,然后通過IoC注入到DispatcherServlet中,然后DispatcherServlet會判斷ModelAndView中是否已經包含了接口View的具體實現,如果包含了,則直接調用View中的方法render(Map model, HttpServletRequest request, HttpServletResponse response)。如果不包含,則說明該ModelAndView只是包含了View的名稱引用,DispatcherServlet會調用ViewResolver中的resolveViewName(String viewName, Locale locale)來解析其真正的視圖。該方法會返回一個View的具體實現。
視圖的渲染
Spring支持多種視圖技術,其中比較常用的包括有Jstl視圖,Veloctiy視圖,FreeMarker視圖等。對Jstl視圖的渲染Spring是通過JstlView這個類具體實現的。事實上其最終的渲染是交給容器來做的,Spring只是通過RequestDispatcher實現了服務器內部請求的Forward。而對于模板視圖,如Veloctiy和FreeMarker等,Spring會初始化其相應的模板引擎,由模板引擎生成最終的Html頁面然后在合并到Response的輸出流中。
異常的處理
如果在Hander中處理請求是拋出異常,DispatcherServlet會查找HandlerExceptionResolver接口的具體實現,該接口定義了一個方法:
ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex),實現類需要實現該方法以便對異常進行處理,最后該方法需要返回一個ModelAndView。
SpringMVC的一些總結 靈活的Interceptor,通過Interceptor我們可以在一個請求處理前和請求處理完成之后做相應的操作,通過Interceptor機制,我們可以做authentication, logging, and filtering等。 良好的表單支持,在SpringMVC的Controller繼承體系結構中,其具體的子類對表單(Form)提供了良好的支持。能夠很好的支持單個表單的顯示、修改、提交操作。同時也提供了表單的分步提交。 可定制的數據綁定(Data Binding)。 多視圖技術的支持,SpringMVC同時支持Jstl, Velocity 等多中視圖技術,但是這同時也會引出一個問題,因為各種視圖技術都有自己的一套方法來處理國際化,例如Jstl和Velocity處理國際化的方式就很不相同。因此在多個視圖技術并存的應用中,國際化也是一個需要注意的問題。 其Handler(控制器)作為Bean定義在Spring容器中,因此能享受容器帶來的服務。 Handler(控制器)具有良好的可測試性。
|
posted @
2007-08-29 10:23 華夢行 閱讀(2954) |
評論 (0) |
編輯 收藏
-
public abstract class HttpServletBean
- extends HttpServlet
Simple extension of HttpServlet
which treats its config parameters (init-param
entries within the servlet
tag in web.xml
) as bean properties.
HttpServlet的簡單擴展用來處理 (init-param)in the web.xml
A handy superclass for any type of servlet. Type conversion of config parameters is automatic, with the corresponding setter method getting invoked with the converted value. It is also possible for subclasses to specify required properties. Parameters without matching bean property setter will simply be ignored.
This servlet leaves request handling to subclasses, inheriting the default behavior of HttpServlet (doGet
, doPost
, etc).
This generic servlet base class has no dependency on the Spring ApplicationContext
concept. Simple servlets usually don't load their own context but rather access service beans from the Spring root application context, accessible via the filter's ServletContext
(see WebApplicationContextUtils
).
The FrameworkServlet
class is a more specific servlet base class which loads its own application context. FrameworkServlet serves as direct base class of Spring's full-fledged DispatcherServlet
.
-
public abstract class FrameworkServlet
- extends HttpServletBean
- implements ApplicationListener
Base servlet for Spring's web framework. Provides integration with a Spring application context, in a JavaBean-based overall solution.
spring web Framework的基礎 servlet? ,提供在以javabean為基礎的整體解決方案已完成與spring應用上下文的集成
This class offers the following functionality:
1.管理一個servlet一個網絡應用上下文實例,這個servlet的配置由servlet命名空間里的bean來決定
2.根據請求處理發布事件,是否請求成功的被處理了
- Manages a
WebApplicationContext
instance per servlet. The servlet's configuration is determined by beans in the servlet's namespace.
- Publishes events on request processing, whether or not a request is successfully handled.
Subclasses must implement doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
to handle requests. Because this extends HttpServletBean
rather than HttpServlet directly, bean properties are automatically mapped onto it. Subclasses can override initFrameworkServlet()
for custom initialization.
因為它繼承自httpservletBean 所以bean的屬性已經被自動的裝配了,子類可以通過覆蓋initFrameworkServlet來定制初始化bean
Detects a "contextClass" parameter at the servlet init-param level, falling back to the default context class, XmlWebApplicationContext
, if not found. Note that, with the default FrameworkServlet, a custom context class needs to implement the ConfigurableWebApplicationContext
SPI.
Passes a "contextConfigLocation" servlet init-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "test-servlet.xml, myServlet.xml". If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the servlet.
Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files, at least when using Spring's default ApplicationContext implementation. This can be leveraged to deliberately override certain bean definitions via an extra XML file.
The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location with XmlWebApplicationContext). The namespace can also be set explicitly via the "namespace" servlet init-param.
posted @
2007-08-29 10:13 華夢行 閱讀(677) |
評論 (0) |
編輯 收藏
HeidiSQL
posted @
2007-08-28 15:54 華夢行 閱讀(124) |
評論 (0) |
編輯 收藏
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="cleanup"/>
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
posted @
2007-08-28 14:28 華夢行 閱讀(172) |
評論 (0) |
編輯 收藏
Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:
BeanFactory的實現應該盡可能支持標準的bean生命周期,以下是所以的方法的順序的依次列表
1. BeanNameAware's?? setBeanName
2. BeanClassLoaderAware's? setBeanClassLoader
3. BeanFactoryAware's?? setBeanFactory
4. ResourceLoaderAware's setResourceLoader
(only applicable when running in an application context)
5. ApplicationEventPublisherAware's setApplicationEventPublisher
(only applicable when running in an application context)
6. MessageSourceAware's setMessageSource
(only applicable when running in an application context)
7. ApplicationContextAware's setApplicationContext
(only applicable when running in an application context)
8. ServletContextAware's setServletContext
(only applicable when running in a web application context)
9. postProcessBeforeInitialization
methods of BeanPostProcessors
10. InitializingBean's afterPropertiesSet
11. a custom init-method definition
12. postProcessAfterInitialization
methods of BeanPostProcessors
?當關閉一個bean的時候
On shutdown of a bean factory, the following lifecycle methods apply:
1. DisposableBean's destroy
2. a custom destroy-method definition