1.根据对应的XML写出pojo,能够在jsp页面中应用EL读取对应的属性值。
2.用DOM解析XML资源文件(java API的org.w3c.dom)
list.xml:
-------------------------------------------------------------------------------------
在spring的Controller中取出name标签的节点值。
DownLoadController.java
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("D:/jproject/Demo_one/war/WEB-INF/list.xml"));
NodeList nl=doc.getElementsByTagName("show");
int len = nl.getLength();
for(int i=0;i<len;i++){
Element elementlist =(Element) nl.item(i);
Node nameElement = elementlist.getElementsByTagName("name").item(0);
String name = nameElement.getFirstChild().getNodeValue();
Node urlElement = elementlist.getElementsByTagName("url").item(0);
String url = urlElement.getFirstChild().getNodeValue();
DownLoad dl = new DownLoad();
dl.setUrl(url);
dl.setName(name);
//加入所需要的属性放入List
downloadOptionList.add(dl);
}
Map model = new HashMap();
//声明一个KV,其中的downloadOption是在jsp中的items="${model.downloadOption}"
//可以得到相应的属性集合
model.put("downloadOption", downloadOptionList);
//ModelAndView的概念。后两个参数是名字值对。
return new ModelAndView("download","model",model);
}
}
--------------------------------------------------------------------------------------
3.download.jsp
<select name="url" multiple="multiple" size="3">
<c:forEach var="item" items="${model.downloadOption}">
<option value="${item.url}">${item.name}</option>
</c:forEach>
</select>
</form>
<br>
</tr>
<tr>
<input type="button" value="DownLoad" onclick="download()"/>
</tr>
</center>
</body>
</html>