導讀跟大家講解下有關struts2是什么?如何使用?,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說struts2是什么?如何使用?,小
跟大家講解下有關struts2是什么?如何使用?,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說struts2是什么?如何使用?,小編也收集到了有關struts2是什么?如何使用?的相關資料,希望大家看到了會喜歡。
一、struts2是什么
1.概念

2.struts2使用優勢以及歷史

二、搭建struts2框架
1.導包
(解壓縮)struts2-blank.war就會看到

2.書寫Action類


public class HelloAction {public String hello(){ System.out.println("hello world!"); return "success"; }}View Code
3.書寫src/struts.xml (記得加上dtd約束)


<?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="hello" namespace="/hello" extends="struts-default" ><action name="HelloAction" class="cn.itheima.a_hello.HelloAction" method="hello" ><result name="success" type="dispatcher" >/hello.jsp</result></action></package></struts>View Code
4.將struts2核心過濾器配置到web.xml
filter-class記不住可以ctrl+shift+t 輸入strutsPre來查找全類名


<!-- 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>View Code
5.測試

三、struts2訪問流程&struts2架構
1.struts2架構

2.Aop編程(面向切面)

四、配置詳解
1.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><!-- i18n:國際化. 解決post提交亂碼 --><constant name="struts.i18n.encoding" value="UTF-8"></constant><!-- 指定反問action時的后綴名 http://localhost:8080/struts2_day01/hello/HelloAction.do--><constant name="struts.action.extension" value="action"></constant><!-- 指定struts2是否以開發模式運行 1.熱加載主配置.(不需要重啟即可生效) 2.提供更多錯誤信息輸出,方便開發時的調試 --><constant name="struts.devMode" value="true"></constant><!-- package:將Action配置封裝.就是可以在Package中配置很多action. name屬性: 給包起個名字,起到標識作用.隨便起.不能其他包名重復. namespace屬性:給action的訪問路徑中定義一個命名空間 extends屬性: 繼承一個 指定包 abstract屬性:包是否為抽象的; 標識性屬性.標識該包不能獨立運行.專門被繼承 --><package name="hello" namespace="/hello" extends="struts-default" ><!-- action元素:配置action類 name屬性: 決定了Action訪問資源名. class屬性: action的完整類名 method屬性: 指定調用Action中的哪個方法來處理請求 --><action name="HelloAction" class="cn.itheima.a_hello.HelloAction" method="hello" ><!-- result元素:結果配置 name屬性: 標識結果處理的名稱.與action方法的返回值對應. type屬性: 指定調用哪一個result類來處理結果,默認使用轉發. 標簽體:填寫頁面的相對路徑--><result name="success" type="dispatcher" >/hello.jsp</result></action></package><!-- 引入其他struts配置文件 --><include file="cn/itheima/b_dynamic/struts.xml"></include><include file="cn/itheima/c_default/struts.xml"></include></struts>View Code
2.struts2常量配置
2.1 struts2默認常量配置位置

2.2 修改struts2常量配置(方式先后也是加載順序)
方式1:src/struts.xml(重要)
<!-- i18n:國際化. 解決post提交亂碼 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant>
方式2:在src下創建struts.properties
struts.i18n.encoding=UTF8
方式3:在項目的web.xml中
<context-param> <param-name>struts.i18n.encoding</param-name> <param-value>UTF-8</param-value> </context-param>
順序:

2.3 常量配置


<!-- i18n:國際化. 解決post提交亂碼 --><constant name="struts.i18n.encoding" value="UTF-8"></constant><!-- 指定反問action時的后綴名 http://localhost:8080/struts2_day01/hello/HelloAction.do--><constant name="struts.action.extension" value="action"></constant><!-- 指定struts2是否以開發模式運行 1.熱加載主配置.(不需要重啟即可生效) 2.提供更多錯誤信息輸出,方便開發時的調試 --><constant name="struts.devMode" value="true"></constant>View Code
3.struts2配置的進階
3.1動態方法調用(重要)


<!-- 配置動態方法調用是否開啟常量 默認是關閉的,需要開啟 --><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>方式一


<!-- 動態方法調用方式2:通配符方式 使用{1} 取出第一個星號通配的內容 --><action name="Demo1Action_*" class="cn.itheima.b_dynamic.Demo1Action" method="{1}" ><result name="success" >/hello.jsp</result></action>方式二
3.2struts2中的默認配置(了解)


<package name="default" namespace="/default" extends="struts-default" ><!-- 找不到包下的action,會使用Demo2Action作為默認action處理請求 --><default-action-ref name="Demo2Action"></default-action-ref><!-- method屬性:execute --><!-- result的name屬性:success --><!-- result的type屬性:dispatcher 轉發 --><!-- class屬性:com.opensymphony.xwork2.ActionSupport --><action name="Demo2Action" ><result >/hello.jsp</result></action></package>View Code
五、action類詳解
Action類的書寫方式


//方式1: 創建一個類.可以是POJO//POJO:不用繼承任何父類.也不需要實現任何接口.//使struts2框架的代碼侵入性更低.public class Demo3Action {}方式一


//方式2: 實現一個接口Action// 里面有execute方法,提供action方法的規范.// Action接口預置了一些字符串.可以在返回結果時使用.為了方便public class Demo4Action implements Action { @Overridepublic String execute() throws Exception {return null; }}方式二


//方式3: 繼承一個類.ActionSupport// 幫我們實現了 Validateable, ValidationAware, TextProvider, LocaleProvider .//如果我們需要用到這些接口的實現時,不需要自己來實現了.public class Demo5Action extends ActionSupport{}方式三
六、練習:客戶列表
圖解:

實現:


public class CustomerAction extends ActionSupport {private CustomerService cs = new CustomerServiceImpl(); public String list() throws Exception {//1 接受參數String cust_name = ServletActionContext.getRequest().getParameter("cust_name");//2 創建離線查詢對象DetachedCriteria dc =DetachedCriteria.forClass(Customer.class);//3 判斷參數拼裝條件if(StringUtils.isNotBlank(cust_name)){ dc.add(Restrictions.like("cust_name", "%"+cust_name+"%")); }//4 調用Service將離線對象傳遞List<Customer> list = cs.getAll(dc);//5 將返回的list放入request域.轉發到list.jsp顯示ServletActionContext.getRequest().setAttribute("list", list);return "list"; }}Web層代碼


public List<Customer> getAll(DetachedCriteria dc) { Session session = HibernateUtils.getCurrentSession();//打開事務Transaction tx = session.beginTransaction(); List<Customer> list = customerDao.getAll(dc); //關閉事務 tx.commit();return list; }Service層代碼


public List<Customer> getAll(DetachedCriteria dc) {//1 獲得sessionSession session = HibernateUtils.getCurrentSession();//2 將離線對象關聯到sessionCriteria c = dc.getExecutableCriteria(session);//3 執行查詢并返回return c.list(); }Dao層代碼
以上就是struts2是什么?如何使用?的詳細內容,更多請關注php中文網其它相關文章!
來源:php中文網
免責聲明:本文由用戶上傳,如有侵權請聯系刪除!