Struts2的常見配置&Action的訪問


 struts2的概要

什么是Struts2?

  • struts2是一個基於mvc的web層框架,本質上相當於一個servlet。Struts 2以WebWork為核心,采用攔截器的機制來處理用戶的請求,這樣的設計也使得業務邏輯控制器能夠與ServletAPI完全脫離開,所以Struts 2可以理解為WebWork的更新產品。
  • Spring MVC也是一個web層的框架

web層框架基於前端控制器的設計

 

 struts2的入門例子

 創建web項目,導入需要的jar包,

1 創建一個jsp界面

<%@ page contentType="text/html;charset=UTF-8"  %>
<html>
  <head>
    <title>hi</title>
  </head>
  <body>
  <h1>struts2的入門</h1>
  <h3><a href="${pageContext.request.contextPath}/hello.action">struts2的入門</a></h3>
  </body>
</html>

 

2 編寫一個HelloAction的類

public class helloAction {
   public String execute(){
       System.out.println("hello被執行了");
       return "sucess";
   }
}

 

3 配置struts.xml(在src目錄下)

<?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="a" extends="struts-default" namespace="/" >

    <action name="hello" class="cn.itcast.demo.helloAction">
        <result name="sucess" >/sucess.jsp</result>
    </action>
</package>

</struts>

 

4 配置前端過濾器

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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>

5 跳轉成功的界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>sucess</title>
</head>
<body>
    <h1>恭喜!跳轉成功</h1>
</body>
</html>

 

struts2的執行流程

當用戶訪問某一個action的時候,先經過過濾器,然后再到攔截器 (這組攔截器實現了框架中的部分功能)
執行目標hello.action,在strtuts的配置文件中與hello對應上,根據name可以找到class類的全路徑從而找到對應的
Java類文件,后面根據反射執行。

加載順序
注意:后配置的常量的值會覆蓋先配置的常量的值

 常見配置

1 配置文件的加載順序

 

2 package相關配置

   package標簽稱為包,這個包與Java中的包的概念不一致。包為了更好管理action的配置。

   package標簽的屬性

    name                 :包的名稱,只有在一個項目中不重名即可。

   extends             :繼承哪個包,通常值為struts-default。

   namespace       :名稱空間,與<action>標簽中的name屬性共同決定訪問路徑。

   abstract            :抽象的,用於其他包的繼承。

3 action的相關配置

  action標簽的屬性

   name                 :與namespace共同決定訪問路徑

   class                  :Action類的全路徑

   method             :執行Action中的哪個方法的方法名,默認值execute

   converter          :用於設置類型轉換器

 Action的訪問

          action類繼承ActionSupport

import com.opensymphony.xwork2.ActionSupport;

public class demo3 extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("demo3執行了...");
        return NONE;
    }
}

 

通過通配符的方式進行配置(開發中這種方式用的最多)

   <!--通配符的配置-->
        <action name="product_*" class="cn.itcast.demo3.demo2" method="{1}"/>
 
        

 整合Hibernate和Struts2

1搭建開發環境

  (1)新建一個web工程,引入jar包,

  (2)配置hibernate

    映射文件   核心配置文件   日志文件

  (3)配置Struts2

    struts.xml     web.xml

2修改menu中列表的請求路徑

 <TR> 
     <TD class=menuSmall><A class=style2 href="/crm/customer_find.action"
         target=main>- 客戶列表</A></TD>

</TR>

編寫Action類

public class CustomerAction extends ActionSupport {
    public String find(){
        CustomerService CustomerService=new CustomerServiceimpl();
      List<Customer> list= CustomerService.find();
         ServletActionContext.getRequest().setAttribute("list",list);
        return "findSucess";
    }
}

 

此demo省略了很多步驟


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM