下面开始开发Spring MVC应用程序
(2)修改WEB-INF目录的web.xml
定义DispatcherServlet来控制所有的请求,同时具有一个标准的servlet-mapping映射到*.htm URL模式
<
servlet
>
< servlet - name > springapp </ servlet - name >
< servlet - class > org.springframework.web.servlet.DispatcherServlet </ servlet - class >
< load - on - startup > 1 </ load - on - startup >
</ servlet >
< servlet - mapping >
< servlet - name > springapp </ servlet - name >
< url - pattern > * .htm </ url - pattern >
</ servlet - mapping >
< servlet - name > springapp </ servlet - name >
< servlet - class > org.springframework.web.servlet.DispatcherServlet </ servlet - class >
< load - on - startup > 1 </ load - on - startup >
</ servlet >
< servlet - mapping >
< servlet - name > springapp </ servlet - name >
< url - pattern > * .htm </ url - pattern >
</ servlet - mapping >
创建springapp-servlet.xml(应用程序名--servlet.xml),配置DispatcherServlet要使用的Web应用程序context
定义名为springappController的Bean条目,指向SpringappController控制器
使用SimpleUrlHandlerMapping来定义URL映射,将/hello.htm映射到SpringappController控制器
<?
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 " springapp " DispatcherServlet.
-->
< beans >
< bean id = " springappController " class = " SpringappController " />
< bean id = " urlMapping " class = " org.springframework.web.servlet.handler.SimpleUrlHandlerMapping " >
< property name = " mappings " >
< props >
< prop key = " /hello.htm " > springappController </ prop >
</ props >
</ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " >
<!--
- Application context definition for " springapp " DispatcherServlet.
-->
< beans >
< bean id = " springappController " class = " SpringappController " />
< bean id = " urlMapping " class = " org.springframework.web.servlet.handler.SimpleUrlHandlerMapping " >
< property name = " mappings " >
< props >
< prop key = " /hello.htm " > springappController </ prop >
</ props >
</ property >
</ bean >
</ beans >
(8)拷贝jars到WEB-INF/lib目录
将dist/spring.jar、lib/jakarta-commons/commons-logging.jar、lib/log4j/log4j-1.2.8.jar文件导入到springapp工程的springapp/WEB-INF/lib目录下
(9)创建控制器
实现Controller接口的handleRequest()方法来处理请求
这里添加日志记录,检查是否执行到这里
handleRequest()方法返回的ModelAndView没有指定Model,所以直接重定向到指定的视图hello.jsp
import
org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** */ /** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());[来源 www.iocblog.net]
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info( " SpringappController - returning hello view " );
return new ModelAndView( " hello.jsp " );
}
}
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SpringappController implements Controller {
/** */ /** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());[来源 www.iocblog.net]
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info( " SpringappController - returning hello view " );
return new ModelAndView( " hello.jsp " );
}
}
(10)创建视图hello.jsp
<
html
>
< head >< title > Example :: Spring Application </ title ></ head >
< body >
< h1 > Hello - Spring Application </ h1 >
< p > Greetings. </ p >
</ body >
</ html >
< head >< title > Example :: Spring Application </ title ></ head >
< body >
< h1 > Hello - Spring Application </ h1 >
< p > Greetings. </ p >
</ body >
</ html >