Spring MVC之基于配置的Hello World

      废话不多说,直接进入经典的Hello World启动模式~~

1、web.xml

      项目总的配置文件,这个不能少:      

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  
  <!-- 配置前端控制器 -->
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
  </servlet>
  	
  	<!-- 映射规则 -->
  	<servlet-mapping>
  		<servlet-name>dispatcherServlet</servlet-name>
  		<url-pattern>*.do</url-pattern>
  	</servlet-mapping>
  	
<!-- Character Encoding filter -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
      说明:前端控制器的servlet名字为dispatcherServlet,对应的class为org.springframework.web.servlet.DispatcherServlet,这是spring mvc的核心分发控制器。注意,当请求交给spring mvc框架处理后,会加载spring mvc的配置文件,默认会加载WEB-INF/[前端控制器的servlet名字]-servlet.xml文件。

      因此上述配置对应的文件为WEB-INF/dispatcherServlet-servlet.xml。

      对了,处理的请求是以.do结尾的请求。

2、dispatcherServlet-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:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	
	<!-- 配置请求映射关系 -->
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="helloworld.do">myController</prop>
			</props>
		</property>
	</bean>
	
	<!-- 配置控制器 -->
	<bean id="myController" class="cn.com.yy.controller.HelloWorldController"/>
	
	<!-- 配置视图解析器  -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		<property name="prefix" value="/test/"/>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>
 

3、HelloWorldController.java

package cn.com.yy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 控制器
 * @author yy
 *
 */
public class HelloWorldController implements Controller {

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//定义ModelAndView对象
		ModelAndView mv = new ModelAndView();
		//添加模型数据
		mv.addObject("msg", "Hello,Welcome to The Spring MVC World!");
		//设置逻辑试图名称,视图解析器会根据此名称解析到具体的视图页面
		mv.setViewName("helloWorld"); //跳转到 WEB-INF/test/helloWorld.jsp
		return mv;
	}

}

4、前端视图helloWorld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'hello.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
   		${msg}
  </body>
</html>

5、结果

    Spring MVC之基于配置的Hello World_第1张图片


       运行步骤:

      (1)请求----》DispatcherServlet

                当用户发送http://localhost:8080/HelloWorld/helloworld.do请求时,根据url-pattern映射到名称为dispatcherServlet对应的servlet进行处理

      (2)DispatcherServlet----》SimpleUrlHandlerMapping

                根据helloworld.do会查找SimpleUrlHandlerMapping对应的myController控制器

      (3)SimpleUrlHandlerMapping----》HelloWorldController

                调用控制器的handleRequest方法进行处理:声明一个ModelAndView对象,添加POJO,设置视图名称。

      (4)调用视图解析器UrlBasedViewResolver

                根据视图名称helloWorld调用视图解析器,找到对应的视图/WEB-INF/test/helloWorld.jsp

      (5)使用View之JstlView进行渲染

                 将处理器出入的模型数据msg在视图中展示。

  

                over~~


你可能感兴趣的:(spring,spring,mvc,mvc,示例)