Struts2_搭建环境

上一篇:Struts2_使用 Filter 作为控制器的 MVC 应用的环境下进行如下操作:(没有 FileterDispatcher 类)
搭建 Struts2 的环境:
加入 jar 包: 复制 struts\apps\struts2-blank\WEB-INF\lib 下的所有 jar 包到当前 web 应用的 lib 目录下.
web.xml 文件中配置 struts2:
复制 struts\apps\struts2-blank1\WEB-INF\web.xml 文件中的过滤器的配置到当前 web 应用的 web.xml 文件中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <!-- 配置 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>
</web-app>

details.jsp页面不一样:

<body>

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

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

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

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

</body>

Product 类也不一样:

package com.atguigu.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";
        }

        public String test(){
              System.out.println("test");
              return "success";
        }

        public Product() {
              System.out.println("Product's constructor...");
        }
}

在当前 web 应用的 classpath 下添加 struts2 的配置文件 struts.xml:
复制 struts1\apps\struts2-blank\WEB-INF\classes 下的 struts.xml 文件到当前 web 应用的 src 目录下.
struts.xml文件中添加提示的方法:复制http://struts.apache.org/dtds/struts-2.3.dtd ,然后打开window=>preferences=>XML=>XML Catalog=>add=>粘贴到key中=>key type选择URI=>点击File System=>找到struts-2.3.4-all\struts-2.3.4\src\core\src\main\resources中的struts-2.3.dtd文件=>打开=>okok关闭,再打开

<struts>

    <!--  
        package: 包. struts2 使用 package 来组织模块. 
        name 属性: 必须. 用于其它的包应用当前包. 
        extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
                 struts-default 这个包在 struts-default.xml 文件中定义.
        namespace 可选, 如果它没有给出, 则以 / 为默认值. 
                                若 namespace 有一个非默认值, 则要想调用这个包里的Action, 
                                就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里

                  http://localhost:8080/contextPath/namespace/actionName.action
    -->
    <package name="helloWorld" extends="struts-default">

        <!-- 
            配置一个 action: 一个 struts2 的请求就是一个 action 
            name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
            class 的默认值为: com.opensymphony.xwork2.ActionSupport
            method 的默认值为: execute
            result: 结果. 
        -->
        <action name="product-input" 
            class="com.opensymphony.xwork2.ActionSupport"
            method="execute">
            <!--  
                result: 结果. 表示 action 方法执行后可能返回的一个结果. 所以一个 action 节点可能会有多个 result 子节点.
                多个 result 子节点使用 name 来区分
                name: 标识一个 result. 和 action 方法的返回值对应. 默认值为 success
                type: 表示结果的类型. 默认值为 dispatcher(转发到结果.)
            -->
            <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
        </action>

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

        <action name="test" class="com.atguigu.struts2.helloworld.Product" method="test">
            <result>/index.jsp</result>
        </action>

    </package>

</struts>

笔记:

1). 搭建 Struts2 的开发环境

2). 不需要显式的定义 Filter, 而使用的是 struts2 的配置文件.

3). details.jsp 比先前变得简单了.

${requestScope.product.productName} -> ${productName}

4). 步骤:

I. 由 product-input.action 转到 /WEB-INF/pages/input.jsp
struts2 中配置一个 action

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

II. 由 input.jsp 页面的 action: product-save.actionProduct's save, 再到 /WEB-INF/pages/details.jsp

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

Prodcut 中定义一个 save 方法, 且返回值为 details

猜你喜欢

转载自blog.csdn.net/qq_36901488/article/details/82284804
今日推荐