| Spring MVC 开发快速入门 |
|
| |
|
|
这篇文章将教你快速地上手使用 Spring 框架,如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦!
首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController 的程序进行处理, helloController 再调用 一个名为 hello.jsp 的 jsp 文件生成 HTML 代码发给用户的浏览器显示. 上面的名称(/hello.do, helloController, hello.jsp) 都是变量, 你可以更改.
在 Spring MVC 中, jsp 文件中尽量不要有 Java 代码, 只有 HTML 代码和"迭代(forEach)"与"判断(if)"两个jstl标签. jsp 文件只作为渲染(或称为视图 View)模板使用.
好了, 我们开始吧. 首先我们需要一个放在 WEB-INF 目录下的 web.xml 文件:
web.xml: 1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 7 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value> 11 /WEB-INF/database.xml 12 /WEB-INF/applicationContext.xml 13 </param-value> 14 </context-param> 15 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 20 <filter> 21 <filter-name>encodingFilter</filter-name> 22 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 23 <init-param> 24 <param-name>encoding</param-name> 25 <param-value>UTF-8</param-value> 26 </init-param> 27 </filter> 28 29 <filter-mapping> 30 <filter-name>encodingFilter</filter-name> 31 <url-pattern>*.do</url-pattern> 32 </filter-mapping> 33 34 <servlet> 35 <servlet-name>ideawu</servlet-name> 36 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 37 <load-on-startup>1</load-on-startup> 38 </servlet> 39 40 <servlet-mapping> 41 <servlet-name>ideawu</servlet-name> 42 <url-pattern>*.do</url-pattern> 43 </servlet-mapping> 44 45 <welcome-file-list> 46 <welcome-file>index.jsp</welcome-file> 47 <welcome-file>index.html</welcome-file> 48 </welcome-file-list> 49 50 <jsp-config> 51 <taglib> 52 <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> 53 <taglib-location>/WEB-INF/tld/c.tld</taglib-location> 54 </taglib> 55 <taglib> 56 <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri> 57 <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location> 58 </taglib> 59 </jsp-config> 60 61 </web-app>
它配置了以下功能:
- 配置 DispatcherServlet (servlet 标签), 它是一个 Java Servlet 程序. 我们将它命名为 ideawu. 然后我们再配置 Servlet 映射(servlet-mapping 标签), 也就是你希望哪些请求被DispatcherServlet处理. 这里, 我们设置后缀名为 do(*.do) 的所有URL请求都被名为 ideawu 的 DispatcherServlet 的程序处理. 选择 .do 只是一个习惯,但是你不要选择 .html! 虽然《Spring in Action》选择了 .html, 但是那是一种非常糟糕的作法, 特别是你整合 Apache 和 Tomcat 的时候.
- 配置 CharacterEncodingFilter (filter 标签), 否则你会发现中文乱码. 因为我的 jsp 和 html 文件都是 UTF-8 编码的, 所以我在 param-value 标签中设置了 UTF-8. 估计你使用的是 GB2312 或者 GBK, 立即转到 UTF-8 上来吧.
- 分解配置文件. context-param 标签指明我们的配置文件还有 /WEB-INF/database.xml 和 /WEB-INF/applicationContext.xml. ContextLoaderListener(listener 标签) 由此得知配置文件是哪些, 它会将它们载入.
因为我们将 DispatcherServlet 命名为 ideawu, 所以我们在 WEB-INF 目录下建立一个名为 ideawu-servlet.xml 的文件:
ideawu-servlet.xml: 1 <?xml version="1.0" encoding="UTF-8" ?>
[1] [2] [3] 下一页
 |
频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。
| 原始作者:佚名 |
录入时间:2006-12-5 3:05:06 |
| 信息来源:不详 |
投稿信箱:itqoo@126.com |
|
|
 |
|
|
|
| 教程录入:itqoo 责任编辑:itqoo |
|
上一个教程: 深入Spring MVC framework之总体分析
下一个教程: Java-JSTL(JSP标准标签库)介绍 |
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |