搭建一个Struts2工程

1、使用Eclipse创建一个Dynamic Web Project功能。
2、从Struts2官网上下载Struts2 struts-2.3.30-all.zip
3、向工程中导入Struts2所必须的jar包:
这里写图片描述
4、在web.xml文件中加入Struts2的配置信息

 <!-- 配置 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>

5、在src目录下创建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.zjp.domain.Product" method="save">
            <result name="details">/WEB-INF/pages/details.jsp</result>
        </action>
    </package>

</struts>

在struts.xml中配置一个action,与之对应的class类为Product

package com.zjp.domain;

public class Product {

    private int productId;
    private String productName;
    private String productDesc;
    private String productPrice;
    public int getProductId() {
        return productId;
    }
    public void setProductId(int 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 String getProductPrice() {
        return productPrice;
    }
    public void setProductPrice(String productPrice) {
        this.productPrice = productPrice;
    }
    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productName="
                + productName + ", productDesc=" + productDesc
                + ", productPrice=" + productPrice + "]";
    }

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

配置文件中method对应的方法与product类中的save必须一一对应,在result中当save方法返回detail字符串,将跳转到/WEB-INF/pages/details.jsp

Product参数需要从一个表中输入,在index.jsp页面中:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <a href="product-input.action">Product Input</a>
</body>
</html>

当用户点击Product Input时,在struts.xml中配置其跳转到/WEB-INF/pages/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>

当用户提交该表格,product-save.action设置跳转到detail页面。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>    

<!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>

测试结果:
这里写图片描述
这里写图片描述
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值