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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Struts2 HelloWorld 示例 > 原文: [https://howtodoinjava.com/struts2/struts-2-hello-world-example-application/](https://howtodoinjava.com/struts2/struts-2-hello-world-example-application/) 在我以前的文章中,我寫了許多關于 [**JAX-RS RESTEasy**](//howtodoinjava.com/restful-web-service/ "resteasy tutorials"),[**Spring3**](//howtodoinjava.com "Spring3 tutorials"),**Hibernate** 和其他 Java 框架,例如 [**Maven**](//howtodoinjava.com/maven/ "maven tutorials") 或 [**junit**](//howtodoinjava.com/junit/ "junit tutorials") 的示例和教程。 我也有很多要求在 **Struts2** 上寫一些東西。 好吧,這里我從世界示例開始。 在下一篇文章中,我將嘗試涵蓋涉及 Struts2 的最大領域和概念。因此,請繼續關注。 ```java Sections in this post: Create maven web project Struts2 dependencies web.xml changes Know struts.xml configuration file Using struts.properties file Writing first action class Composing view files Testing the application 源碼下載 ``` ## 創建 Maven Web 項目 我在這里不會吃太多空間。 您可以閱讀有關[**如何創建 Maven Eclipse Web 項目**](//howtodoinjava.com/maven/how-to-create-a-eclipse-web-application-using-maven/ "How to create a eclipse web application using maven")的更多詳細信息。 簡而言之,使用以下命令。 ```java mvn archetype:generate -DgroupId=com.howtodoinjava.struts2.example -DartifactId=struts2example -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false cd struts2example mvn eclipse:eclipse -Dwtpversion=2.0 ``` ## Struts2 依賴項 我正在使用 maven 導入 Struts2 運行時依賴項。 這樣做的好處是,您無需手動記住和尋找必需的依賴項,一舉就能掌握所有內容。 ```java <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.15.1</version> </dependency> ``` 如果您想查看所有包含的內容,請查看下圖:(獲取最新版本的 jar 文件) ![Struts2 jar files](https://img.kancloud.cn/76/b8/76b86dd53136e2a8deb2502a034a02dd_358x202.png) Struts2 jar 文件 ## 修改后的`web.xml` 需要以某種方式將 Struts 插入您的 Web 應用。 這意味著應將對應用的傳入請求移交給 strut 進行處理。 這是通過在`web.xml`文件中添加過濾器定義來完成的。 此篩選器實質上將所有傳入請求重定向到[**`StrutsPrepareAndExecuteFilter`**](https://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/dispatcher/filter/StrutsPrepareAndExecuteFilter.html "StrutsPrepareAndExecuteFilter"),然后使用配置來處理該請求。 ```java <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> ``` ## 了解`struts.xml`配置文件 因此,`StrutsPrepareAndExecuteFilter`有一個要處理的請求。 怎么辦? 它將使用該配置來知道如何處理特定請求。 此配置在 struts.xml 文件中定義。 該文件將具有特定于應用工作流及其操作處理器的 url 映射。 它還定義了輸入/成功/錯誤視圖。 ```java <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <package name="default" extends="struts-default"> <action name=""> <result>/WEB-INF/jsp/index.jsp</result> </action> <action name="testAction" class="com.howtodoinjava.struts2.example.web.TestAction"> <result name="input">/WEB-INF/jsp/index.jsp</result> <result name="success">/WEB-INF/jsp/success.jsp</result> <result name="error">/WEB-INF/jsp/index.jsp</result> </action> </package> </struts> ``` ## 使用`struts.properties`文件 Struts 使用一些默認屬性在運行時配置其行為。 您可以在`stuts.properties`文件中覆蓋這些默認值。 ```java #see http://struts.apache.org/2.0.14/docs/strutsproperties.html struts.devMode=true //message resource struts.custom.i18n.resources=global ``` ## 編寫第一個動作類 這很重要,因為您將在此處編寫實際的應用邏輯。 Struts2 動作通常擴展[**`ActionSupport`**](https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/ActionSupport.html "ActionSupport")類,這些類提供了一些方法來覆蓋和更改應用流,并在兩者之間注入業務邏輯。 ```java package com.howtodoinjava.struts2.example.web; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class TestAction extends ActionSupport { private String name; private Date nowDate; @Override public void validate(){ if (name==null || name.length()==0) addActionError(getText("error.enter.message")); } @Override public String execute() throws Exception { nowDate = new Date(); return ActionSupport.SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getNowDate() { return nowDate; } } ``` **注意**: Struts2 動作看起來像 POJO 類,因為它們必須充當動作形式,它們也是 Struts1 中的單獨實體。 ## 構建視圖文件 這是一般步驟,涉及編寫視圖層,例如,在我們的例子中,我們正在編寫 jsp 文件。 您可以使用消息資源從屬性文件中獲取消息,稍后在 i18n 中提供幫助。 **`index.jsp`** ```java <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Struts2 hello world example</title> <s:head/> </head> <body> <h1><s:text name="welcome" /></h1> <s:if test="hasActionErrors()"> <div id="fieldErrors"> <s:actionerror/> </div> </s:if> <s:form action="testAction" namespace="/" method="post" name="myForm" theme="xhtml"> <s:textfield name="name" size="40" maxlength="40" key="your.message-label"/> <s:submit key="submit" /> </s:form> </body> </html> ``` **`success.jsp`** ```java <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Success Screen !!</title> </head> <body> <h2>Thank you for running this demo on <s:property value="nowDate" /></h2> <p> Your name recorded was: <h3><s:property value="name" /></h3> </p> </body> </html> ``` 他們正在使用的消息資源文件是: **`global.properties`** ```java submit=Submit your.message-label=Your Name welcome=Welcome to Struts2! error.enter.message=Please enter your name !! ``` ## 測試應用 現在好運行我們的 helloworld 應用。 讓我們點擊瀏覽器。 **鍵入`http://localhost:8080/struts2example/`,然后按`Enter`** ![struts2-hello-world-1](https://img.kancloud.cn/1d/1d/1d1de2b59e1ac4de0d9fc77afe55703e_445x255.png) **按提交而不輸入任何內容** ![struts2-hello-world-2](https://img.kancloud.cn/2b/38/2b3833d651141d1aac87119bef8a1901_492x291.png) **輸入您的姓名,然后按提交** ![struts2-hello-world-3](https://img.kancloud.cn/8a/88/8a8848376b22d005ee4504f2589d0892_518x223.png) 這就是 Struts2 helloworld 應用的全部朋友。 如果要下載本教程的源代碼,請遵循以下給定的下載鏈接。 [**源碼下載**](https://docs.google.com/file/d/0B7yo2HclmjI4d2sxRGtSS1pONEE/edit?usp=sharing "Struts2 hello world 源碼下載") **學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看