7.Spring Web MVC 框架

1.Spring Web MVC 框架

MVC 框架教程

Spring web ? ? ? ? MVC 框架提供了模型-視圖-控制的體系結(jié)構(gòu)和可以用來(lái)開發(fā)靈活、松散耦合的 web 應(yīng)用程序的組件攀操。MVC 模式導(dǎo)致了應(yīng)用程序的不同方面(輸入邏輯仆救、業(yè)務(wù)邏輯和 UI 邏輯)的分離,同時(shí)提供了在這些元素之間的松散耦合。

模型封裝了應(yīng)用程序數(shù)據(jù)恐疲,并且通常它們由 POJO 組成。

視圖主要用于呈現(xiàn)模型數(shù)據(jù)套么,并且通常它生成客戶端的瀏覽器可以解釋的 HTML 輸出培己。

控制器主要用于處理用戶請(qǐng)求,并且構(gòu)建合適的模型并將其傳遞到視圖呈現(xiàn)胚泌。

DispatcherServlet

Spring Web 模型-視圖-控制(MVC)框架是圍繞?DispatcherServlet?設(shè)計(jì)的省咨,DispatcherServlet?用來(lái)處理所有的 HTTP 請(qǐng)求和響應(yīng)。Spring Web MVC?DispatcherServlet?的請(qǐng)求處理的工作流程如下圖所示:

下面是對(duì)應(yīng)于?DispatcherServlet?傳入 HTTP 請(qǐng)求的事件序列:

收到一個(gè) HTTP 請(qǐng)求后诸迟,DispatcherServlet?根據(jù)?HandlerMapping?來(lái)選擇并且調(diào)用適當(dāng)?shù)?i>控制器茸炒。

控制器接受請(qǐng)求,并基于使用的 GET 或 POST 方法來(lái)調(diào)用適當(dāng)?shù)?service 方法阵苇。Service 方法將設(shè)置基于定義的業(yè)務(wù)邏輯的模型數(shù)據(jù),并返回視圖名稱到?DispatcherServlet?中感论。

DispatcherServlet?會(huì)從?ViewResolver?獲取幫助绅项,為請(qǐng)求檢取定義視圖。

一旦確定視圖比肄,DispatcherServlet?將把模型數(shù)據(jù)傳遞給視圖快耿,最后呈現(xiàn)在瀏覽器中。

上面所提到的所有組件芳绩,即 HandlerMapping掀亥、Controller 和 ViewResolver 是?WebApplicationContext?的一部分,而?WebApplicationContext?是帶有一些對(duì) web 應(yīng)用程序必要的額外特性的?ApplicationContext?的擴(kuò)展妥色。

需求的配置

你需要映射你想讓?DispatcherServlet?處理的請(qǐng)求搪花,通過(guò)使用在?web.xml?文件中的一個(gè) URL 映射。下面是一個(gè)顯示聲明和映射?HelloWeb?DispatcherServlet?的示例:

web.xml?文件將被保留在你的應(yīng)用程序的?WebContent/WEB-INF?目錄下嘹害。好的撮竿,在初始化?HelloWeb?DispatcherServlet?時(shí),該框架將嘗試加載位于該應(yīng)用程序的?WebContent/WEB-INF?目錄中文件名為?[servlet-name]-servlet.xml?的應(yīng)用程序內(nèi)容笔呀。在這種情況下幢踏,我們的文件將是?HelloWeb-servlet.xml

接下來(lái)许师,?標(biāo)簽表明哪些 URLs 將被 DispatcherServlet 處理房蝉。這里所有以?.jsp?結(jié)束的 HTTP 請(qǐng)求將由?HelloWeb?DispatcherServle t處理僚匆。

如果你不想使用默認(rèn)文件名?[servlet-name]-servlet.xml?和默認(rèn)位置?WebContent/WEB-INF,你可以通過(guò)在 web.xml 文件中添加 servlet 監(jiān)聽器?ContextLoaderListener?自定義該文件的名稱和位置搭幻,如下所示:

<web-app...>....<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/HelloWeb-servlet.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>

現(xiàn)在咧擂,檢查?HelloWeb-servlet.xml?文件的請(qǐng)求配置,該文件位于 web 應(yīng)用程序的?WebContent/WEB-INF?目錄下:

? 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">

以下是關(guān)于?HelloWeb-servlet.xml?文件的一些要點(diǎn):

[servlet-name]-servlet.xml?文件將用于創(chuàng)建 bean 定義粗卜,重新定義在全局范圍內(nèi)具有相同名稱的任何已定義的 bean屋确。

標(biāo)簽將用于激活 Spring MVC 注釋掃描功能,該功能允許使用注釋续扔,如 @Controller 和 @RequestMapping 等等攻臀。

InternalResourceViewResolver?將使用定義的規(guī)則來(lái)解決視圖名稱。按照上述定義的規(guī)則纱昧,一個(gè)名稱為?hello?的邏輯視圖將發(fā)送給位于?/WEB-INF/jsp/hello.jsp?中實(shí)現(xiàn)的視圖刨啸。

下一節(jié)將向你展示如何創(chuàng)建實(shí)際的組件,例如控制器识脆,模式和視圖设联。

定義控制器

DispatcherServlet 發(fā)送請(qǐng)求到控制器中執(zhí)行特定的功能。@Controller?注釋表明一個(gè)特定類是一個(gè)控制器的作用灼捂。@RequestMapping?注釋用于映射 URL 到整個(gè)類或一個(gè)特定的處理方法离例。

@Controller@RequestMapping("/hello")publicclassHelloController{@RequestMapping(method = RequestMethod.GET)publicStringprintHello(ModelMap model){? ? ? model.addAttribute("message","Hello Spring MVC Framework!");return"hello";? }}

@Controller?注釋定義該類作為一個(gè) Spring MVC 控制器。在這里悉稠,第一次使用的?@RequestMapping?表明在該控制器中處理的所有方法都是相對(duì)于?/hello?路徑的宫蛆。下一個(gè)注釋?@RequestMapping(method = RequestMethod.GET)?用于聲明?printHello()?方法作為控制器的默認(rèn) service 方法來(lái)處理 HTTP GET 請(qǐng)求。你可以在相同的 URL 中定義其他方法來(lái)處理任何 POST 請(qǐng)求的猛。

你可以用另一種形式來(lái)編寫上面的控制器耀盗,你可以在?@RequestMapping?中添加額外的屬性,如下所示:

@ControllerpublicclassHelloController{@RequestMapping(value ="/hello", method = RequestMethod.GET)publicStringprintHello(ModelMap model){? ? ? model.addAttribute("message","Hello Spring MVC Framework!");return"hello";? }}

屬性表明 URL 映射到哪個(gè)處理方法卦尊,方法屬性定義了 service 方法來(lái)處理 HTTP GET 請(qǐng)求叛拷。關(guān)于上面定義的控制器,這里有以下幾個(gè)要注意的要點(diǎn):

你將在一個(gè) service 方法中定義需要的業(yè)務(wù)邏輯岂却。你可以根據(jù)每次需求在這個(gè)方法中調(diào)用其他方法忿薇。

基于定義的業(yè)務(wù)邏輯,你將在這個(gè)方法中創(chuàng)建一個(gè)模型淌友。你可以設(shè)置不同的模型屬性煌恢,這些屬性將被視圖訪問并顯示最終的結(jié)果。這個(gè)示例創(chuàng)建了一個(gè)帶有屬性 “message” 的模型震庭。

一個(gè)定義的 service 方法可以返回一個(gè)包含視圖名稱的字符串用于呈現(xiàn)該模型瑰抵。這個(gè)示例返回 “hello” 作為邏輯視圖的名稱。

創(chuàng)建 JSP 視圖

對(duì)于不同的表示技術(shù)器联,Spring MVC 支持許多類型的視圖二汛。這些包括 JSP婿崭、HTML、PDF肴颊、Excel 工作表氓栈、XML、Velocity 模板婿着、XSLT授瘦、JSON、Atom 和 RSS 提要竟宋、JasperReports 等等提完。但我們最常使用利用 JSTL 編寫的 JSP 模板。所以讓我們?cè)?/WEB-INF/hello/hello.jsp 中編寫一個(gè)簡(jiǎn)單的?hello?視圖:

Hello Spring MVC

${message}

其中丘侠,${message}?是我們?cè)诳刂破鲀?nèi)部設(shè)置的屬性徒欣。你可以在你的視圖中有多個(gè)屬性顯示。




2.Spring MVC Hello World 例子

Spring MVC Hello World 例子

下面的例子說(shuō)明了如何使用 Spring MVC 框架來(lái)編寫一個(gè)簡(jiǎn)單的基于 web 的 Hello World 應(yīng)用程序蜗字。下面讓我們使用 Eclipse IDE打肝,然后按照下面的步驟使用 Spring 的 Web 框架來(lái)開發(fā)一個(gè)動(dòng)態(tài) Web 應(yīng)用程序:

步驟描述

1創(chuàng)建一個(gè)名稱為?HelloWeb?的動(dòng)態(tài) Web 項(xiàng)目,并且在已創(chuàng)建的項(xiàng)目的?src?文件夾中創(chuàng)建一個(gè)包?com.tutorialspoint挪捕。

2將上面提到的 Spring 和其他庫(kù)拖拽到文件夾?WebContent/WEB-INF/lib?中粗梭。

3在?com.tutorialspoint?包下創(chuàng)建一個(gè) Java 類?HelloController

4在?WebContent/WEB-INF?文件夾下創(chuàng)建 Spring 的配置文件?Web.xml?和?HelloWeb-servlet.xml级零。

5在?WebContent/WEB-INF?文件夾下創(chuàng)建名稱為?jsp?的子文件夾楼吃。在這個(gè)子文件夾下創(chuàng)建一個(gè)視圖文件?hello.jsp

6最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容妄讯,并導(dǎo)出該應(yīng)用程序,正如下面解釋的一樣酷宵。

這里是?HelloController.java?文件的內(nèi)容:

packagecom.tutorialspoint;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.ui.ModelMap;@Controller@RequestMapping("/hello")public class HelloController{? ? @RequestMapping(method= RequestMethod.GET)? public String printHello(ModelMap model) {model.addAttribute("message", "HelloSpringMVCFramework!");return"hello";? }}

下面是 Spring Web 配置文件?web.xml?的內(nèi)容

? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">Spring MVC ApplicationHelloWeborg.springframework.web.servlet.DispatcherServlet1HelloWeb/

下面是另一個(gè) Spring Web 配置文件?HelloWeb-servlet.xml?的內(nèi)容

? 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">

下面是 Spring 視圖文件?hello.jsp?的內(nèi)容

<%@pagecontentType="text/html; charset=UTF-8"%>Hello World

${message}

最后亥贸,下面是包含在你的 web 應(yīng)用程序中的 Spring 和其他庫(kù)。你僅僅需要將這些文件拖拽到?WebContent/WEB-INF/lib?文件夾中浇垦。

commons-logging-x.y.z.jar

org.springframework.asm-x.y.z.jar

org.springframework.beans-x.y.z.jar

org.springframework.context-x.y.z.jar

org.springframework.core-x.y.z.jar

org.springframework.expression-x.y.z.jar

org.springframework.web.servlet-x.y.z.jar

org.springframework.web-x.y.z.jar

spring-web.jar

一旦你完成了創(chuàng)建源代碼和配置文件后炕置,導(dǎo)出你的應(yīng)用程序。右鍵單擊你的應(yīng)用程序男韧,并且使用?Export > WAR File?選項(xiàng)朴摊,在 Tomcat 的?webapps?文件夾中保存你的?HelloWeb.war?文件。

現(xiàn)在啟動(dòng)你的 Tomcat 服務(wù)器此虑,并且確保你能夠使用標(biāo)準(zhǔn)的瀏覽器訪問 webapps 文件夾中的其他 web 頁(yè)面∩醺伲現(xiàn)在嘗試訪問該 URL?http://localhost:8080/HelloWeb/hello。如果你的 Spring Web 應(yīng)用程序一切都正常朦前,你應(yīng)該看到下面的結(jié)果:

你應(yīng)該注意介杆,在給定的 URL 中鹃操,HelloWeb?是這個(gè)應(yīng)用程序的名稱,并且?hello?是我們?cè)诳刂破髦惺褂?@RequestMapping("/hello") 提到的虛擬子文件夾春哨。當(dāng)使用?@RequestMapping("/")?映射你的 URL 時(shí)荆隘,你可以使用直接 root,在這種情況下赴背,你可以使用短 URL?http://localhost:8080/HelloWeb/?訪問相同的頁(yè)面椰拒,但是建議在不同的文件夾下有不同的功能。




3.Spring MVC 表單處理例子

Spring MVC 表單處理例子

下面的例子說(shuō)明了如何編寫一個(gè)簡(jiǎn)單的基于 web 的應(yīng)用程序凰荚,它利用了使用 Spring 的 Web MVC 框架的 HTML 表單燃观。為了開始使用它,讓我們?cè)谇‘?dāng)?shù)奈恢檬褂?Eclipse IDE浇揩,然后按照下面的步驟使用 Spring 的 Web 框架來(lái)開發(fā)一個(gè)動(dòng)態(tài)的基于表單的 Web 應(yīng)用程序:

步驟描述

1創(chuàng)建一個(gè)名稱為?HelloWeb?的動(dòng)態(tài) Web 項(xiàng)目仪壮,并且在已創(chuàng)建的項(xiàng)目的?src?文件夾中創(chuàng)建一個(gè)包?com.tutorialspoint

2將上面提到的 Spring 和其他庫(kù)拖拽到文件夾?WebContent/WEB-INF/lib?中胳徽。

3在?com.tutorialspoint?包下創(chuàng)建一個(gè) Java 類?Student?和?StudentController积锅。

4在?WebContent/WEB-INF?文件夾下創(chuàng)建 Spring 的配置文件?Web.xml?和?HelloWeb-servlet.xml

5在?WebContent/WEB-INF?文件夾下創(chuàng)建名稱為?jsp?的子文件夾养盗。在這個(gè)子文件夾下創(chuàng)建視圖文件?student.jsp?和?result.jsp缚陷。

6最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容,并導(dǎo)出該應(yīng)用程序往核,正如下面解釋的一樣箫爷。

這里是?Student.java?文件的內(nèi)容:

packagecom.tutorialspoint;publicclassStudent{privateInteger age;privateString name;privateInteger id;publicvoidsetAge(Integer age){this.age = age;? }publicIntegergetAge(){returnage;? }publicvoidsetName(String name){this.name = name;? }publicStringgetName(){returnname;? }publicvoidsetId(Integer id){this.id = id;? }publicIntegergetId(){returnid;? }}

下面是?StudentController.java?文件的內(nèi)容:

packagecom.tutorialspoint;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.servlet.ModelAndView;importorg.springframework.ui.ModelMap;@ControllerpublicclassStudentController{@RequestMapping(value ="/student", method = RequestMethod.GET)publicModelAndViewstudent(){returnnewModelAndView("student","command",newStudent());? }@RequestMapping(value ="/addStudent", method = RequestMethod.POST)publicStringaddStudent(@ModelAttribute("SpringWeb")Student student,? ? ModelMap model){? ? ? model.addAttribute("name", student.getName());? ? ? model.addAttribute("age", student.getAge());? ? ? model.addAttribute("id", student.getId());return"result";? }}

在這里,第一個(gè) service 方法?student()聂儒,我們已經(jīng)在名稱為 “command” 的 ModelAndView 對(duì)象中傳遞一個(gè)空的?Student?對(duì)象虎锚,因?yàn)?spring 框架需要一個(gè)名稱的 “command” 的對(duì)象,如果你在 JSP 文件中使用 標(biāo)簽衩婚。所以窜护,當(dāng)?student()?方法被調(diào)用時(shí),它返回?student.jsp?視圖非春。

第二個(gè) service 方法?addStudent()?將調(diào)用?HelloWeb/addStudent?URL 中的 POST 方法柱徙。你將根據(jù)提交的信息準(zhǔn)備好你的模型對(duì)象。最后一個(gè) “result” 視圖會(huì)從 service 方法中返回奇昙,它將導(dǎo)致呈現(xiàn) result.jsp护侮。

下面是 Spring Web 配置文件?web.xml?的內(nèi)容

? ? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">Spring MVC Form HandlingHelloWeborg.springframework.web.servlet.DispatcherServlet1HelloWeb/

下面是另一個(gè) Spring Web 配置文件?HelloWeb-servlet.xml?的內(nèi)容

? 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">

下面是 Spring 視圖文件?student.jsp?的內(nèi)容

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring MVC Form Handling

Student Information

NameAgeid

下面是 Spring 視圖文件?result.jsp?的內(nèi)容

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring MVC Form Handling

Submitted Student Information

Name${name}Age${age}ID${id}

最后,下面是包含在你的 web 應(yīng)用程序中的 Spring 和其他庫(kù)的列表储耐。你僅僅需要將這些文件拖拽到?WebContent/WEB-INF/lib?文件夾中羊初。

commons-logging-x.y.z.jar

org.springframework.asm-x.y.z.jar

org.springframework.beans-x.y.z.jar

org.springframework.context-x.y.z.jar

org.springframework.core-x.y.z.jar

org.springframework.expression-x.y.z.jar

org.springframework.web.servlet-x.y.z.jar

org.springframework.web-x.y.z.jar

spring-web.jar

一旦你完成了創(chuàng)建源代碼和配置文件后,導(dǎo)出你的應(yīng)用程序弧岳。右鍵單擊你的應(yīng)用程序凳忙,并且使用?Export > WAR File?選項(xiàng)业踏,并且在 Tomcat 的?webapps?文件夾中保存你的?HelloWeb.war?文件。

現(xiàn)在啟動(dòng)你的 Tomcat 服務(wù)器涧卵,并且確保你能夠使用標(biāo)準(zhǔn)的瀏覽器訪問 webapps 文件夾中的其他 web 頁(yè)面∏诩遥現(xiàn)在嘗試訪問該 URL?http://localhost:8080/HelloWeb/student。如果你的 Spring Web 應(yīng)用程序一切都正常柳恐,你應(yīng)該看到下面的結(jié)果:

在提交必需的信息之后伐脖,單擊提交按鈕來(lái)提交這個(gè)表單。如果你的 Spring Web 應(yīng)用程序一切都正常乐设,你應(yīng)該看到下面的結(jié)果:





4.Spring 頁(yè)面重定向例子

Spring 頁(yè)面重定向例子

下面的例子說(shuō)明了如何編寫一個(gè)簡(jiǎn)單的基于 web 的應(yīng)用程序讼庇,它利用重定向來(lái)傳送一個(gè) http 請(qǐng)求到另一個(gè)頁(yè)面中。為了開始使用它近尚,讓我們?cè)谇‘?dāng)?shù)奈恢檬褂?Eclipse IDE蠕啄,然后按照下面的步驟使用 Spring 的 Web 框架來(lái)開發(fā)一個(gè)動(dòng)態(tài)的基于表單的 Web 應(yīng)用程序:

步驟描述

1創(chuàng)建一個(gè)名稱為?HelloWeb?的動(dòng)態(tài) Web 項(xiàng)目,并且在已創(chuàng)建的項(xiàng)目的?src?文件夾中創(chuàng)建一個(gè)包?com.tutorialspoint戈锻。

2將上面提到的 Spring 和其他庫(kù)拖拽到文件夾?WebContent/WEB-INF/lib?中歼跟。

3在?com.tutorialspoint?包下創(chuàng)建一個(gè) Java 類?WebController

4在?WebContent/WEB-INF?文件夾下創(chuàng)建 Spring 的配置文件?Web.xml?和?HelloWeb-servlet.xml格遭。

5在?WebContent/WEB-INF?文件夾下創(chuàng)建名稱為?jsp?的子文件夾哈街。在這個(gè)子文件夾下創(chuàng)建視圖文件?index.jsp?和?final.jsp

6最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容拒迅,并導(dǎo)出該應(yīng)用程序骚秦,正如下面解釋的一樣。

這里是?WebController.java?文件的內(nèi)容:

packagecom.tutorialspoint;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;@ControllerpublicclassWebController{@RequestMapping(value ="/index", method = RequestMethod.GET)publicStringindex(){return"index";? }@RequestMapping(value ="/redirect", method = RequestMethod.GET)publicStringredirect(){return"redirect:finalPage";? }@RequestMapping(value ="/finalPage", method = RequestMethod.GET)publicStringfinalPage(){return"final";? }}

下面是 Spring Web 配置文件?web.xml?的內(nèi)容

? ? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">Spring Page RedirectionHelloWeborg.springframework.web.servlet.DispatcherServlet1HelloWeb/

下面是另一個(gè) Spring Web 配置文件?HelloWeb-servlet.xml?的內(nèi)容

<?xml version="1.0"encoding="UTF-8"?>

? 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">

下面是 Spring 視圖文件?index.jsp?文件的內(nèi)容璧微。這將是一個(gè)登陸頁(yè)面作箍,這個(gè)頁(yè)面將發(fā)送一個(gè)請(qǐng)求來(lái)訪問重定向?service 方法,該方法將把這個(gè)請(qǐng)求重定向到另一個(gè) service 方法中前硫,最后將顯示?final.jsp?頁(yè)面蒙揣。

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring Page Redirection

Spring Page Redirection

Click below button to redirect the result to new page

下面是 Spring 視圖文件?final.jsp?的內(nèi)容。這是最終的重定向頁(yè)面开瞭。

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring Page Redirection

Redirected Page

最后,下面是包含在你的 web 應(yīng)用程序中的 Spring 和其他庫(kù)的列表罩息。你僅僅需要將這些文件拖拽到?WebContent/WEB-INF/lib?文件夾中嗤详。

commons-logging-x.y.z.jar

org.springframework.asm-x.y.z.jar

org.springframework.beans-x.y.z.jar

org.springframework.context-x.y.z.jar

org.springframework.core-x.y.z.jar

org.springframework.expression-x.y.z.jar

org.springframework.web.servlet-x.y.z.jar

org.springframework.web-x.y.z.jar

spring-web.jar

一旦你完成了創(chuàng)建源代碼和配置文件后,導(dǎo)出你的應(yīng)用程序瓷炮。右鍵單擊你的應(yīng)用程序葱色,并且使用?Export > WAR File?選項(xiàng),并且在 Tomcat 的?webapps?文件夾中保存你的?HelloWeb.war?文件娘香。

現(xiàn)在啟動(dòng)你的 Tomcat 服務(wù)器苍狰,并且確保你能夠使用標(biāo)準(zhǔn)的瀏覽器訪問 webapps 文件夾中的其他 web 頁(yè)面“炝洌現(xiàn)在嘗試訪問該 URL?http://localhost:8080/HelloWeb/index。如果你的 Spring Web 應(yīng)用程序一切都正常淋昭,你應(yīng)該看到下面的結(jié)果:

現(xiàn)在單擊 “Redirect Page” 按鈕來(lái)提交表單俐填,并且得到最終的重定向頁(yè)面。如果你的 Spring Web 應(yīng)用程序一切都正常翔忽,你應(yīng)該看到下面的結(jié)果:




5.Spring 靜態(tài)頁(yè)面例子

Spring 靜態(tài)頁(yè)面例子

下面的例子說(shuō)明了如何使用 Spring MVC 框架來(lái)編寫一個(gè)簡(jiǎn)單的基于 web 的應(yīng)用程序英融,它可以在 <mvc:resources> 標(biāo)簽的幫助下訪問靜態(tài)頁(yè)面和動(dòng)態(tài)頁(yè)面。為了開始使用它歇式,讓我們?cè)谇‘?dāng)?shù)奈恢檬褂?Eclipse IDE驶悟,然后按照下面的步驟使用 Spring 的 Web 框架來(lái)開發(fā)一個(gè)動(dòng)態(tài)的基于表單的 Web 應(yīng)用程序:

步驟描述

1創(chuàng)建一個(gè)名稱為?HelloWeb?的動(dòng)態(tài) Web 項(xiàng)目,并且在已創(chuàng)建的項(xiàng)目的?src?文件夾中創(chuàng)建一個(gè)包?com.tutorialspoint材失。

2將上面提到的 Spring 和其他庫(kù)拖拽到文件夾?WebContent/WEB-INF/lib?中痕鳍。

3在?com.tutorialspoint?包下創(chuàng)建一個(gè) Java 類?WebController

4在?WebContent/WEB-INF?文件夾下創(chuàng)建 Spring 的配置文件?Web.xml?和?HelloWeb-servlet.xml龙巨。

5在?WebContent/WEB-INF?文件夾下創(chuàng)建名稱為?jsp?的子文件夾笼呆。在這個(gè)子文件夾下創(chuàng)建一個(gè)視圖文件?index.jsp

6在?WebContent/WEB-INF?文件夾下創(chuàng)建名稱為?pages?的子文件夾恭应。在這個(gè)子文件夾下創(chuàng)建一個(gè)靜態(tài)文件?final.htm

7最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容抄邀,并導(dǎo)出該應(yīng)用程序,正如下面解釋的一樣昼榛。

這里是?WebController.java?文件的內(nèi)容:

packagecom.tutorialspoint;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;@ControllerpublicclassWebController{@RequestMapping(value ="/index", method = RequestMethod.GET)publicStringindex(){return"index";? }@RequestMapping(value ="/staticPage", method = RequestMethod.GET)publicStringredirect(){return"redirect:/pages/final.htm";? }}

下面是 Spring Web 配置文件?web.xml?的內(nèi)容

? ? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">Spring Page RedirectionHelloWeborg.springframework.web.servlet.DispatcherServlet1HelloWeb/

下面是另一個(gè) Spring Web 配置文件?HelloWeb-servlet.xml?的內(nèi)容

<?xml version="1.0"encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-2.5.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">

在這里境肾,<mvc:resources..../>?標(biāo)簽被用來(lái)映射靜態(tài)頁(yè)面。?mapping?屬性必須是一個(gè)指定一個(gè) http 請(qǐng)求的 URL 模式的 Ant 模式胆屿。?location?屬性必須指定一個(gè)或者多個(gè)具有包含圖片奥喻,樣式表,JavaScript 和其他靜態(tài)內(nèi)容的靜態(tài)頁(yè)面的資源目錄位置非迹。多個(gè)資源位置可以使用逗號(hào)分隔這些值的列表來(lái)被指定环鲤。

下面是 Spring 視圖文件?WEB-INF/jsp/index.jsp?的內(nèi)容。這將是一個(gè)登陸頁(yè)面憎兽,這個(gè)頁(yè)面將發(fā)送一個(gè)請(qǐng)求來(lái)訪問?staticPage?的 service 方法冷离,它將重定向這個(gè)請(qǐng)求到 WEB-INF/pages 文件夾中的一個(gè)可用的靜態(tài)頁(yè)面。

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring Landing Page

Spring Landing Pag

Click below button to get a simple HTML page

下面是 Spring 視圖文件?WEB-INF/pages/final.htm?的內(nèi)容纯命。

Spring Static Page

A simple HTML page

最后西剥,下面是包含在你的 web 應(yīng)用程序中的 Spring 和其他庫(kù)的列表。你僅僅需要將這些文件拖拽到?WebContent/WEB-INF/lib?文件夾中亿汞。

commons-logging-x.y.z.jar

org.springframework.asm-x.y.z.jar

org.springframework.beans-x.y.z.jar

org.springframework.context-x.y.z.jar

org.springframework.core-x.y.z.jar

org.springframework.expression-x.y.z.jar

org.springframework.web.servlet-x.y.z.jar

org.springframework.web-x.y.z.jar

spring-web.jar

一旦你完成了創(chuàng)建源代碼和配置文件后瞭空,導(dǎo)出你的應(yīng)用程序。右鍵單擊你的應(yīng)用程序,并且使用?Export > WAR File?選項(xiàng)咆畏,并且在 Tomcat 的?webapps?文件夾中保存你的?HelloWeb.war?文件南捂。

現(xiàn)在啟動(dòng)你的 Tomcat 服務(wù)器,并且確保你能夠使用標(biāo)準(zhǔn)的瀏覽器訪問 webapps 文件夾中的其他 web 頁(yè)面【烧遥現(xiàn)在嘗試訪問該 URL?http://localhost:8080/HelloWeb/index溺健。 如果你的 Spring Web 應(yīng)用程序一切都正常,你應(yīng)該看到下面的結(jié)果:

單擊 “Get HTML Page” 按鈕來(lái)訪問 staticPage 中的 service 方法中提到的一個(gè)靜態(tài)頁(yè)面钦讳。如果你的 Spring Web 應(yīng)用程序一切都正常矿瘦,你應(yīng)該看到下面的結(jié)果:



6.Spring 異常處理例子

Spring 異常處理例子

下面的例子說(shuō)明了如何使用 Spring MVC 框架來(lái)編寫一個(gè)簡(jiǎn)單的基于 web 的應(yīng)用程序,它可以處理它的內(nèi)置控制器產(chǎn)生的一個(gè)或多個(gè)異常愿卒。為了開始使用它缚去,讓我們?cè)谇‘?dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟使用 Spring 的 Web 框架來(lái)開發(fā)一個(gè)動(dòng)態(tài)的基于表單的 Web 應(yīng)用程序:

步驟描述

1創(chuàng)建一個(gè)名稱為?HelloWeb?的動(dòng)態(tài) Web 項(xiàng)目琼开,并且在已創(chuàng)建的項(xiàng)目的?src?文件夾中創(chuàng)建一個(gè)包?com.tutorialspoint易结。

2將上面提到的 Spring 和其他庫(kù)拖拽到文件夾?WebContent/WEB-INF/lib?中。

3在?com.tutorialspoint?包下創(chuàng)建一個(gè) Java 類?Student柜候,StudentController?和?SpringException搞动。

4在?WebContent/WEB-INF?文件夾下創(chuàng)建 Spring 的配置文件?Web.xml?和?HelloWeb-servlet.xml

5在?WebContent/WEB-INF?文件夾下創(chuàng)建名稱為?jsp?的子文件夾渣刷。在這個(gè)子文件夾下創(chuàng)建視圖文件?student.jsp鹦肿,result.jsperror.jsp?和?ExceptionPage.jsp辅柴。

6最后一步是創(chuàng)建所有的源代碼和配置文件的內(nèi)容箩溃,并導(dǎo)出該應(yīng)用程序,正如下面解釋的一樣碌嘀。

這里是?Student.java?文件的內(nèi)容:

packagecom.tutorialspoint;publicclassStudent{privateInteger age;privateString name;privateInteger id;publicvoidsetAge(Integer age){this.age = age;? }publicIntegergetAge(){returnage;? }publicvoidsetName(String name){this.name = name;? }publicStringgetName(){returnname;? }publicvoidsetId(Integer id){this.id = id;? }publicIntegergetId(){returnid;? }}

下面是?SpringException.java?文件的內(nèi)容:

packagecom.tutorialspoint;publicclassSpringExceptionextendsRuntimeException{privateString exceptionMsg;publicSpringException(String exceptionMsg){this.exceptionMsg = exceptionMsg;? }publicStringgetExceptionMsg(){returnthis.exceptionMsg;? }publicvoidsetExceptionMsg(String exceptionMsg){this.exceptionMsg = exceptionMsg;? }}

下面是?StudentController.java?文件的內(nèi)容涣旨。這里,你需要使用?@ExceptionHandler?注解一個(gè) service 方法股冗,你可以指定要處理的一個(gè)或多個(gè)異常霹陡。如果你要指定一個(gè)以上的異常,那么你可以使用逗號(hào)分隔這些值止状。

packagecom.tutorialspoint;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.servlet.ModelAndView;importorg.springframework.ui.ModelMap;@ControllerpublicclassStudentController{@RequestMapping(value ="/student", method = RequestMethod.GET)publicModelAndViewstudent(){returnnewModelAndView("student","command",newStudent());? }@RequestMapping(value ="/addStudent", method = RequestMethod.POST)@ExceptionHandler({SpringException.class})publicStringaddStudent( @ModelAttribute("HelloWeb")Student student,? ? ? ModelMap model){if(student.getName().length() <5){thrownewSpringException("Given name is too short");? ? ? }else{? ? ? model.addAttribute("name", student.getName());? ? ? }if( student.getAge() <10){thrownewSpringException("Given age is too low");? ? ? }else{? ? ? model.addAttribute("age", student.getAge());? ? ? }? ? ? model.addAttribute("id", student.getId());return"result";? }}

下面是 Spring Web 配置文件?web.xml?的內(nèi)容

? ? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">Spring Exception HandlingHelloWeborg.springframework.web.servlet.DispatcherServlet1HelloWeb/

下面是另一個(gè) Spring Web 配置文件?HelloWeb-servlet.xml?的內(nèi)容

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">

? ? ? SimpleMappingExceptionResolver">ExceptionPage

在這里烹棉,你指定?ExceptionPage?作為一個(gè)異常視圖,以便 SpringException 發(fā)生怯疤,如果有任何其他類型的異常發(fā)生峦耘,那么一個(gè)通用的視圖?error?會(huì)發(fā)生。

下面是 Spring 視圖文件?student.jsp?的內(nèi)容:

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring MVC Exception Handling

Student Information

NameAgeid

下面是 Spring 視圖文件?error.jsp?的內(nèi)容:

Spring Error Page

An error occured, please contact webmaster.

;

下面是 Spring 視圖文件?ExceptionPage.jsp?的內(nèi)容旅薄。在這里,你將通過(guò) ${exception} 訪問異常實(shí)例。

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring MVC Exception Handling

Spring MVC Exception Handling

${exception.exceptionMsg}

下面是 Spring 視圖文件?result.jsp?的內(nèi)容:

<%@tagliburi="http://www.springframework.org/tags/form"prefix="form"%>Spring MVC Form Handling

Submitted Student Information

Name${name}Age${age}ID${id}

最后少梁,下面是包含在你的 web 應(yīng)用程序中的 Spring 和其他庫(kù)的列表洛口。你僅僅需要將這些文件拖拽到?WebContent/WEB-INF/lib?文件夾中。

commons-logging-x.y.z.jar

org.springframework.asm-x.y.z.jar

org.springframework.beans-x.y.z.jar

org.springframework.context-x.y.z.jar

org.springframework.core-x.y.z.jar

org.springframework.expression-x.y.z.jar

org.springframework.web.servlet-x.y.z.jar

org.springframework.web-x.y.z.jar

spring-web.jar

一旦你完成了創(chuàng)建源代碼和配置文件后凯沪,導(dǎo)出你的應(yīng)用程序第焰。右鍵單擊你的應(yīng)用程序,并且使用?Export > WAR File?選項(xiàng)妨马,并且在 Tomcat 的?webapps?文件夾中保存你的?HelloWeb.war?文件挺举。

現(xiàn)在啟動(dòng)你的 Tomcat 服務(wù)器,并且確保你能夠使用標(biāo)準(zhǔn)的瀏覽器訪問 webapps 文件夾中的其他 web 頁(yè)面『娑澹現(xiàn)在嘗試訪問該 URL?http://localhost:8080/SpringWeb/student湘纵。如果你的 Spring Web 應(yīng)用程序一切都正常,你應(yīng)該看到下面的結(jié)果:

輸入如上圖所示的值滤淳,然后單擊提交按鈕梧喷。如果你的 Spring Web 應(yīng)用程序一切都正常,你應(yīng)該看到下面的結(jié)果:




7.Spring 使用 Log4J 記錄日志

使用 Log4J 記錄日志

在 Spring 應(yīng)用程序中使用 Log4J 的功能是非常容易的脖咐。下面的例子將帶你通過(guò)簡(jiǎn)單的步驟解釋 Log4J 和 Spring 之間的簡(jiǎn)單集成铺敌。

假設(shè)你已經(jīng)在你的機(jī)器上安裝了?Log4J,如果你還沒有 Log4J屁擅,你可以從?http://logging.apache.org/?中下載偿凭,并且僅僅在任何文件夾中提取壓縮文件。在我們的項(xiàng)目中派歌,我們將只使用?log4j-x.y.z.jar弯囊。

接下來(lái),我們讓 Eclipse IDE 在恰當(dāng)?shù)奈恢霉ぷ飨踉恚裱韵虏襟E常挚,使用 Spring Web 框架開發(fā)一個(gè)基于 Web 應(yīng)用程序的動(dòng)態(tài)表單:

步驟描述

1創(chuàng)建一個(gè)名稱為?SpringExample?的項(xiàng)目,并且在創(chuàng)建項(xiàng)目的?src?文件夾中創(chuàng)建一個(gè)包?com.tutorialspoint稽物。

2使用?Add External JARs?選項(xiàng)奄毡,添加所需的 Spring 庫(kù),解釋見?Spring Hello World Example?章節(jié)贝或。

3使用?Add External JARs?選項(xiàng)吼过,同樣在你的項(xiàng)目中添加 log4j 庫(kù)?log4j-x.y.z.jar

4在?com.tutorialspoint?包下創(chuàng)建 Java 類?HelloWorld?和?MainApp咪奖。

5在?src?文件中創(chuàng)建 Bean 配置文件?Beans.xml盗忱。

6在?src?文件中創(chuàng)建 log4J 配置文件?log4j.properties

7最后一步是創(chuàng)建的所有 Java 文件和 Bean 配置文件的內(nèi)容羊赵,并運(yùn)行應(yīng)用程序趟佃,解釋如下所示扇谣。

這個(gè)是?HelloWorld.java?文件的內(nèi)容:

packagecom.tutorialspoint;publicclassHelloWorld{privateString message;publicvoidsetMessage(String message){this.message? = message;? }publicvoidgetMessage(){? ? ? System.out.println("Your Message : "+ message);? }}

下面的是第二個(gè)文件?MainApp.java?的內(nèi)容:

packagecom.tutorialspoint;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.apache.log4j.Logger;publicclassMainApp{staticLogger log = Logger.getLogger(MainApp.class.getName());publicstaticvoidmain(String[] args){? ? ? ApplicationContext context =newClassPathXmlApplicationContext("Beans.xml");? ? ? log.info("Going to create HelloWord Obj");? ? ? HelloWorld obj = (HelloWorld) context.getBean("helloWorld");? ? ? obj.getMessage();? ? ? log.info("Exiting the program");? }}

使用與我們已經(jīng)生成信息消息類似的方法,你可以生成調(diào)試錯(cuò)誤消息∠姓眩現(xiàn)在讓我們看看?Beans.xml?文件的內(nèi)容:

? ? http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">? ? ? ? ?

下面是?log4j.properties?的內(nèi)容罐寨,它定義了使用 Log4J 生成日志信息所需的標(biāo)準(zhǔn)規(guī)則:

# Define the root logger with appender filelog4j.rootLogger = DEBUG, FILE# Define the file appenderlog4j.appender.FILE=org.apache.log4j.FileAppender# Set the name of the filelog4j.appender.FILE.File=C:\\log.out#Set the immediate flush totrue(default)log4j.appender.FILE.ImmediateFlush=true# Set the threshold to debug modelog4j.appender.FILE.Threshold=debug# Set the append tofalse, overwritelog4j.appender.FILE.Append=false# Define the layoutforfile appenderlog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n

一旦你完成了創(chuàng)建源和 bean 的配置文件后,我們就可以運(yùn)行該應(yīng)用程序序矩。如果你的應(yīng)用程序一切都正常鸯绿,在 Eclipse 控制臺(tái)將輸出以下信息:

Your Message : Hello World!

同時(shí)如果你檢查你的 C:\ 驅(qū)動(dòng),那么你應(yīng)該發(fā)現(xiàn)含有各種日志消息的日志文件?log.out簸淀,其中一些如下所示:

Going to create HelloWord ObjReturning cached instance of singleton bean'helloWorld'Exiting the program

Jakarta Commons Logging (JCL) API

或者瓶蝴,你可以使用?Jakarta Commons Logging(JCL)?API 在你的 Spring 應(yīng)用程序中生成日志。JCL 可以從?http://jakarta.apache.org/commons/logging/?下載租幕。我們?cè)诩夹g(shù)上需要這個(gè)包的唯一文件是?commons-logging-x.y.z.jar?文件舷手,需要使用與上面的例子中你使用?log4j-x.y.z.jar?類似的方法來(lái)把?commons-logging-x.y.z.jar?放在你的類路徑中。

為了使用日志功能令蛉,你需要一個(gè)?org.apache.commons.logging.Log?對(duì)象聚霜,然后你可以根據(jù)你的需要調(diào)用任何一個(gè)下面的方法:

fatal(Object message)

error(Object message)

warn(Object message)

info(Object message)

debug(Object message)

trace(Object message)

下面是使用 JCL API 對(duì) MainApp.java 的替換:

packagecom.tutorialspoint;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.apache.commons.logging. Log;importorg.apache.commons.logging. LogFactory;publicclassMainApp{staticLog log = LogFactory.getLog(MainApp.class.getName());publicstaticvoidmain(String[] args){? ? ? ApplicationContext context =newClassPathXmlApplicationContext("Beans.xml");? ? ? log.info("Going to create HelloWord Obj");? ? ? HelloWorld obj = (HelloWorld) context.getBean("helloWorld");? ? ? obj.getMessage();? ? ? log.info("Exiting the program");? }}

你應(yīng)該確保在編譯和運(yùn)行該程序之前在你的項(xiàng)目中已經(jīng)引入了?commons-logging-x.y.z.jar?文件。

現(xiàn)在保持在上面的例子中剩下的配置和內(nèi)容不變珠叔,如果你編譯并運(yùn)行你的應(yīng)用程序蝎宇,你就會(huì)得到與使用 Log4J API 后獲得的結(jié)果類似的結(jié)果。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末祷安,一起剝皮案震驚了整個(gè)濱河市姥芥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌汇鞭,老刑警劉巖凉唐,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異霍骄,居然都是意外死亡台囱,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門读整,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)簿训,“玉大人,你說(shuō)我怎么就攤上這事米间∏科罚” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵屈糊,是天一觀的道長(zhǎng)的榛。 經(jīng)常有香客問我,道長(zhǎng)逻锐,這世上最難降的妖魔是什么夫晌? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任雕薪,我火速辦了婚禮,結(jié)果婚禮上晓淀,老公的妹妹穿的比我還像新娘蹦哼。我一直安慰自己,他們只是感情好要糊,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著妆丘,像睡著了一般锄俄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上勺拣,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天奶赠,我揣著相機(jī)與錄音,去河邊找鬼药有。 笑死毅戈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的愤惰。 我是一名探鬼主播苇经,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼宦言!你這毒婦竟也來(lái)了扇单?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤奠旺,失蹤者是張志新(化名)和其女友劉穎蜘澜,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體响疚,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鄙信,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了忿晕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片装诡。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蓬抄,死狀恐怖浩淘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情本辐,我是刑警寧澤宏侍,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布赖淤,位于F島的核電站,受9級(jí)特大地震影響谅河,放射性物質(zhì)發(fā)生泄漏咱旱。R本人自食惡果不足惜确丢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吐限。 院中可真熱鬧鲜侥,春花似錦、人聲如沸诸典。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)狐粱。三九已至舀寓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肌蜻,已是汗流浹背互墓。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蒋搜,地道東北人篡撵。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像豆挽,于是被迫代替她去往敵國(guó)和親育谬。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359