这篇文章将教你快速地上手使用 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 命名为 ideawu, 所以我们在 web-inf 目录下建立一个名为 ideawu-servlet.xml 的文件:
ideawu-servlet.xml:
1 <?xml version="1.0" encoding="utf-8" ?>
2 <!doctype beans public "-//spring//dtd bean//en" "
http://www.springframework.org/dtd/spring-beans.dtd">
3
4 <beans>
5
6 <bean id="viewresolver" class="org.springframework.web.servlet.
_view.internalresourceviewresolver">
7 <property name="prefix" value="/web-inf/jsp/" />
8 <property name="suffix" value=".jsp" />
9 </bean>
10
11 <bean id="simpleurlhandlermapping"
class="org.springframework.web.servlet.handler.simpleurlhandlermapping">
12 <property name="mappings">
13 <props>
14 <prop key="/hello.do">hellocontroller</prop>
15 </props>
16 </property>
17 </bean>
18
19 <bean id="hellocontroller" class="com.ideawu.hellocontroller">
20 <!--
21 <property name="hellomanager" ref="hellomanager" />
22 -->
23 </bean>
24
25 </beans>
它配置了以下功能:
上面, 我们在 web.xml 文件中告诉 contextloaderlistener, 我们还有另外两个配置文件 /web-inf/database.xml 和 /web-inf/applicationcontext.xml.
applicationcontext.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <!doctype beans public "-//spring//dtd bean//en" "
http://www.springframework.org/dtd/spring-beans.dtd">
3
4 <beans>
5
6 <bean id="propertyconfigurer"
class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">
7 <property name="locations">
8 <list>
9 <value>/web-inf/jdbc.properties</value>
10 </list>
11 </property>
12 </bean>
13
14 </beans>
它配置了以下功能:
database.xml:
1 <?xml version="1.0" encoding="utf-8"?>
2 <!doctype beans public "-//spring//dtd bean//en" "
http://www.springframework.org/dtd/spring-beans.dtd">
3
4 <beans>
5
6 <!-- remove this if your database setting is fine.
7 <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
8 <property name="driverclassname" value="${jdbc.driverclassname}"/>
9 <property name="url" value="${jdbc.url}"/>
10 <property name="username" value="${jdbc.username}"/>
11 <property name="password" value="${jdbc.password}"/>
12 </bean>
13 -->
14
15 <!-- transaction manager for a single jdbc datasource
16 <bean id="transactionmanager"
class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
17 <property name="datasource" ref="datasource"/>
18 </bean>
19 -->
20
21 <!--
22 <bean id="attributemanager" class="com.ideawu.core.attributemanager">
23 <property name="datasource" ref="datasource"/>
24 </bean>
25 -->
26
27 </beans>
它配置了以下功能(不过,已经注释掉了):
jdbc.properties:
1 jdbc.driverclassname=com.mysql.jdbc.driver
2 jdbc.url=jdbc:mysql://localhost/test?useunicode=true&characterencoding=utf-8
3 jdbc.username=test
4 jdbc.password=12345
现在, 我们来编写 java 代码吧.
1 /***********************************************************
2 * date: 2006-8-26
3 * file: hellocontroller.java
4 * author: ideawu
5 ***********************************************************/
6
7 package com.ideawu;
8
9 import org.springframework.web.servlet.mvc.controller;
10 import org.springframework.web.servlet.modelandview;
11
12 import javax.servlet.http.httpservletrequest;
13 import javax.servlet.http.httpservletresponse;
14
15 /**
16 * @author ideawu
17 *
18 */
19 public class hellocontroller implements controller {
20 /*
21 private hellomanager hellomanager;
22
23 public void sethellomanager(hellomanager hellomanager) {
24 this.hellomanager = hellomanager;
25 }
26 */
27
28 public modelandview handlerequest(httpservletrequest request,
29 httpservletresponse response)throws exception{
30
31 request.setattribute("hello_1", "你好啊, spring!");
32 request.setattribute("hello_2", "hello world!");
33
34 return new modelandview("hello");
35 }
36
37 }
return new modelandview("hello"); 告诉 internalresourceviewresolver jsp 模板的名字叫作 hello. request.setattribute() 设置的对象我们可以在 jsp 文件中使用.
hello.jsp:
1 <%@ page contenttype="text/html; charset=utf-8" %>
2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "
http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <title>hello world!</title>
8 </head>
9 <body>
10
11 <h2>${hello_1}</h2>
12
13 <h2>${hello_2}</h2>
14
15 </body>
16 </html>
你可以下载整个 web 应用程序. 在 debian linux, tomcat 5.5.16, jdk1.5.0 下运行良好. 解压后得到一个 spring 文件夹, 放到你的 webapps 目录下, 在浏览器中输入 http://localhost:8080/spring/hello.do 就可以访问了。
新闻热点
疑难解答