Spring 发布时所需的最小包依赖
Copy the following files into the library directory:
- spring.jar
- commons-logging.jar
- servlet-api.jar
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<!–
– Web app deployment descriptor that just loads an "example" DispatcherServlet
– with a specific context.
–
– Depends on the following libraries in WEB-INF/lib:
– * spring.jar
– * commons-logging.jar
–>
<web-app>
<!–
– Servlet that dispatches request to registered handlers (Controller implementations).
– Has its own application context, by default defined in "{servlet-name}-servlet.xml",
– i.e. "example-servlet.xml".
–
– A web app can contain any number of such servlets.
– Note that this web app does not have a shared root application context,
– therefore the DispatcherServlet contexts do not have a common parent.
–>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!–
– Maps the example dispatcher to /example/*. All handler mappings in
– example-servlet.xml will by default be applied to this subpath.
– If a mapping isn’t a /* subpath, the handler mappings are considered
– relative to the web app root.
–
– A single dispatcher could get mapped to multiple paths, like any servlet.
–>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
example-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!–
– Application context definition for "example" DispatcherServlet.
–>
<beans>
<!–
– Due to the dispatcher mapping in web.xml and the default BeanNameUrlHandlerMapping
– in this context, "http://localhost/example/test" will trigger this controller.
–
– Due to the default InternalResourceViewResolver, returned view names get treated
– as internal resources, e.g. JSPs: "/test.jsp" or "/WEB-INF/jsp/test.jsp" will
– render the respective file. So with the default resolver, controllers have to
– specify the whole resource path as view name (see below for an alternative).
–
– HandlerMapping and ViewResolver are simple interfaces: Choose one of Spring’s
– provided implementations, or write your own one.
–
– A very simple Controller implementation could look as follows:
–
- public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
- return new ModelAndView("/test.jsp");
- }
–
– Or for writing the response directly:
–
- public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
- response.getWriter().write("This is a test");
- return null;
- }
–>
<bean name="/test" class="example.ExampleController"/>
<!–
– Optional: Explicit InternalResourceViewResolver definition,
– specifying view class, name prefix, and name suffix.
–
– Specifying the JstlView class instead of the default InternalResourceView
– exports Spring’s message source and the current user locale as standard
– JSTL attributes, to be leveraged by JSTL fmt tags.
–
– A prefix/suffix combination allows for symbolic view names to be returned
– by controllers, still getting straightly mapped to internal resources.
– Example: view name "test" -> resource "/WEB-INF/jsp/test.jsp".
–
– For more sophisticated mappings, consider ResourceBundleViewResolver
– (see "webapp-typical" skeleton) or XmlViewResolver.
–>
<!–
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
–>
</beans>

