1.继承AbstractController

 1 package com.xzl.test;
 2 import java.util.Date;
 3 
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 
 7 import org.springframework.web.servlet.ModelAndView;
 8 import org.springframework.web.servlet.mvc.AbstractController;
 9 
10 public class SimpleController extends AbstractController{
11     public ModelAndView handleRequestInternal(HttpServletRequest request,
12             HttpServletResponse response) 
13           throws Exception {
14             
15             // create a model-and-view using 'welcome’
16             ModelAndView mav = new ModelAndView("welcome");
17             // then, add an object to it and return it
18             mav.addObject("date"new Date());
19             return mav;
20           }
21 }
2.dispatcher-servlet.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3 
 4 <beans>
 5     <bean name="simpleController"
 6     class="com.xzl.test.SimpleController"/>
 7    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 8         <property name="mappings">
 9             <props>
10                 <prop key="/welcome.c">simpleController</prop>
11             </props>
12         </property>
13     </bean>
14      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15         <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
16         <property name="prefix"><value>/</value></property>
17         <property name="suffix"><value>.jsp</value></property>
18     </bean>
19 </beans>
20 

3.web.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
 3 <web-app>
 4     <servlet>
 5   <servlet-name>dispatcher</servlet-name>
 6   <servlet-class>
 7     org.springframework.web.servlet.DispatcherServlet
 8   </servlet-class>
 9   <load-on-startup>1</load-on-startup>
10 </servlet>
11 
12 
13 
14 <servlet-mapping>
15   <servlet-name>dispatcher</servlet-name>
16   <url-pattern>*.c</url-pattern>
17 </servlet-mapping>
18 
19     <taglib> 
20         <taglib-uri>c</taglib-uri> 
21         <taglib-location>/WEB-INF/c.tld</taglib-location> 
22     </taglib>
23 </web-app>
24 

4.welcome.jsp

 1 <%-- welcome.jsp --%>
 2 <%@ page session="false"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
 4 <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> 
 5 <html>
 6   <head>
 7     <title>Hello world!</title>
 8   </head>
 9   <body>
10     <h1>Hello world</h1>
11     <p>
12       Right now, the time is <c:out value="${date}" />
13       .
14     </p>
15   </body>
16 </html>
17 

5.build.xml

 1 <?xml version="1.0"?>
 2 
 3 <project name="SpringMVC" default="deploy">
 4     <property name="src.dir" value="src" />
 5     <property name="web.dir" value="WebRoot" />
 6     <property name="build.dir" value="${web.dir}/WEB-INF/classes" />
 7     <!-- TOMCAT HOME DIR -->
 8     <property name="tomcat.home" value="D:\Program Files\Apache Software Foundation\Tomcat 5.0" />
 9     <property name="deploy.dir" value="${tomcat.home}/webapps/SpringTest" />
10 
11     <target name="build">
12         <mkdir dir="${build.dir}"/>
13         <javac srcdir="${src.dir}" destdir="${build.dir}" target="1.4">
14             <classpath>
15                 <fileset file="${web.dir}/WEB-INF/lib/*.jar" />
16                 <fileset dir="${tomcat.home}/common/lib">
17                     <include name="servlet*.jar"/>
18                 </fileset>
19             </classpath>
20         </javac>
21     </target>
22 
23     <target name="deploy" depends="build">
24         <mkdir dir="${deploy.dir}"/>
25         <copy todir="${deploy.dir}">
26             <fileset dir="${web.dir}">
27                 <include name="**/*.*" />
28             </fileset>
29         </copy>
30     </target>
31 </project>
32 
33 

6.先ANT,然后http://localhost:8080/SpringMVC/welcome.c
7.结果:

Hello world

Right now, the time is Wed Jan 04 10:14:01 GMT 2006 .


 



ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程