springMVC学习笔记一(hello world)

===========================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:[email protected]">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:[email protected]">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返加响应给用户,结束


参考:http://jinnianshilongnian.iteye.com/blog/1752171 

你可能感兴趣的:(springMVC学习笔记一(hello world))