-1

我有一个 JSP 文件向 Java 类输入一个字符串,并且应该从它返回一个 ArrayList。我是否需要一个公共类(即void main(String[] args))才能返回调用 JSP,还是public Sc​​opus()足以返回值?

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

1 回答 1

1

您在 class 中指定的类不能是抽象的,并且必须具有您没有的公共、无参数构造函数。

更新::不需要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>
于 2012-05-31T15:45:32.067 回答