Spring MVC 3.0 深入

时间:2023-03-09 19:31:39
Spring MVC 3.0 深入

核心原理:

.    用户发送请求给服务器。url:user.do
. 服务器收到请求。发现DispatchServlet可以处理。于是调用DispatchServlet。
. DispatchServlet内部,通过HandleMapping检查这个url有没有对应的Controller。如果有,则调用Controller。
. Controller开始执行。
. Controller执行完毕后,如果返回字符串,则ViewResolver将字符串转化成相应的视图对象;如果返回ModelAndView对象,该对象本身就包含了视图对象信息。
. DispatchServlet将执视图对象中的数据,输出给服务器。
. 服务器将数据输出给客户端。

spring3.0中相关jar包的含义:

org.springframework.aop-3.0..RELEASE.jar           spring的aop面向切面编程
org.springframework.asm-3.0..RELEASE.jar spring独立的asm字节码生成程序
org.springframework.beans-3.0..RELEASE.jar        IOC的基础实现
org.springframework.context-3.0..RELEASE.jar    IOC基础上的扩展服务
org.springframework.core-3.0..RELEASE.jar       spring的核心包
org.springframework.expression-3.0..RELEASE.jar spring的表达式语言
org.springframework.web-3.0..RELEASE.jar        web工具包
org.springframework.web.servlet-3.0..RELEASE.jar mvc工具包

小demo:

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">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

WEB-INF下的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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.sxt.web"/> <!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
<!-- 如果使用jstl的话,配置下面的属性 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>

java文件的代码:

package com.sxt.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
@RequestMapping(value = "/user")
public class UserController { @RequestMapping(value = "/reg")
public String reg(@RequestParam(value = "uname") String uname) {
System.out.println("HelloController.handleRequest()");
return "index";
} @RequestMapping(value = "/page")
public String page(@RequestParam(value = "page") String page, Model model) {
System.out.println("////////////////////" + page);
model.addAttribute("param", "success");
return page;
}
}

jar包我将所有的spring的lib下的jar都放进去了,如果发现classNotFound的话,挨个添加补充就行了。