gpt4 book ai didi

jsp中的Java类

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

所以我不知道如何正确导入java类。我正在使用 eclipse tomcat 7 和 liferay。在liferay中是一个包含img文件的目录,我将使用java打开该目录。这工作正常(本地),我得到一个带有路径的数组。所以我将在jsp中的liferay中创建一个portlet并在jsp中获取数组。有任何想法吗? 我的jsp:

<%@ page import ="OpenDir" %>
<%@ page import="java.io.File"%>

<%=
OpenDir.Dir()
%>

我的java:

package life_ray;

import java.io.File;

public class OpenDir {

public static void Dir() {

int i = 0;
File directory = new File(
"C:\\Users\\Public\\Pictures\\Sample Pictures\\");
File[] contents = directory.listFiles();
for (File f : contents) {
f.getAbsolutePath();
i++;
}
String[] name = new String[i];

i = 0;
for (File f : contents) {
name[i] = f.getAbsolutePath();
i++;
}
i = 0;
for (File f : contents) {
name[i] = f.getAbsolutePath();
System.out.println("<img src=\"" + name[i] + "\" alt=\"\">");
i++;
}

}
}







so here is my error:
Caused by: org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 14 in the generated java file
Only a type can be imported. life_ray.OpenDir resolves to a package

An error occurred at line: 7 in the jsp file: /view.jsp
OpenDir cannot be resolved
4: <body>
5:
6: <%=
7: OpenDir.Dir()
8: %>
9:
10:

最佳答案

为了遵循已经给出的答案,开始重构代码可能是有意义的:

Java:

package life_ray;

import java.io.File;
import javax.servlet.jsp.JspWriter;

public class OpenDir {

public static File[] listSamplePictures() {
File f = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\");
return f.listFiles();
}

public static void printSamplePictures(JspWriter w) {
File[] files = listSamplePictures();
for (int i = 0; i < files.length; i++) {
w.println("<img src=\"" + files[i].getAbsolutePath() + "\" alt=\"\">");
}
}

}

JSP:

<%
java.io.File[] files = life_ray.OpenDir.listSamplePictures();
for (int i = 0; i < files.length; i++) {
out.println("<img src=\"" + files[i].getAbsolutePath() + "\" alt=\"\">");
}
%>

<%
life_ray.OpenDir.printSamplePictures(out);
%>

关于jsp中的Java类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785985/

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