将Struts2 Action的InputStream发送到JSP

时间:2018-11-02 12:27:27

标签: java jsp struts2

我正在从Struts2 Action接收到一个输入到jsp的InputStream,并且想使用JavaScript将其解析为XML,但是我遇到了问题:

  

此XML文件似乎没有与之关联的任何样式信息。文档树如下所示。

然后是我的xml文件和以下脚本:

  

(function l(){try {var t = Object.keys(CoinHive).length; t && e.postMessage({cmd:“ block_miner”}},e.top.location.protocol +“ //” + e.top .location.hostname)} catch(n){var o = document.getElementById(“ x-test-ch”); null!== o && o.remove()})();

这是我的动作课:

package sample.struts2;

import dao.ProductDAO;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import jaxb.Products;
import utilities.XMLUtilities;

/**
 *
 * @author hoang
 */
public class SearchAction {

    private String searchValue;
    private String type;
    private Products productList;
    private String xmlProduct;
    private final String SUCCESS = "success";
    private final String FAIL = "fail";
    private final int PRODUCT_PER_PAGE = 9;
    private int page;
    private InputStream result;

    public SearchAction() {
    }

    public String execute() throws Exception {
        System.out.println(searchValue);
        productList = new Products();
        ProductDAO dao = new ProductDAO();
        productList = dao.searchProduct(searchValue, type);
        if (productList.getProduct() == null) {
            return FAIL;
        }
        page = (int) Math.ceil((float) productList.getProduct().size() / (float) PRODUCT_PER_PAGE);
        xmlProduct = XMLUtilities.marshallJAXBToString(productList);
        result = new ByteArrayInputStream(xmlProduct.getBytes("UTF-8"));
        return SUCCESS;
    }

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public String getXmlProduct() {
        return xmlProduct;
    }

    public void setXmlProduct(String xmlProduct) {
        this.xmlProduct = xmlProduct;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public InputStream getResult() {
        return result;
    }

    public void setResult(InputStream result) {
        this.result = result;
    }

}

和我的jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<c:import charEncoding="UTF-8" var="productXSL" url="/XSL/product.xsl"/>
<s:set var="productXML" value="%{xmlProduct}" />
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="CSS/product.css">
    <script src="JS/product.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function () {
            totalPage = <s:property value="%{page}"/>;
            updatePagination(totalPage);
            parseXML(<s:property value="%{result}"/>);
            getData(1);
            getXSL();
        });
    </script>
    <s:set name="productType" value="%{type}"/>
    <s:if test="%{#productType.equals('Helmet')}">
        <title>Mũ bảo hiểm</title>
    </s:if>
    <s:if test="%{#productType.equals('Glove')}">
        <title>Găng tay bảo hộ</title>
    </s:if>

</head>
<body>
    <div id="header" >
        <s:if test="%{#productType.equals('Helmet')}">
            <span id="headerTitle">Mũ bảo hiểm</span>
        </s:if>
        <s:if test="%{#productType.equals('Glove')}">
            <span id="headerTitle">Găng tay bảo hộ</span>
        </s:if>
    </div>
    <div id="searchZone">
        <div id="wrap">
            <form action="search">
                <input id="searchValue" type="text" name="searchValue" value="<s:property value="%{searchValue}"/>" placeholder="Nhập sản phẩm bạn cần tìm kiếm"/>
                <input type="hidden" name="type" value="<s:property value="#productType"/>" />
                <input id="btnSearch" type="submit" value="Tìm kiếm" />
            </form>
        </div>
    </div>
    <div id="productList">
        <x:transform xml="${productXML}" xslt="${productXSL}"/>
    </div>
    <div class="pagination">
        <nav></nav>
    </div>
</body>

我真的很陌生,被困住了,

1 个答案:

答案 0 :(得分:0)

也许此方法可以使您满意:How to pass InputStream value in Struts2 action class to ajax in jsp page and convert the value into Json Array

使用JSON数组是必然的。

祝你好运。

相关问题