我有一个JSP文件将一个字符串输入到Java类,并且应该从它返回一个ArrayList。我是否需要公共类(即 void main(String [] args))才能返回调用JSP,或 public Scopus()足以返回值?
Scopus.java(在scopusID中接受,返回scopusList)
package newpackage1;
import org.w3c.dom.Document;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.util.ArrayList;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class Scopus {
String scopusID;
URL url;
ArrayList<String> scopusList = new ArrayList<String>();
NodeList nodes;
DocumentBuilder builder;
Document doc;
public void setScopusList(ArrayList scopusList) {
this.scopusList = scopusList;
}
public ArrayList getScopusList() {
return scopusList;
}
public void setScopusID(String url) {
this.scopusID = url;
}
public String getScopusID() {
return scopusID;
}
public Scopus(String scopusID) {
String fTitle, fLink;
try {
URL url = new URL( "http://syndic8.scopus.com/getMessage?registrationId=" + scopusID );
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = builder.parse(url.openStream());
nodes = doc.getElementsByTagName("item");
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
}
//Only output if at least one is found
int counter = 0;
if ( nodes.getLength() > 0 ) {
//Place all results into an array list first
for(int i=0;i<nodes.getLength();i++) {
Element element = (Element)nodes.item(i);
fTitle = getElementValue(element, "title");
fLink = getElementValue(element, "link");
scopusList.add("<a href=\"" + fLink + "\" target=\"_blank\">" + fTitle + "</a>");
counter++;
}
}
}
public static void main(String[] args) {
}
private String getElementValue(Element parent,String label) {
return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0));
}
private String getCharacterDataFromElement(Element e) {
try {
Node child = e.getFirstChild();
if(child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
} //if
} //try
catch(Exception ex) {
}
return " ";
} //private String getCharacterDataFromElement
}
output.jsp(调用上面的类)
<%@page import="java.util.ArrayList,org.w3c.dom.Node,org.w3c.dom.NodeList" %>
<%
String feedID = "HEDCIHLCIGDKPFHHJEEEHJDEIEGJIKJHKWFQWLHFJH";
%>
<jsp:useBean id="scopus" scope="page" class="newpackage1.Scopus">
<jsp:setProperty name="scopus" property="scopusID" value="<%= feedID %>" />
</jsp:useBean>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:getProperty name="scopus" property="scopusList" />
<%
ArrayList sl = scopus.getScopusList();
//Will do output later
%>
</body>
</html>
答案 0 :(得分:1)
您在类中指定的类不能是抽象的,并且必须具有您没有的公共,无参数构造函数。
更新::您不需要main()方法,公共Scopus()非参数构造函数就可以了。 JSP容器将使用零参数构造函数使用反射创建此类的实例。如果您不创建任何构造函数,Java编译器将向编译的类添加一个。您只需要使用参数重命名原始构造函数,使其成为执行所有这些业务逻辑操作的方法,并在设置scopusID属性之后和调用getScopusList()
方法之前调用它(当您尝试访问scopusList属性)。
至于列表数据的表示,使用时不是一个好主意:
<jsp:getProperty name="scopus" property="scopusList" />
<%
ArrayList sl = scopus.getScopusList();
//Will do output later
%>
您最好使用JSP EL和JSTL核心taglib,并执行以下操作:
<c:forEach items="${scopus.scopusList}" var="item">
${item}<br/>
</c:forEach>