< servlet >
< servlet-name > SpringMVCDemo servlet-name >
< servlet-class >
org.springframework.web.servlet.DispatcherServlet
servlet-class >
< load-on-startup > 1 load-on-startup >
servlet >
< servlet-mapping >
< servlet-name > SpringMVCDemo servlet-name >
< url-pattern > *.do url-pattern >
servlet-mapping >
门安装好了之后,我们需要定义门能打开的范围,在哪定义呢?就在门的上面贴上一张“入门指导”,介绍从哪方面(hello.do)进门需要使用何种交通方式(springmvcController)进去,并定义该交通方式。
< bean id ="urlMapping"
class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
< property name ="mappings" >
< props >
< prop key ="/hello.do" > springmvcController prop >
props >
property >
bean >
< bean id ="springmvcController"
class ="org.improviser.webapp.controller.SpringmvcController" >
bean >
接下来该渲染一下页面了,下面为渲染所有以
< 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/pages/ value >
property >
< property name ="suffix" >
< value > .jsp value >
property >
bean >
上面提到的SpringmvcController,现在该作个定义了,该类需要实现Controller接口。
public class SpringmvcController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map model = new HashMap();
String str = "即兴诗人";
logger.info("returning hello view with " + currTime);
model.put("coldice", str);
return new ModelAndView("hello", "message", model);
}
编写jsp测试页面。
<html>
<head>
<title>Spring Applicationtitle>
head>
<body>
<h1>
<br />
Hello
<c:out value="${message.coldice}" />
Spring MVC 测试成功!
h1>
body>
html>
现在对以上内容作个总结,正如上面所叙述,SpringMVC框架的处理过程机制如下图所示,接收请求页面,通过DispatcherServlet映射到相应规则处理,并找到相应的控制器,结果控制器处理后返回相应的页面最后返回给用户。