美文网首页
Struts2学习笔记 | 部署第一个Struts2项目及其项目

Struts2学习笔记 | 部署第一个Struts2项目及其项目

作者: 一颗白菜_ | 来源:发表于2019-07-31 20:02 被阅读0次

1.Struct2概述

Struct2是一个用来开发MVC应用程序的框架


2.Struct2的环境配置

1.下载及解压

网址:https://struts.apache.org/

进去后下载下图文件,下载好后进行解压


在这里插入图片描述

2.建立一个项目(struts2)

在IDEA下新建一个项目

在这里插入图片描述

取个名字后创建成功

3.对该项目进行配置

1.新建lib文件夹和classes文件夹

在web/WEB-INF目录下新建这两个文件夹


在这里插入图片描述
2.打开Project Structure

点击Problems后如下图操作


在这里插入图片描述

在Modules中修改:


在这里插入图片描述
在Libraries中:
点击+号-->java-->选择刚刚创建的lib-->选择Jar Directory

结果为如下


在这里插入图片描述
3.配置Tomcat

配置如下:


在这里插入图片描述
在这里插入图片描述

4.更改web.xml

更改为

<?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.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

5.编写第一个Demo

1.新建一个class类
package com.struts2.helloworld;

public class Product {
    private Integer productId;
    private String productName;
    private String productDesc;
    private double productPrice;

    @Override
    public String toString() {
        return "Product{" +
                "productId=" + productId +
                ", productName='" + productName + '\'' +
                ", productDesc='" + productDesc + '\'' +
                ", productPrice=" + 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;
    }
    public String save(){
        System.out.println("save :" + this);
        return "details";
    }
}


2.编写三个jsp文件如下:

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>$Title$</title>
</head>
<body>
    <a href="product-input.action">Product Input</a>
</body>
</html>

input.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</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" />
    </form>
</body>
</html>

details.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    ProductId : ${productId}
    <br><br>
    ProductName : ${productName}
    <br><br>
    ProductDesc : ${productDesc}
    <br><br>
    ProductPrice : ${productPrice}
    <br><br>


</body>
</html>
3.配置struts2.xml文件
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <!--
        struct2使用package来组织模块。
        其中name属性是必须的,用于被其它的包引用当前包。可以随意取。
        extends指:当前包继承哪个包,即可以继承其中的所有配置。
        通常情况下继承struts-default。

        namespace是可选的,如果没有给出,则以/为默认值,
        若namespace有一个非默认值,则要想调用这个包里的Action,
        就必须把这个属性所定义的命名空间添加到有关的URI字符串里
        如果为默认值:http://localhost:8080/struct2-1/product-input.action
        设置为/cerr:http://localhost:8080/struct2-1/cerr/product-input.action
    -->
    <package name="helloworld" extends="struts-default" >
        <!-- 配置一个action:一个struct2的请求就是一个action -->

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

        <action name="product-save" class="com.struts2.helloworld.Product" method="save">
            <!-- 由product-save.ation跳转到com.struts2.helloworld.Product类的save方法(返回值为下面result中的name属性)-->
            <!-- 再由该类跳转到details.jsp-->
            <result name="details">/page/details.jsp</result>
        </action>
    </package>
</struts>


3.对struct2.xml进行解析

1.struct2使用package来组织模块

2.对以下demo进行解析

<package name="helloworld" extends="struts-default" namespace="/cerr">
    <!-- 由product-save.ation跳转到com.struts2.helloworld.Product类的save方法(返回值为下面result中的name属性)-->
    <!-- 再由该类跳转到details.jsp-->
    <action name="product-save" class="com.struts2.helloworld.Product" method="save">
        <result name="details" type="dispatcher">/page/details.jsp</result>
    </action>
</package>
1.packname节点

name属性是必须的,用于被其它的包引用当前包,命名自由

extends属性:当前包继承哪个包,即可以继承其中的所有配置。通常情况下继承struts-default。

namespace是可选的,如果没有给出,则以/为默认值,
若namespace有一个非默认值,则要想调用这个包里的Action,
就必须把这个属性所定义的命名空间添加到有关的URI字符串里
如果为默认值:http://localhost:8080/struct2-1/product-input.action
设置为/cerr后:http://localhost:8080/struct2-1/cerr/product-input.action

2.action节点

一个struct2的请求就是一个action

name:对于一个struts2的请求的名字(或对应ServletPath去除斜杆和扩展名),不包含扩展名

class的默认值为:com.opensymphony.xwork2.ActionSupport

method的默认值为:execute

表示为执行class类中的method方法,返回值为result节点中的name属性

3.result节点

result(结果):表示action方法执行后可能返回的一个结果,所以一个action节点可能有多个result子节点,多个result子节点使用name来区分。

names属性:标识一个result,和action方法的返回值对应,默认值为success

result节点的type:表示结果的类型,默认值为dispatcher(转发到结果)

相关文章

网友评论

      本文标题:Struts2学习笔记 | 部署第一个Struts2项目及其项目

      本文链接:https://www.haomeiwen.com/subject/xxlxdctx.html