SpringMVC学习笔记(二)

一.HandleMapping和HandlerAdapter的讲解

HandleMapping:处理映射器,可以理解为为请求的url查找对应的Controller类.

HandlerAdapter:可以简单的理解为调用Controller中的方法。

HandlerMapping+HnadlerAdapter:找到url所对应的Controller Method。

二.SpringMVC中的HandleMapping有三种.

BeanNameUrlHandlerMapping:根据bean的名称路径访问Controller类

SimpleUrlHandlerMapping:根据url找到对应的bean的id

ControllerClassNameHandlerMapping: 根据类的名称找到对应Controller类.

三.HandleMapping的三种示例.

工程结构图

SpringMVC学习笔记(二)

1.网页代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
<a href="simple.do">Simple Url Controller</a>
<br/>
<a href="index.do">BeanName Url Controller</a>
<br/>
<a href="myController.do">ControllerClassName Controller</a>
</body>
</html>

2.springMVC配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 讲解HandleMapping
1.BeanNameUrlHandlerMapping :根据url找到对应的bean name名
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean name="/index.do" class="com.eduask.ykq.controller.BeanNameController"></bean>
<!--
2.SimpleUrlHandlerMapping:
-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="simgle.do">a</prop>
</props>
</property>
</bean>
<bean id="a" class="com.eduask.ykq.controller.SimpleUrlController"></bean>
<!-- 3.ControllerClassNameHandleMapping: -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <bean class="com.eduask.ykq.controller.MyController"></bean> <!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

3.web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVC1</display-name>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- classpath:========>/web-inf/class/ (源文件下的文件经过编译后产生的字节码文件所在的文件夹) -->
<param-value>classpath:SpringMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
上一篇:SpringMVC学习笔记二第一个小的程序


下一篇:hello Kotlin!