Struts 學習總結
一、? struts2 簡介
struts2 官網地址: http://struts.apache.org/
struts2 官方開發介紹 :? http://struts.apache.org/2.0.14/docs/core-developers-guide.html
struts 官方開發介紹本地地址 :?
F:\ 編程資料 \ struts\struts-2.1.6-all\struts-2.1.6\docs\docs\guides.html (包括了所有使用的信息 , 相當重要)
F:\ 編程資料 \ struts\struts-2.1.6-all\struts-2.1.6\docs\docs\home.html
學習計劃 : 首先將電子書看了再看文檔。
在 Struts2 中,模型 - 視圖 - 控制器模式通過五個核心組件來實現 —— Action 、攔截器?值棧 /OGNL ?結果類型和結果 / 視圖技術。
在 “ struts.properties ” 文件中定義的屬性都可以在 “ web.xml ” 文件的 “ init-param ” 標簽中進行配置,或者通過 “ struts.xml ” 文件中的 “ constant ” ? 標簽來修改(我們在下一章中會繼續討論這個標簽)。
?? struts.properties 文件通常放在 Web 應用的 WEB-INF/classes 路徑下。實際上,只要將該文件放在 Web 應用的 CLASSPATH 路徑下, Struts?2 框架就可以加載該文件。
Struts2 使用了名為依賴注入 5—— 又名控制反轉 —— 的技術來降低系統的耦合性。依賴注入可以通過構造器注入,接口注入和 setter 注入來實現。 Struts2 中用的是 setter 注入。這就是說,你只需要提供一個 setter ,對應的對象就可以被 Action 使用了。 Struts2 推薦的依賴注入框架是 Spring 框架,并通過插件對它進行配置。你還可以使用 Plexus ,或者是提供自定義的實現。
在 struts 中獲得 HttpRequest 對象 :
Inteceptor 中獲取 HttpServletRequest :
? ActionContext?ac?=?invocation.getInvocationContext();
????????HttpServletRequest?request?=?(HttpServletRequest)?ac.get(ServletActionContext. HTTP_REQUEST );
Action 等其他類中獲取 HttpServletRequest :
HttpServletRequest?request?=?ServletActionContext. getRequest ();
String? userNameReq ?=?request.getParameter( "userName" );
配置 struts 應用程序步驟說明 :
1、? 導入 struts 應用程序需要的 jar 包 (struts2 項目一般需要的 jar) 。
2、? 配置 web.xml 文件 :
? < filter >
< filter-name > struts2 </ filter-name >
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
3、? 在 src 路徑下配置 struts.xml 初始文件。
? <! 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" />
</ struts >
4、? 在 WebContent 路徑下新建 html 文件,看是否能夠訪問。如果報 404 證明工程搭建有問題。
5、? 成功訪問,則創建對應的 jsp 文件和在 struts.xml 中配置對應的 action 等。在瀏覽器中訪問對應的 action 時,如果采用標簽則應該如下訪問 :
<package?name="tutorial"?namespace="/"?extends="struts-default">
頁面 form 配置如下 :
< s:form? action = "login"? namespace = "/" >
User?id : < input? type = "text"? name = "userId"? /> ? < br />
Password : ? < input? type = "password"? name = "passwd"? /> ? < br? />
< input? type = "submit"? value = "Login" />
</ s:form >
如果采用 html 標記則配置如下 :
<form?action="<%=request.getContextPath()?/login.action">
注意 : 可以通過查看網頁源碼來看 form 的 action 是否配置正確。
6、? 相關 Struts 書籍及資料
F:\ 編程資料 \ struts\struts-2.1.6-all\struts-2.1.6\docs\docs\other-resources.html
示例程序導航 :?
Apache?Struts?2?Documentation?>?Home?>?Tutorials?>?Bootstrap?>?Hello?World
一、? Bootstrap( 引導 )
標簽的使用 :<s:url> 的使用 .
<s:url?var= "url" ?action= "Welcome" >
???????????? <s:param?name= "request_locale" > en </s:param>
???????? </s:url>
<s:a?href= "%{url}" > English </s:a>
在 struts.xml 中 action 配置可以不用配置 class.
<action?name= "Welcome" ?>
?? <result>/Welcome.jsp</result>
</action>
? <default-action-ref> 用于在沒有找到 action 的時候調用。
Struts 標簽使用說明 :
<s:set> 標簽設值,如果需要設置常量,需要在 value=”’’” 內部添加單引號。
<s:set?var="name"?value="'xieyun'"?></s:set>
動態傳遞參數
globalMessages_en_US.properties 配置如下 :myParam=xie-{0}
頁面上調用如下 :
< s:text? name = "myParam" >
? < s:param > callan </ s:param >
</ s:text >
浪曦視頻教程總結
一、基礎知識說明
在 action 使用中,可以不繼承 ActionSupport 類或實現 Action 接口。 Struts2 能夠通過 struts.xml 文件中聲明的 action 來反射解析 Action 類,但是該類必須包含一個返回字符串的 execute 方法。如果不聲明 execute 方法會報錯誤如下 :
java.lang.NoSuchMethodException:?com.test.action.LoginAction.execute()
java.lang.Class.getMethod(Unknown?Source)
即使我們在 struts.xml 文件的 action 中申明一個 method 屬性,指定一個方法,也會報上面的錯誤。
二、 struts2 類型轉換
1 、 struts 中的錯誤驗證
???Action 繼承 ActionSupport 后, ActionSupport 里面有一個 Validate() 方法,該方法負責在執行 execute() 方法前進行一些輸入參數的簡單驗證,如果我們在該方法類添加了錯誤信息 (addFieldError("userName","userName?is?required");) ,那么我們的 result 將會執行一個 input 結果,跳轉到提交的頁面 ( 該 input 結果需要用戶自己在 struts.xml 中進行配置 ) 。
示例程序如下 :
Login.jsp
<%@? page? language = "java"? contentType = "text/html;?charset=UTF-8"
???? pageEncoding = "UTF-8" %>
<%@? taglib? prefix = "s"?? uri = "/struts-tags" %>
<! DOCTYPE? html? PUBLIC? "-//W3C//DTD?HTML?4.01?Transitional//EN"? "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
發表評論
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

評論