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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Struts2 HelloWorld 注解示例 > 原文: [https://howtodoinjava.com/struts2/struts-2-hello-world-with-annotations/](https://howtodoinjava.com/struts2/struts-2-hello-world-with-annotations/) 在我以前的文章中,我寫了許多關于 [**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 helloworld 和 xml 配置**](//howtodoinjava.com/struts-2/struts-2-hello-world-example-application/ "Struts2 hello world example application") 相關的帖子。 在本文中,我將更新先前的示例,以使用注解來配置 Struts2 應用。 有關信息,struts 注解是 [**struts 常規插件**](https://struts.apache.org/docs/convention-plugin.html "Struts2 convention plugin")的一部分。 ```java Sections in this post: Create maven web project Struts2 dependencies web.xml changes 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 -DartifactIad=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> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.15.1</version> </dependency> </dependencies> ``` ## `web.xml`的更改 需要以某種方式將 Struts 插入您的 Web 應用。 這意味著應將對應用的傳入請求移交給 strut 進行處理。 這是通過在`web.xml`文件中添加過濾器定義來完成的。 此篩選器實質上將所有傳入請求重定向到 `StrutsPrepareAndExecuteFilter`,然后使用配置來處理該請求。 另外,我傳遞了“`actionPackages`”初始化參數,以便可以掃描此包以查找必需的帶注解的類。 ```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> <init-param> <param-name>actionPackages</param-name> <param-value>com.howtodoinjava.struts2.example.web</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> ``` ## 編寫第一個類 這很重要,因為您將在此處編寫實際的應用邏輯。 Struts2 操作通常擴展`ActionSupport`類,該類提供了一些方法來覆蓋和更改應用流,并在兩者之間注入業務邏輯。 ```java @Namespace("/default") @ResultPath(value="/") @Results({ @Result(name="success", location="WEB-INF/jsp/success.jsp"), @Result(name="input", location="WEB-INF/jsp/index.jsp") }) public class TestAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; private Date nowDate; @Override public void validate(){ if (name==null || name.length()==0) addActionError(getText("error.enter.message")); } @Actions({ @Action("/"), @Action("/test") }) @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="test" 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> ``` 我正在使用的消息資源文件是: **`TestAction.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) **項目**的文件/文件夾樹 ![struts-folder-tree](https://img.kancloud.cn/e0/d6/e0d644d92f3d7cd10939dcdb205e2fd7_366x399.png) 就是這個帶有注解的 Struts2 helloworld 應用的朋友。 如果要**下載本教程的源代碼**,請按照以下給定的下載鏈接進行操作。 [**源碼下載**](https://docs.google.com/file/d/0B7yo2HclmjI4Q0NZUlgxbm90Vk0/edit?usp=sharing "Struts2 annotations hello world source code") **學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看