日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2 简介

發布時間:2024/1/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2 简介 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

回顧Struts2的使用過程,網上搜的教程多多少少都會有點問題,重新記錄下創建過程,方便查閱。

1、下載Struts2的jar包

下載地址:http://archive.apache.org/dist/struts/binaries/

我用的是struts-2.3.14-all.zip這個版本

2、創建一個web project項目

下面給出所有文件均創建完成后的工程師圖。

?

3、導入Struts2所需jar包

因為只是示例程序,只需要導入Struts 2支持最小的包就可以了,網上很多教程中添加的最小包都有出入,教大家一個保險的方法。

解壓剛才下載的壓縮包struts-2.3.14-all.zip,在apps文件夾下有個struts2-blank.war包,打開它,到WEB-INF/lib目錄下,如下圖所示,即為所需的最小包。包含的包應該和具體的Struts版本有關。

?

4、配置web.xml

下面進入到具體的配置編碼階段。

打開web.xml,修改配置參數,修改后的具體配置如下。

?

[html]?view plaincopyprint?
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <web-app?version="2.5"???
  • ????xmlns="http://java.sun.com/xml/ns/javaee"???
  • ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
  • ????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee???
  • ????http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">??
  • ??<display-name></display-name>???
  • ????
  • ??<!--?Struts2配置?-->??
  • ??<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>??
  • 這里需要注意的是

    這里面填入的類,?

    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilterand StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one..即,從Struts 2.1.3起已被標注為過時的,改用StrutsPrepareAndExecuteFilter。
    我剛用這個版本的時候還是填的org.apache.struts2.dispatcher.FilterDispatcher
    結果報錯

    [plain]?view plaincopyprint?
  • ***********************************************************************??
  • *???????????????????????????????WARNING!!!????????????????????????????*??
  • *?????????????????????????????????????????????????????????????????????*??
  • *?>>>?FilterDispatcher?<<<?is?deprecated!?Please?use?the?new?filters!?*??
  • *?????????????????????????????????????????????????????????????????????*??
  • *???????????This?can?be?a?source?of?unpredictable?problems!???????????*??
  • *?????????????????????????????????????????????????????????????????????*??
  • *??????????????Please?refer?to?the?docs?for?more?details!?????????????*??
  • *????????????http://struts.apache.org/2.x/docs/webxml.html????????????*??
  • *?????????????????????????????????????????????????????????????????????*??
  • ***********************************************************************??

  • 如果你也遇到如上的錯誤,要仔細再檢查下了。

    ?

    ?

    5、配置struts.xml

    下面需要創建struts.xml文件,配置strust2要調用的action。直接新建在src目錄下,那樣部署的時候會自動發布到WEB-INF/classes目錄下,或者直接創建在WEB-INF/classes目錄下面。

    [html]?view plaincopyprint?
  • <?xml?version="1.0"?encoding="UTF-8"??>??
  • <!DOCTYPE?struts?PUBLIC??
  • ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.3//EN"??
  • ????"http://struts.apache.org/dtds/struts-2.3.dtd">??
  • ??
  • ??
  • ????<struts>??
  • ????????<package?name="struts2"?extends="struts-default">??
  • ????????????<action?name="HelloWorld"?class="tutorial.HelloWorld">??
  • ????????????????<result>/HelloWorld.jsp</result>??
  • ????????????</action>??
  • ????????</package>??
  • ????</struts>??

  • 其中,package元素,作用類似于Java包的機制,他是用于分門別類的一個工具,extends屬性如他的名字一樣,它繼承了struts-default這個包的所有信息,一般我們自己創建一個包最好都繼承它,因為他為我們提供了絕大部分的功能,你可以在struts2-core的jar包中的struts-default.xml文件中找到這個包。action元素對應與你的表單,例如你的表單的action="welcome",那么該表單提交后就會將參數交予action的name="welcome"的實現類處理。result元素為action的結果,它由動作類返回的控制字段選擇。

    6、寫action類(HelloWorld.java)

    這個類主要用于struts2跳轉到這個action后。默認執行execute()方法。并根據結果返回字符,然后struts.xml根據返回的字符跳到相應的頁面

    [java]?view plaincopyprint?
  • package?tutorial;??
  • import?com.opensymphony.xwork2.ActionSupport;??
  • ??
  • public?class?HelloWorld?extends?ActionSupport??
  • {??
  • ????public?final?static?String?MESSAGE?=?"Struts2?is?up?and?running?...";??
  • ??????
  • ????private?String?message;??
  • ??
  • ??
  • ????/**?
  • ?????*?@return?the?message?
  • ?????*/??
  • ????public?String?getMessage()??
  • ????{??
  • ????????return?message;??
  • ????}??
  • ??
  • ??
  • ????/**?
  • ?????*?@param?message?the?message?to?set?
  • ?????*/??
  • ????public?void?setMessage(String?message)??
  • ????{??
  • ????????this.message?=?message;??
  • ????}??
  • ??
  • ??
  • ????public?String?execute()?throws?Exception??
  • ????{??
  • ????????setMessage(MESSAGE);??
  • ????????return?SUCCESS;??
  • ????}??
  • }??
  • 7、寫jsp頁面

    新建一個jsp頁面來呈現信息。

    ?

    [plain]?view plaincopyprint?
  • <%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%>??
  • <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
  • ??
  • <%@?taglib?prefix="s"?uri="/struts-tags"?%>??
  • ??
  • <html>??
  • ??<head>??????
  • ????<title>Hello?World!</title>??????
  • ??</head>??
  • ????
  • ??<body>??
  • ????<h2><s:property?value="message"?/></h2>??
  • ??</body>??
  • </html>??
  • ?

    ?

    8、部署運行


    在Tomcat中運行該項目,然后打開瀏覽器,在地址欄中輸入:http://localhost:8080/Struts2Demo/HelloWorld
    IE效果如下。

    ?

    至此,最簡單的Struts2的使用方法介紹完畢。

    轉載于:https://www.cnblogs.com/tongtkk/p/4957539.html

    總結

    以上是生活随笔為你收集整理的Struts2 简介的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。