<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Stripes 介紹 原文:http://zetcode.com/java/stripes/ 這是 Stripes 入門教程。 我們使用 Stripes Web 框架創建兩個簡單的 Web 應用。 我們使用 NetBeans 來構建應用。 Stripes 是一個開源的輕量級 Java Web 應用框架。 Stripes 的目標是使 Java 中基于 Servlet/JSP 的 Web 開發變得簡單,直觀和直接。 Stripes 是基于動作的 MVC(模型視圖控制器)框架。 它運行在 JEE Web 容器中,使用最少的配置文件,并具有靈活和簡單的參數綁定。 Stripes 的`ActionBean`是一個對象,用于接收在請求中提交的數據并處理用戶的輸入。 它既定義了表單的屬性,又定義了表單的處理邏輯。 Stripes 會在部署時通過掃描 Web 應用的類路徑來自動發現`ActionBean`。 條帶過濾器的`ActionResolver.Packages` `init-param`(在`web.xml`中)設置一個或多個包根。 分辨率是作為對已處理請求的響應而創建的對象。 解決方案可以轉發到 JSP 頁面,流數據或返回錯誤消息。 分辨率由`ActionBeans`的處理器方法返回。 從 Stripes 的 Github [頁面](https://github.com/StripesFramework/stripes/releases)中,我們下載了最新的 Stripes 版本。 在`lib`子目錄中,我們需要在項目中包含三個 JAR 文件:`commons-logging-1.1.3.jar`,`cos-05Nov2002.jar`和`stripes-1.6.0.jar`。 此外,還有`StripesResources.properties`文件,其中包含各種消息。 ## 簡單 Stripes 應用 第一個應用顯示當前日期。 我們在 NetBeans 中創建一個新的 Web 應用。 我們選擇 Tomcat 作為我們的 JSP/servlet 硬幣容器。 ![The project files](https://img.kancloud.cn/58/c7/58c7fd6a788442d0f810b73a5a9a39a5_263x265.jpg) 圖:項目文件 該項目包含三個文件:`HelloActionBean.java`包含響應我們請求的代碼,`showDate.jsp`是作為響應發送回用戶的視圖,而`web.xml`文件包含用于設置 Stripes 的配置。 在此應用中,我們不使用`StripesResources.properties`。 ![The project libraries](https://img.kancloud.cn/ca/b4/cab4e1233c41b85a2d69e4adfc71e702_242x107.jpg) 圖:項目庫 這些是我們構建 Stripes 應用所需的庫。 `web.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <display-name>Stripes Filter</display-name> <filter-name>StripesFilter</filter-name> <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class> <init-param> <param-name>ActionResolver.Packages</param-name> <param-value>com.zetcode.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>StripesFilter</filter-name> <url-pattern>*.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>StripesFilter</filter-name> <servlet-name>StripesDispatcher</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> <servlet> <servlet-name>StripesDispatcher</servlet-name> <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StripesDispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>Hello.action</welcome-file> </welcome-file-list> </web-app> ``` 在標準`web.xml`部署描述符中,我們配置 Stripes。 我們指定 Stripes 在哪里尋找`ActionBean`:在我們的例子中是`com.zetcode.action`包。 歡迎文件是當我們請求主頁時顯示的文件。 `Hello.action`指示應執行`HelloActionBean`。 `HelloActionBean.java` ```java package com.zetcode.action; import java.util.Date; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.Resolution; public class HelloActionBean implements ActionBean { private static final String VIEW = "/WEB-INF/jsp/showDate.jsp"; private ActionBeanContext context; private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public void setContext(ActionBeanContext context) { this.context = context; } @Override public ActionBeanContext getContext() { return context; } @DefaultHandler public Resolution hello() { this.date = new Date(); return new ForwardResolution(VIEW); } } ``` `HelloActionBean`處理請求,并以向前解析的方式響應 JSP 頁面。 ```java private static final String VIEW = "/WEB-INF/jsp/showDate.jsp"; ``` 該視圖是`showDate.jsp`文件。 ```java private ActionBeanContext context; ``` `ActionBeanContext`封裝有關當前請求的信息。 如果我們出于任何原因需要使用它,它提供對底層 Servlet API 的訪問。 ```java @DefaultHandler public Resolution hello() { this.date = new Date(); return new ForwardResolution(VIEW); } ``` `@DefaultHandler`注解為此動作 bean 設置了默認處理器。 它用當前日期填充`date`屬性,并返回一個新的`ForwardResolution`。 分辨率轉發到視圖。 `showDate.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Current date</title> </head> <body> <h3>The date is ${actionBean.date}</h3> </body> </html> ``` 這是用戶的模板視圖。 `${actionBean}`表達式引用指向此視圖的操作 bean。 我們使用表達式來引用動作 bean 的`date`屬性。 ```java $ curl localhost:8084/SimpleStripes/ <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Current date</title> </head> <body> <h3>The date is Thu Jun 02 14:13:01 CEST 2016</h3> </body> </html> ``` 構建和部署應用之后,我們將使用`curl`工具訪問應用的主頁。 該應用將響應一個包含當前日期的 HTML 頁面。 ## Hello Stripes 應用 在第二個應用中,我們有一個簡單的 HTML 表單。 用戶在文本框中指定其名稱。 該應用以問候回應。 驗證用于確保用戶已在文本字段中輸入了內容。 `web.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <display-name>Stripes Filter</display-name> <filter-name>StripesFilter</filter-name> <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class> <init-param> <param-name>ActionResolver.Packages</param-name> <param-value>com.zetcode.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>StripesFilter</filter-name> <url-pattern>*.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>StripesFilter</filter-name> <servlet-name>StripesDispatcher</servlet-name> <dispatcher>REQUEST</dispatcher> </filter-mapping> <servlet> <servlet-name>StripesDispatcher</servlet-name> <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StripesDispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ``` 在`web.xml`文件中,我們將`index.jsp`文件設置為歡迎文件。 `index.jsp` ```java <%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Enter your name</title> </head> <body> <stripes:form beanclass="com.zetcode.action.HelloActionBean"> <stripes:errors/> Enter your name: <stripes:text name="userName"/> <stripes:submit name="save" value="Submit"/> </stripes:form> </body> </html> ``` `index.jsp`包含一個簡單的 HTML 表單。 Stripes 具有自己的標簽。 `<stripes:errors/>`顯示驗證錯誤。 如果我們未在該字段中寫入任何文本,則會顯示驗證錯誤。 在`<stripes:form>`標記中,我們指定應處理請求的操作 bean。 `<stripes:text/>`創建一個文本字段。 創建的請求參數將自動映射到`HelloActionBean`的`userName`屬性。 ![StripesResources.properties](https://img.kancloud.cn/45/fc/45fc417b1c2e0a256cf49ba015884407_264x92.jpg) 圖:`StripesResources.properties` `StripesResources.properties`是 Stripes 框架的默認資源包。 它包含各種消息和標簽的值。 樣本文件包含在 Stripes 下載文件的`lib`子目錄中。 我們將文件放入源包中,未指定包。 (該文件應最終位于`WEB-INF/classes`目錄中。) `StripesResources.properties` ```java ... validation.required.valueNotPresent={0} is a required field ... ``` 當我們在文本字段中未輸入任何內容并單擊“提交”按鈕時,將顯示此錯誤消息。 `HelloActionBean.java` ```java package com.zetcode.action; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.action.ActionBeanContext; import net.sourceforge.stripes.action.DefaultHandler; import net.sourceforge.stripes.action.ForwardResolution; import net.sourceforge.stripes.action.Resolution; import net.sourceforge.stripes.validation.Validate; public class HelloActionBean implements ActionBean { private static final String VIEW = "/WEB-INF/jsp/greet.jsp"; private ActionBeanContext context; @Validate(required=true) private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public void setContext(ActionBeanContext context) { this.context = context; } @Override public ActionBeanContext getContext() { return context; } @DefaultHandler public Resolution greet() { return new ForwardResolution(VIEW); } } ``` 單擊提交按鈕時,將執行`HelloActionBean`。 `request`參數自動綁定到其`userName`屬性。 默認處理器將轉發到`greet.jsp`視圖。 ```java @Validate(required=true) private String userName; ``` `@Validate`注解用于強制驗證表單的用戶名字段。 如果未輸入任何值,則會顯示一條錯誤消息。 ![Validation error message](https://img.kancloud.cn/e4/6d/e46d9d0037ae69448849c3435645f276_446x218.jpg) 圖:驗證錯誤消息 我們應用中的第二個視圖是`greet.jsp`。 `greet.jsp` ```java <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Greeting</title> </head> <body> <h3>Hello ${actionBean.userName}</h3> </body> </html> ``` `greet.jsp`顯示給用戶的問候消息。 通過`${actionBean.userName}`表達式,我們獲得了用戶名。 ![Greeting](https://img.kancloud.cn/c4/8d/c48dddec58d8176f543f57b19e8ac803_456x154.jpg) 圖:問候語 該應用以一條簡單消息響應。 在本教程中,我們使用 Stripes Web 框架創建了兩個簡單的 Web 應用。 您可能也對 ZetCode 的 [Java 教程](/lang/java/),[驗證過濾器教程](/java/validationfilter/), [Java MVC 教程](/java/mvc/), [Play 框架簡介](/java/play/)和 [Spark Java](/java/spark/), [Stripes,MyBatis & Derby 教程](/java/stripesmybatisderby/)或 [EJB 教程](/java/ejb/)感興趣。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看