===========================hello world=================
1 所用jar包
spring框架3.3.m1:
log日誌commons.logging-1.1.1.jar;
jstl標籤庫jstl-1.1.2.jar 和 standard-1.1.2.jar
2 前端控制器的配置,在web.xml中添加以下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringMVC</display-name>
<!-- 配置DispatcherServlet -->
<!-- 方式一 -->
<servlet>
<!-- 名稱,有自已的webApplicationContext -->
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--攔載/*請求 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3 spring的配置文件SpringMVC-servlet.xml
默認 DispatcherServlet 會加載 WEB-INF/[DispatcherServlet 的 Servlet 名 字 ]-servlet.xml 配 置 文 件
示例代碼以下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- id 表示你這個組件的名字,class表示組件類 -->
<bean id="hello" name="/hello" class="cn.yue.mvc.controller.HelloWorldController" />
<!-- HandlerMapping -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- HandlerAdapter -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- ViewResolver 用於支持servlet jsp視圖 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 頁面使用jstl標籤庫 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 查找視圖頁面的前綴和後綴 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4 頁面控制器
/**
* Copyright (C) 2014
*
* FileName:HelloWorldController.java
*
* Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
* CreateTime: 2014-7-22
*/
// Package Information
package cn.yue.mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* 頁面處理器
*
* @version
*
* @Description:
*
* @author <a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
* @since 2014-7-22
*
*/
public class HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
/**
* 收集參數,驗證參數
*
* 綁定參數到命令對象
*
* 將命令對象傳入業務對象進行業務處理
*
* 選擇下一個頁面
*/
ModelAndView mv = new ModelAndView();
mv.addObject("message", "hello world!");
mv.setViewName("hello");
return mv;
}
}
6 jsp頁面
<%@ page language="java" pageEncoding="UTF-8"
contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>${message}
</body>
</html>
運行過程:
1 客戶端發送請求,/hello---->DispatcherServlet進行處理
2 DispatcherServlet-->BeanNameUrlHandlerMapping處理映射-->
由"/hello"的bean進行處理,即HelloWorldController
3 DispatcherServlet-->SimpleControllerHandlerAdapter-->
將處理器HelloWorldController適配爲SimpleControllerHandlerAdapter
4 SimpleControllerHandlerAdapter-->HelloWorldController-->
handlerRequest方法進行處理-->返回一個modelAndView給dispatcherServlet
5 hello(視圖名)--->InternalResourceViewResolver-->JstlView(/WEB-INF/jsp/hello.jsp)
6 jstlView-->將傳入的數據模型數據在視圖中展現出來
7 dispatcherServlet返加響應給用戶,結束html
參考:http://jinnianshilongnian.iteye.com/blog/1752171 前端