struts2教程--快速入门
Struts2框架介紹
1、三大框架 : 是企業主流 JavaEE開發的一套架構
Struts2 + Spring + Hibernate
2、 什么是框架?為什么要學框架 ?
框架 是 實現部分功能的代碼 (半成品),使用框架簡化企業級軟件開發
學習框架 ,清楚的知道框架能做什么?還有哪些工作需要自己編碼實現 ?
3、 什么是Struts2?
Struts2 是一款優秀MVC框架
MVC:是一種思想,是一種模式,將軟件分為 Model模型、View視圖、Controller控制器
?* MVC由來是web開發
JavaEE軟件三層結構 : web層(表現層)、業務邏輯層、數據持久層 (sun提供JavaEE開發規范)
JavaEE開發更強調三層結構, web層開發注重MVC
struts2 就是 web層開發框架,符合MVC模式
?* struts1 、webwork、jsf、SpringMVC都是MVC
4、 Struts2和 Struts1關系
沒有關系, Struts2 全新框架,引入WebWork很多技術和思想,Struts2保留Struts1類似開發流程
?* Struts2 內核 webwork ?
Xwork提供了很多核心功能:前端攔截機(interceptor),運行時表單屬性驗證,類型轉換,強大的表達式語言(OGNL– the Object Graph Navigation Language),IoC(Inversion of Control反轉控制)容器等
下面這是struts的一張架構圖
這些大概就是struts的核心的東西了。
Struts2快速入門
1、 下載開發包
這個在官網下載就可以了。
2、 目錄結構
apps : struts2官方demo ?
docs : ?文檔
lib : jar包
src : 源碼
3、 導入jar包到開發工程
只需要導入 apps/struts2-blank.war 中所有jar包 ?---- 13個jar包
4、 編寫頁面
hello.jsp 請求頁面
<a href="${pageContext.request.contextPath }/hello.action">訪問struts2入門</a>success.jsp 結果頁面
5、在web.xml配置struts2前端控制器 (Filter)
<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>??
6、執行struts2過濾器后,讀取struts2配置文件,將請求分發
在src下創建struts.xml
<package name="default" namespace="/" extends="struts-default"><!-- <a href="${pageContext.request.contextPath }/hello.action">訪問struts2入門</a> --><!-- 將請求 分發給一個Action --><!-- action的name 就是hello.action 去掉擴展名 --><action name="hello" class="com.sihai.struts2.demo1.HelloAction"></action></package>攔截器實現:
package com.sihai.filter;import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader;public class StrutsFilter implements Filter {public void init(FilterConfig filterConfig) throws ServletException {}public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException {// 1.強轉HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) resp;// 2.操作// 2.1 得到請求資源路徑String uri = request.getRequestURI();String contextPath = request.getContextPath();String path = uri.substring(contextPath.length() + 1);// System.out.println(path); // hello// 2.2 使用path去struts.xml文件中查找某一個<action name=path>這個標簽SAXReader reader = new SAXReader();try {// 得到struts.xml文件的document對象。Document document = reader.read(new File(this.getClass().getResource("/struts.xml").getPath()));Element actionElement = (Element) document.selectSingleNode("//action[@name='" + path + "']"); // 查找<action// name='hello'>這樣的標簽if (actionElement != null) {// 得到<action>標簽上的class屬性以及method屬性String className = actionElement.attributeValue("class"); // 得到了action類的名稱String methodName = actionElement.attributeValue("method");// 得到action類中的方法名稱。// 2.3通過反射,得到Class對象,得到Method對象Class actionClass = Class.forName(className);Method method = actionClass.getDeclaredMethod(methodName);// 2.4 讓method執行.String returnValue = (String) method.invoke(actionClass.newInstance()); // 是讓action類中的方法執行,并獲取方法的返回值。// 2.5// 使用returnValue去action下查找其子元素result的name屬性值,與returnValue做對比。Element resultElement = actionElement.element("result");String nameValue = resultElement.attributeValue("name");if (returnValue.equals(nameValue)) {// 2.6得到了要跳轉的路徑。String skipPath = resultElement.getText();// System.out.println(skipPath);request.getRequestDispatcher(skipPath).forward(request,response);return;}}} catch (DocumentException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 3.放行chain.doFilter(request, response);}public void destroy() {}}7、執行目標Action中execute方法?
8、在Action的execute方法中返回 字符串,在struts.xml中配置字符串與頁面對應關系
<result name="executesuccess">/demo1/success.jsp</result> ?完成結果頁面跳轉
下面是具體的源代碼
helloAction.java:
package com.sihai.action;public class HelloAction {public String say() {System.out.println("hello action say method");return "good";} } com.sihai.action;public class HelloAction {public String say() {System.out.println("hello action say method");return "good";} }struts.xml
<?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="default" namespace="/" extends="struts-default"><action name="hello" class="com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package><package name="dd" namespace="/abc" extends="struts-default"><action name="hello" class="com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package> </struts> com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package><package name="dd" namespace="/abc" extends="struts-default"><action name="hello" class="com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package> </struts>Struts2流程分析與工具配置
1、 運行流程
請求 ---- StrutsPrepareAndExecuteFilter核心控制器 ----- Interceptors攔截器(實現代碼功能 ) ----- Action的execuute ---結果頁面 Result
* 攔截器 在 struts-default.xml定義
* 執行攔截器 是 defaultStack中引用攔截器
2、 配置struts.xml提示問題
?如果安裝Aptana編輯器 ,請不要用Aptana自帶xml編輯器 編寫struts2配置文件
?struts.xml提示來自于 DTD約束,
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
如果可以上網,自動緩存dtd,提供提示功能
如果不能上網,也可以配置本地DTD提示
3、 關聯struts2源碼
關聯 zip包
4、 Config Brower插件使用
提供在瀏覽器中查看 struts2 配置加載情況
將解壓struts2/lib/struts2-config-browser-plugin-2.3.7.jar復制WEB-INF/lib下
訪問 http://localhost:8080/struts2_day1/config-browser/index.action查看 struts2配置加載情況
?
總結
以上是生活随笔為你收集整理的struts2教程--快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝桥杯java第八届第四题--魔方状态
- 下一篇: struts2教程(2)--配置