創建struts2支持的第一個應用(二)


下載struts2

網址:http://struts.apache.org/download.cgi

一般下載Full Distribution完整all版本,該版本目錄結構:

這裏寫圖片描述

  • apps裏包含struts2示例應用
  • docs包含struts2相關文檔如API文檔
  • lib包含struts2框架核心類庫,和第三方插件庫
  • src包含struts2全部源代碼


搭建struts2環境

  1. 添加jar 包: 將struts\apps\struts2-blank\WEB-INF\lib 下的所有 jar 包複製到當前 web 應用的 lib 目錄下;
  2. web.xml 文件中配置 struts2: 將 struts\apps\struts2-blank1\WEB-INF\web.xml 文件中的過濾器的配置代碼複製到當前 web 應用的 web.xml 文件中;
  3. 添加struts2 的配置文件struts.xml: 複製 struts1\apps\struts2-blank\WEB-INF\classes 下的 struts.xml 文件到當前 web 應用的 src 目錄下。

注:爲了在eclipse下能正常提示struts2語法,要添加DTD約束。

這裏寫圖片描述

struts-2.3.28\src\core\src\main\resources\struts-2.3.dtd這裏寫圖片描述

創建第一個struts2應用

應用效果:

這裏寫圖片描述這裏寫圖片描述這裏寫圖片描述

代碼:
index.jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <a href="product-input.action">Product Input</a>

</body>
</html>

input.jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <form action="product-save.action" method="post">

        ProductName: <input type="text" name="productName"/>
        <br><br>

        ProductDesc: <input type="text" name="productDesc"/>
        <br><br>

        ProductPrice: <input type="text" name="productPrice" />
        <br><br>

        <input type="submit" value="Submit"/>
        <br><br>

    </form>

</body>
</html>

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="helloWorld" extends="struts-default">

    <action name="product-input">
      <result>/WEB-INF/pages/input.jsp</result>         
    </action>

    <action name="product-save" class="com.afy.struts2.helloworld.Product" method="save">
      <result name="details">/WEB-INF/pages/details.jsp</result>
     </action>

  </package>

</struts>

Product.java類和save方法:

package com.afy.struts2.helloworld;

public class Product {

    private Integer productId;
    private String productName;
    private String productDesc;

    private double productPrice;

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productName="
                + productName + ", productDesc=" + productDesc
                + ", productPrice=" + productPrice + "]";
    }

    public String save(){
        System.out.println("save:" + this);
        return "details";
    }
}

details.jsp頁面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    ProductId: ${productId }
    <br><br>

    ProductName: ${productName }
    <br><br>

    ProductDesc: ${productDesc }
    <br><br>

    ProductPrice: ${productPrice }
    <br><br>

</body>
</html>


重點代碼塊簡析

這裏主要簡析一下struts.xml文件中的配置代碼

<struts>

  <package name="helloWorld" extends="struts-default">

    <action name="product-input">
      <result>/WEB-INF/pages/input.jsp</result>         
    </action>

    <action name="product-save" class="com.afy.struts2.helloworld.Product" method="save">
      <result name="details">/WEB-INF/pages/details.jsp</result>
     </action>

  </package>

</struts>


先從< packege>標籤及其裏面的屬性開始,
1.< packege>: 包,用於組織模塊。一個項目會有多個模塊,可以把每個模塊分給一個包,一個struts.xml文件可以有多個< packege>標籤;
2.< name>: 包的名字,必須的,以便其他包應用該包;
3.< extends>: 包的繼承功能,指定當前包繼承其他包,這樣當前包便可以繼承特定包所有的配置。通常情況要繼承struts-default包,struts的核心攔截器都配置在struts-default包中,如果沒有繼承該包,所有請求都會請求不到。
4.< namespace>: 可選,若未指定則爲默認值 / ;若指定其值,則調用包裏Action時,就必須把所定義的命名空間值加到相關URI字符串裏:http//localhost:8080/contextPath/namespace/actionName.action

再來看< package>之下的子標籤< action>及其裏面的屬性標籤
1.< action>: 一個struts2的請求就是一個action,一個< action>標籤對應一個action類;
2.< name>: action的名字,即對應struts2請求的名字;
3.< class>:默認值爲
com.opensymphony.xwork2.ActionSupport;
4.< method>: 可以指定執行action中某方法,默認值爲execute

接着是< action>之下的子標籤< result>及其屬性標籤
1.< result>: 結果,表示action方法可能返回的結果。一個action節點可能會有多個result子節點,多個result子節點由name區分;
2.< name>: result的標識名,對應action中< method>的具體方法返回值。默認是success。
3.< type>: 表示結果的類型,默認值爲dispatcher(轉發到結果)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章