国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Spring MVC 開發(fā)快速入門

2024-07-21 02:14:35
字體:
供稿:網(wǎng)友

  這篇文章將教你快速地上手使用 spring 框架,如果你手上有一本《spring in action》, 那么你最好從第三部分"spring 在 web 層的應(yīng)用--建立 web 層"開始看, 否則那將是一場(chǎng)惡夢(mèng)!

  首先, 我需要在你心里建立起 spring mvc 的基本概念. 基于 spring 的 web 應(yīng)用程序接收到 http://localhost:8080/hello.do(事實(shí)上請(qǐng)求路徑是 /hello.do) 的請(qǐng)求后, spring 將這個(gè)請(qǐng)求交給一個(gè)名為 hellocontroller 的程序進(jìn)行處理, hellocontroller 再調(diào)用 一個(gè)名為 hello.jsp 的 jsp 文件生成 html 代碼發(fā)給用戶的瀏覽器顯示. 上面的名稱(/hello.do, hellocontroller, hello.jsp) 都是變量, 你可以更改.

  在 spring mvc 中, jsp 文件中盡量不要有 java 代碼, 只有 html 代碼和"迭代(foreach)"與"判斷(if)"兩個(gè)jstl標(biāo)簽. jsp 文件只作為渲染(或稱為視圖 view)模板使用.

  好了, 我們開始吧. 首先我們需要一個(gè)放在 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-namecontextconfiglocation</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-classorg.springframework.web.context.contextloaderlistener</listener-class>
18 </listener>
19
20 filter
21 filter-nameencodingfilter</filter-name>
22 filter-classorg.springframework.web.filter.characterencodingfilter</filter-class>
23 init-param
24 param-nameencoding</param-name>
25 param-valueutf-8</param-value>
26 </init-param>
27 </filter>
28
29 filter-mapping
30 filter-nameencodingfilter</filter-name>
31 url-pattern*.do</url-pattern>
32 </filter-mapping>
33
34 servlet
35 servlet-nameideawu</servlet-name>
36 servlet-classorg.springframework.web.servlet.dispatcherservlet</servlet-class>
37 load-on-startup1</load-on-startup>
38 </servlet>
39
40 servlet-mapping
41 servlet-nameideawu</servlet-name>
42 url-pattern*.do</url-pattern>
43 </servlet-mapping>
44
45 welcome-file-list
46 welcome-fileindex.jsp</welcome-file>
47 welcome-fileindex.html</welcome-file>
48 </welcome-file-list>
49
50 jsp-config
51 taglib
52 taglib-urihttp://java.sun.com/jsp/jstl/core</taglib-uri>
53 taglib-location/web-inf/tld/c.tld</taglib-location>
54 </taglib>
55 taglib
56 taglib-urihttp://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 標(biāo)簽), 它是一個(gè) java servlet 程序. 我們將它命名為 ideawu. 然后我們?cè)倥渲?servlet 映射(servlet-mapping 標(biāo)簽), 也就是你希望哪些請(qǐng)求被dispatcherservlet處理. 這里, 我們?cè)O(shè)置后綴名為 do(*.do) 的所有url請(qǐng)求都被名為 ideawu 的 dispatcherservlet 的程序處理. 選擇 .do 只是一個(gè)習(xí)慣,但是你不要選擇 .html! 雖然《spring in action》選擇了 .html, 但是那是一種非常糟糕的作法, 特別是你整合 apachetomcat 的時(shí)候.
  • 配置 characterencodingfilter (filter 標(biāo)簽), 否則你會(huì)發(fā)現(xiàn)中文亂碼. 因?yàn)槲业?jsp 和 html 文件都是 utf-8 編碼的, 所以我在 param-value 標(biāo)簽中設(shè)置了 utf-8. 估計(jì)你使用的是 gb2312 或者 gbk, 立即轉(zhuǎn)到 utf-8 上來吧.
  • 分解配置文件. context-param 標(biāo)簽指明我們的配置文件還有 /web-inf/database.xml 和 /web-inf/applicationcontext.xml. contextloaderlistener(listener 標(biāo)簽) 由此得知配置文件是哪些, 它會(huì)將它們載入.

  因?yàn)槲覀儗?dispatcherservlet 命名為 ideawu, 所以我們?cè)?web-inf 目錄下建立一個(gè)名為 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>

  它配置了以下功能:

  • 配置 internalresourceviewresolver, 它是 jsp 渲染模板的處理器. 如果你告訴 internalresourceviewresolver 處理一個(gè)名為 hello 的模板時(shí), 它會(huì)渲染 /web-inf/jsp/hello.jsp 文件. 把 jsp 文件放到 /web-inf/jsp/ 目錄下是被鼓勵(lì)的, 這樣可以防止用戶不經(jīng)過 controller 直接訪問 jsp 文件從而出錯(cuò)(有些頑皮的人很喜歡這樣做).
  • 配置 simpleurlhandlermapping, 在上面的配置文件中, /hello.do 的請(qǐng)求將被 hellocontroller 處理. "/hello.do"和"hellocontroller" 是變量, 你可以更改. 但是你注意到了嗎, hello.do 以 .do 作為后綴名. 如果這里(本文的條件下)你 不使用.do 作為后綴名, 就沒有程序來處理這個(gè)請(qǐng)求了. 因?yàn)?dispatcherservlet 將收到的請(qǐng)求轉(zhuǎn)交給 simpleurlhandlermapping, dispatcherservlet 收不到的請(qǐng)求, simpleurlhandlermapping 當(dāng)然也收不到了. 你可以在 props 標(biāo)簽內(nèi)配置多個(gè) prop 標(biāo)簽.
  • 我們將在后面編寫 com.ideawu.hellocontroller 類.

  上面, 我們?cè)?web.xml 文件中告訴 contextloaderlistener, 我們還有另外兩個(gè)配置文件 /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>

  它配置了以下功能:

  • 讀取 /web-inf/jdbc.properties 文件. 你可以在 list 標(biāo)簽中配置多個(gè) value 標(biāo)簽.

  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>

  它配置了以下功能(不過,已經(jīng)注釋掉了):

  • 配置數(shù)據(jù)庫連接. 類似${jbbc.url}是一種訪問變量的方法. 我們可以從 /web-inf/jdbc.properties 中找到這個(gè)變量的值. 如果你的數(shù)據(jù)庫已經(jīng)配置好, 就將第一個(gè)注釋去掉.

  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

  現(xiàn)在, 我們來編寫 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() 設(shè)置的對(duì)象我們可以在 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 titlehello world!</title
8 </head
9 body
10
11 h2${hello_1}</h2
12
13 h2${hello_2}</h2
14
15 </body
16 </html

  你可以下載整個(gè) web 應(yīng)用程序. 在 debian linux, tomcat 5.5.16, jdk1.5.0 下運(yùn)行良好. 解壓后得到一個(gè) spring 文件夾, 放到你的 webapps 目錄下, 在瀏覽器中輸入 http://localhost:8080/spring/hello.do 就可以訪問了。



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 林西县| 青神县| 松江区| 电白县| 山丹县| 古浪县| 白水县| 长海县| 白水县| 革吉县| 无锡市| 海口市| 惠州市| 渝北区| 五家渠市| 依兰县| 永修县| 靖宇县| 松原市| 南和县| 临汾市| 祁连县| 巴青县| 小金县| 岳西县| 库尔勒市| 桓台县| 土默特右旗| 德江县| 仙桃市| 久治县| 乌海市| 中阳县| 同德县| 永寿县| 宁河县| 泸州市| 徐州市| 贡山| 澄迈县| 桐乡市|