gpt4 book ai didi

java - ZK框架: Load xls into ZK Spreadseet from Servlet

转载 作者:行者123 更新时间:2023-12-01 19:24:01 24 4
gpt4 key购买 nike

寻找如何使用 servlet 加载 xls 并将该 xls 设置到电子表格中。我无法将电子表格连接到 servlet,因为它始终为空。另外,我无法使用

获取 bean EventComposer

EventComposer loadFileServlet = (EventComposer)SpringUtil.getBean("eventComposer");

不知何故,我需要在 servlet 中获取电子表格并将 xls 传递到其中。

或者简单地说:如何从servlet中的Composer(jsp)获取Speradseet

我有jsp文件:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>zk</title>
<zssjsp:head/>
</head>
<body>
<div width="100%" style="height: 100%;">
<zssjsp:spreadsheet id="ss" src="/WEB-INF/books/hide.xlsx" apply="org.autto.zk.EventComposer"
width="100%" height="100%" maxVisibleRows="200" maxVisibleColumns="40"
showToolbar="true" showFormulabar="true" showContextMenu="true" showSheetbar="true"/>
</div>
</body>
</html>

带有电子表格的EventComposer

public class EventComposer extends SelectorComposer<Component> {
**********
@Wire
private Spreadsheet ss;

//for testing, check xls loaded into spreadsheet after stop editing
@Listen("onStopEditing = #ss")
public void showBook() throws IOException {
Path path = Paths.get("D:\\Documents\\zssjspdemo-master\\src\\main\\webapp\\upload\\blank.xls");
File file = path.toFile();

Importer importer = Importers.getImporter();
Book book = importer.imports(getStreamData(file), "hide");
ss.setBook(book);
}
********

}

还有servlet

@WebServlet("/load")
public class LoadFileServlet extends HttpServlet {
@Wire
private Spreadsheet ss; // this spreadsheet null, can't wire it

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setSizeThreshold(1024 * 1024);

File tempDir = (File)getServletContext().getAttribute("javax.servlet.context.tempdir");
factory.setRepository(tempDir);

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setSizeMax(1024 * 1024 * 10);

try {
List items = upload.parseRequest(req);
Iterator iter = items.iterator();

while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();

if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);



**//whant to do something like this but ss is null
Importer importer = Importers.getImporter();
Book book = importer.imports(getStreamData(item), "hide");
ss.setBook(book);**

}
}
} catch (Exception e) {
e.printStackTrace();
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
}

/**
* save file to upload folder.
* folder should be created already
*
* @param item
* @throws Exception
*/
private void processUploadedFile(FileItem item) throws Exception {
File uploadedFile = null;
do {
String path = getServletContext().getRealPath("/upload/" + item.getName());
uploadedFile = new File(path);
} while (uploadedFile.exists());

uploadedFile.createNewFile();
item.write(uploadedFile);
}
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.zkoss.zss</groupId>
<artifactId>home</artifactId>
<version>3.9.5-Eval</version>
<properties>
<commons-io>1.3.1</commons-io>
<maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
<packname>-${project.version}-FL-${maven.build.timestamp}</packname>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<packaging>war</packaging>
<name>The zssjsp demo Project</name>
<description>The zssjspdemo Project</description>
<licenses>
<license>
<name>GNU LESSER GENERAL PUBLIC LICENSE, Version 3</name>
<url>http://www.gnu.org/licenses/lgpl.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<repositories>
<repository>
<id>ZK CE</id>
<name>ZK CE Repository</name>
<url>http://mavensync.zkoss.org/maven2</url>
</repository>
<repository>
<id>ZK EVAL</id>
<name>ZK Evaluation Repository</name>
<url>http://mavensync.zkoss.org/eval</url>
</repository>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>zkmaven</id>
<name>ZK Maven Plugin Repository</name>
<url>http://mavensync.zkoss.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zss</groupId>
<artifactId>zssex</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.zkoss.zss</groupId>
<artifactId>zssjsp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<!-- https://search.maven.org/remotecontent?filepath=org/apache/httpcomponents/httpclient/4.5.9/httpclient-4.5.9.jar -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0.0.redhat-1</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.7.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>

</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Run with Jetty -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<contextPath>/</contextPath>
</configuration>
</plugin>
</plugins>
</build>
</project>

UPD 1:感谢 Hawk 的想法:现在如何运作:1.添加了processShowBook(item, req, resp);请求:

    private void processShowBook(FileItem item, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.setAttribute("string", getPathString(item));
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
  • 已添加到 Composer

    public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);
    String s = (String)((HttpServletRequest)Executions.getCurrent().getNativeRequest()).getAttribute("string");
    if (s != null) {
    showBook(s);
    }

    }

    public void showBook(String s) throws IOException {
    Path path = Paths.get(s);
    File file = path.toFile();

    Importer importer = Importers.getImporter();
    Book book = importer.imports(getStreamData(file), "hide");
    ss.setBook(book);

    }

  • 我通过请求尝试了部分传递的两种变体在这两种方式中,我都有书或字符串路径。电子表格也可用,但未更新。为什么?

    如果我添加 @Listen("onStopEditing = #ss") 从另一边到showBook方法,并传递Path字符串,编辑exl并输入回车,电子表格被更新。

    UPD 2:问题是,当加载 ss 时,它的 id 为 8418(示例)。当我发送请求并调用 doAfterCompose 时,id 是 8452。当我编辑单元格并再次触发 onCellSelection id 8418 时。并且有不同的 session 。

    正如我所理解的,当 servlet 将请求重定向到 index.jsp/zul 时,我在调用 doAfterCompose 时创建了新对象 ss,但仍然有旧的 Activity ss。我怎样才能避免这种情况?

    what I have when send request inside do aftercompose()

    when I select cell on the same window without reloading

    最佳答案

    失败的原因

    ZK Composer 存在于由 ZK 创建的特定“桌面”范围内,该范围比 session 短,但比请求长。每次您加载(或重新加载)带有 ZK 组件的页面时,服务器都会创建一个新组件和一个新组合器,并将页面上的组件连接到组合器中,然后该组件会向您的浏览器呈现一个 javascript 小部件。渲染的 javascript 小部件和 java 组件形成一对。当您与小部件交互时,它会向服务器发出 AJAX 请求,而不是重新加载整个页面。​ZK有自己的流程来控制composer的生命周期,你自己创建的composer(如new EventComposer())与ZK框架创建的composer不一样。

    我推荐 3 种方法:

    包括 ZUL 方法

    由于zul提供了更丰富的功能,因此您可以在zul中使用FileUpload组件来实现上传功能。因此,同一页面中的 2 个 zk 组件(FileUpload 和 Spreadsheet)之间的通信更容易。然后你只需要在jsp中包含zul,就像jsp菜单示例一样。如何实现上传,请引用开发引用中的文件上传

    通过请求传递

    您可以通过您当前的servlet方式实现上传。1. 将 Book 对象(由 Importer 生成)存储为 HttpServletRequest 属性。2.用电子表格将请求转发(保持请求不变)到JSP3. 在 Composer.doAfterCompose() 中获取这本书((HttpServletRequest)Excuctions.getCurrent().getNativeRequest()).getAttribute()

    另一种方法是:1.直接将xls文件存放在特定文件夹中2. 将文件名作为查询字符串传递3.使用电子表格将请求转发到JSP4.通过HttpServletRequest.getParameter()获取查询字符串(文件名)5.导入文件

    检查this example这很简单,但展示了这个想法。

    @Wire 在 Servlet 中不起作用

    @Wire是ZK注解,仅在ZKSelectorComposer中起作用

    关于java - ZK框架: Load xls into ZK Spreadseet from Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329554/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com