嗨,大家好,我试图将当前引用存储到数组列表“pl”。 例如pl.add(本); 由于某种原因,我只得到最后一个项目的参考,没有以前的项目。循环确实经历了所有三个项目。在ArrayList中存储对象的引用
下面是我得到的代码和输出。任何人都可以告诉我我做错了什么,谢谢你提前帮忙。
// variables
private String productType;
private String hyperLinkParam;
private ArrayList <ProductList> pl = new ArrayList<ProductList>();
public ProductList() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream url = null;
url = getClass().getResourceAsStream("inventory.xml");
Document doc = db.parse(url);
doc.getDocumentElement().normalize();
// loop through each item
NodeList items = doc.getElementsByTagName("item"); //Returns a list of elements with the given tag name item
for (int i = 0; i < items.getLength(); i++)
{
Element e = (Element) items.item(i);
setHyperLinkParam(e.getAttribute("name").toString());
setProductType(getTextValue(e,"productType"));
System.out.print(e.getAttribute("name").toString());
System.out.println(getTextValue(e,"productType"));
pl.add(this);
}
for(int j=0; j < pl.size(); j++){
System.out.print("getHyperLinkParam: " + pl.get(j).getHyperLinkParam());
System.out.println("getProductType: " + pl.get(j).getProductType());
}
Manufacture.java
@WebMethod(operationName = "getProductList")
public ProductList getProductList() {
try {
ProductList productlist = new ProductList();
if(productlist == null){
return null;
}else{
return productlist;
}
} catch(Exception e){
System.out.println("error: " + e.getMessage());
return null;
}
}
的index.jsp
<%
try {
org.soen487.supplychain.manufacturer.Manufacture_Service service = new org.soen487.supplychain.manufacturer.Manufacture_Service();
org.soen487.supplychain.manufacturer.Manufacture port = service.getManufacturePort();
// TODO process result here
org.soen487.supplychain.manufacturer.ProductList result = port.getProductList();
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
我会试试这个谢谢你! – KSM
上面的代码确实显示了Productlist类中的不同项目,但是无法访问JSP内的代码我假设我不能返回标题为Manufacture.java和index的代码中上述两个新子部分的正确对象。 JSP。你能帮我修正 – KSM
不应该我能够作为productlist.product.getMethod()访问它? – KSM