首先准备开发需要的jar包,请到spring-framework-3.0.5.RELEASE-dependencies.zip和spring- framework-3.0.5.RELEASE-with-docs中查找如下jar包:
1.1 org.springframework.asm-3.0.5.RELEASE.jar
1.2 org.springframework.beans-3.0.5.RELEASE.jar
1.3 org.springframework.context-3.0.5.RELEASE.jar
1.4 org.springframework.core-3.0.5.RELEASE.jar
1.5 org.springframework.expression-3.0.5.RELEASE.jar
1.6 org.springframework.web.portlet-3.0.5.RELEASE.jar
1.7 org.springframework.web.servlet-3.0.5.RELEASE.jar
1.8 org.springframework.web-3.0.5.RELEASE.jar
1.9 commons-logging-1.1.1.jar :教程中没有引入这个包,我们这不必须引入,否者会报错。
注意: org.springframework.web.servlet-3.0.5.RELEASE.jar在后面的版本中也叫做:
org.springframework.web.webmvc-3.2.0.RELEASE.jar
如果不知道用哪些包,可以将spring的所有包都条件到项目中。
3.1 web.xml文件
3.2 spring mvc的配置文件,默认在WEB-INF下,叫做[servlet-name]-servlet.xml
3.3 控制器类文件
3.4 视图显示的jsp文件
4.1 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" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0"> <welcome-file-list > <welcome-file >/WEB-INF/ jsp/index.jsp</welcome-file > </welcome-file-list > <display-name >SpringMVC </display-name > <servlet > <servlet-name >dispatcher </servlet-name > <servlet-class >org.springframework.web.servlet.DispatcherServlet </servlet-class > <init-param > <param-name >contextConfigLocation </param-name > <param-value >/WEB-INF/dispatcher-servlet.xml </param-value > </init-param > <!-- load-on-startup:表示启动容器时初始化Servlet --> <load-on-startup >1 </load-on-startup > </servlet > <servlet-mapping > <servlet-name >dispatcher </servlet-name > <!-- url-pattern:表示哪些请求交给Spring Web MVC处理, "/" : 定义默认servlet的映射 "*. html":表示拦截所有以.html为扩展名的请求 --> <url-pattern >/</url-pattern > </servlet-mapping > </web-app>
4.2 dispatcher-servlet.xml 配置文件
<?xml version="1.0" encoding= "UTF-8"?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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/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"> <!-- JSR-303 support will be detected on classpath and enabled automatically --> <context:component-scan base-package= "org.springframework.samples.web.controller" /> <!-- 有了context:component-scan可以省略context:annotation-config --> <mvc:annotation-driven /> <bean id ="viewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > <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.3 控制器类文件
package org.springframework.samples.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/login") public class HelloWorldController { @RequestMapping("/index") public String springIndex(Model model) { model. addAttribute("message", "Hello indexS! Who are you ?"); return "index"; } @RequestMapping("/helloWorld") public String helloWorld(Model model) { model. addAttribute("message", "Hello World! Who are you ?"); return "helloWorld"; } }
4.4 默认显示的index.jsp
<%@ 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 >Hello World </title > </head > <body > <a href ="login/helloWorld"> login/helloWorld</ a> </body > </html>
4.5 spring mvc跳转的界面
<%@ 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 >Hello World </title > </head > <body > ${message} </body > </html>
到此,一个简单的SpringMVC的基础框架就已经搭建完成。希望开始学习SpringMVC的朋友能有个好的开头。
在后续业余时间中,还会继续更新SpringMVC的相关文章,包括参数传递,页面跳转路径等