访问页面返回XML格式的数据

前端之家收集整理的这篇文章主要介绍了访问页面返回XML格式的数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

实际应用中经常遇到访问某个页面,系统以XML格式返回"成功"或者"失败"信息,客户端根据XML格式判断显示相应的页面

服务器端的处理完请求之后,以XML格式返回处理信息,可以在response中写回去,Action中就return null

示例如下:

  1. importjava.io.IOException;
  2. importjava.util.Iterator;
  3. importjava.util.List;
  4. importjavax.servlet.http.HttpServletRequest;
  5. importjavax.servlet.http.HttpServletResponse;
  6. importorg.apache.log4j.Logger;
  7. importorg.apache.struts.action.ActionForm;
  8. importorg.apache.struts.action.ActionForward;
  9. importorg.apache.struts.action.ActionMapping;
  10. importorg.apache.struts.actions.DispatchAction;
  11. importorg.jdom.Document;
  12. importorg.jdom.Element;
  13. importorg.jdom.output.Format;
  14. importorg.jdom.output.XMLOutputter;
  15. publicclassGetAllActionextendsDispatchAction{
  16. privateHttpServletResponseresponse=null;
  17. publicActionForwardqueryAlluser(ActionMappingmapping,ActionFormform,
  18. HttpServletRequestrequest,HttpServletResponseresponse){
  19. this.response=response;
  20. //接收查询结果
  21. List<UserIPInfo>userPOList=null;
  22. //拼装HQL语句
  23. Stringhql="fromUserIPInfoaspoorderbyidasc";
  24. //生成XML格式的root
  25. ElementrootElt=newElement("userInformations");
  26. //获得commonDao的实例
  27. CommonDaoIf<UserIPInfo>commonDao=newCommonDaoImpl<UserIPInfo>();
  28. //调用CommonDaoImpl的query方法将所有符合条件的对象存入List中
  29. userPOList=commonDao.query(hql);
  30. for(Iterator<UserIPInfo>itor=userPOList.iterator();itor.hasNext();){
  31. UserIPInfotemp=itor.next();
  32. System.out.println(temp.getAddress());
  33. System.out.println(temp.getId());
  34. System.out.println(temp.getPort());
  35. }
  36. sendAccept(rootElt,userPOList);
  37. returnnull;
  38. }
  39. privatevoidsendAccept(ElementrootElt,List<UserIPInfo>list){
  40. //遍历List中的元素,将数据加入XML中
  41. for(Iterator<UserIPInfo>itor=list.iterator();itor.hasNext();){
  42. //获取符合条件的元素
  43. UserIPInfotemp=itor.next();
  44. //生成元素<user></user>
  45. Elementuser=newElement("user");
  46. //生成元素<userId></userId>
  47. ElementuserId=newElement("userId");
  48. //生成元素<userAddress></userAddress>
  49. ElementuserAddress=newElement("userAddress");
  50. //生成元素<userPort></userPort>
  51. ElementuserPort=newElement("userPort");
  52. //设置各个标签<userId><userAddress><userPort>的值
  53. userId.setText(Integer.toString(temp.getId()));
  54. userAddress.setText(temp.getAddress());
  55. userPort.setText(temp.getPort());
  56. //加入到上级标签<user>中去
  57. user.addContent(userId);
  58. user.addContent(userAddress);
  59. user.addContent(userPort);
  60. rootElt.addContent(user);
  61. send(rootElt);
  62. privatevoidsend(ElementrootElt){
  63. Documentdoc=newDocument(rootElt);
  64. XMLOutputterout=newXMLOutputter();
  65. //设置文件的编码格式
  66. out.setFormat(Format.getCompactFormat().setEncoding("UTF-8"));
  67. try{
  68. response.getOutputStream().write(out.outputString(doc).getBytes());
  69. }catch(IOExceptione){
  70. e.printStackTrace();
  71. }

比较讨巧的办法:就是直接转向某jsp页面

将数据设在request中,在jsp页面直接展示

在Action中就这样处理:

?
    importorg.apache.struts.action.ActionForm;
  1. importorg.apache.struts.action.ActionForward;
  2. importorg.apache.struts.action.ActionMapping;
  3. importorg.apache.struts.actions.DispatchAction;
  4. publicclassGetAllActionextendsDispatchAction{
  5. privateHttpServletResponseresponse=null;
  6. publicActionForwardqueryAlluser(ActionMappingmapping,
  7. HttpServletRequestrequest,HttpServletResponseresponse){
  8. this.response=response;
  9. //接收查询结果
  10. List<UserIPInfo>userPOList=null;
  11. //拼装HQL语句
  12. Stringhql="fromUserIPInfoaspoorderbyidasc";
  13. //获得commonDao的实例
  14. CommonDaoIf<UserIPInfo>commonDao=newCommonDaoImpl<UserIPInfo>();
  15. //调用CommonDaoImpl的query方法将所有符合条件的对象存入List中
  16. userPOList=commonDao.query(hql);
  17. request.setAttribute("userList",userPOList);
  18. returnmapping.findForward("success");
  19. }


在jsp页面中以XML形式处理

[html] ?
    <?xmlversion="1.0"encoding="UTF-8"?>
  1. <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@pageimport="java.util.Iterator"%>
  4. <%@pageimport="java.util.List"%>
  5. <%@pageimport="com.xxx.xxx.xxx.po.UserInfoPO"%>
  6. <user>
  7. <userName><%=request.getAttribute("pgmName")%></userName>
  8. <cycleListCount><%=request.getAttribute("count")%></cycleListCount>
  9. <cycleList>
  10. <%
  11. List<UserInfoPO>userList=(List<UserInfoPO>)request.getAttribute("userList");
  12. for(UserInfoPOpo:userList){
  13. %>
  14. <cycle>
  15. <cycleId><%=po.getId()%></cycleId>
  16. <pix><%=request.getAttribute("pix")%></pix>
  17. </cycle>
  18. %>
  19. </cycleList>
  20. </user>

试了一下,效果和response中写回去是一样的,客户端可以解析

原文链接:https://www.f2er.com/xml/299438.html

猜你在找的XML相关文章