实际应用中经常遇到访问某个页面,系统以XML格式返回"成功"或者"失败"信息,客户端根据XML格式判断显示相应的页面。
服务器端的处理完请求之后,以XML格式返回处理信息,可以在response中写回去,Action中就return null
示例如下:
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.log4j.Logger;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
- public class GetAllAction extends DispatchAction {
- private HttpServletResponse response = null;
- public ActionForward queryAlluser(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response){
- this.response = response;
- // 接收查询结果
- List<UserIPInfo> userPOList = null;
- // 拼装HQL语句
- String hql = "from UserIPInfo as po order by id asc";
- // 生成XML格式的root
- Element rootElt=new Element("userInformations");
- // 获得commonDao的实例
- CommonDaoIf<UserIPInfo> commonDao = new CommonDaoImpl<UserIPInfo>();
- // 调用CommonDaoImpl的query方法将所有符合条件的对象存入List中
- userPOList = commonDao.query(hql);
- for(Iterator<UserIPInfo> itor = userPOList.iterator();itor.hasNext();){
- UserIPInfo temp = itor.next();
- System.out.println(temp.getAddress());
- System.out.println(temp.getId());
- System.out.println(temp.getPort());
- }
- sendAccept(rootElt, userPOList);
- return null;
- }
- private void sendAccept(Element rootElt,List<UserIPInfo> list){
- // 遍历List中的元素,将数据加入XML中
- for(Iterator<UserIPInfo> itor = list.iterator();itor.hasNext();){
- // 获取符合条件的元素
- UserIPInfo temp = itor.next();
- // 生成元素<user></user>
- Element user = new Element("user");
- // 生成元素<userId></userId>
- Element userId = new Element("userId");
- // 生成元素<userAddress></userAddress>
- Element userAddress = new Element("userAddress");
- // 生成元素<userPort></userPort>
- Element userPort = new Element("userPort");
- // 设置各个标签<userId><userAddress><userPort>的值
- userId.setText(Integer.toString(temp.getId()));
- userAddress.setText(temp.getAddress());
- userPort.setText(temp.getPort());
- // 加入到上级标签<user>中去
- user.addContent(userId);
- user.addContent(userAddress);
- user.addContent(userPort);
- rootElt.addContent(user);
- }
- send(rootElt);
- }
- private void send(Element rootElt){
- Document doc = new Document(rootElt);
- XMLOutputter out = new XMLOutputter();
- //设置文件的编码格式
- out.setFormat(Format.getCompactFormat().setEncoding("UTF-8"));
- try {
- response.getOutputStream().write(out.outputString(doc).getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
比较讨巧的办法:就是直接转向某jsp页面
将数据设在request中,在jsp页面直接展示
在Action中就这样处理:
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- public class GetAllAction extends DispatchAction {
- private HttpServletResponse response = null;
- public ActionForward queryAlluser(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response){
- this.response = response;
- // 接收查询结果
- List<UserIPInfo> userPOList = null;
- // 拼装HQL语句
- String hql = "from UserIPInfo as po order by id asc";
- // 获得commonDao的实例
- CommonDaoIf<UserIPInfo> commonDao = new CommonDaoImpl<UserIPInfo>();
- // 调用CommonDaoImpl的query方法将所有符合条件的对象存入List中
- userPOList = commonDao.query(hql);
- request.setAttribute("userList", userPOList);
- return mapping.findForward("success");
- }
- }
在jsp页面中以XML形式处理
- <?xml version="1.0" encoding="UTF-8"?>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import="java.util.Iterator"%>
- <%@ page import="java.util.List"%>
- <%@ page import="com.xxx.xxx.xxx.po.UserInfoPO"%>
- <user>
- <userName><%=request.getAttribute("pgmName")%></userName>
- <cycleListCount><%=request.getAttribute("count")%></cycleListCount>
- <cycleList>
- <%
- List<UserInfoPO> userList = (List<UserInfoPO>)request.getAttribute("userList");
- for(UserInfoPO po : userList){
- %>
- <cycle>
- <cycleId ><%=po.getId()%></cycleId>
- <pix><%=request.getAttribute("pix")%></pix>
- </cycle>
- <%
- }
- %>
- </cycleList>
- </user>
试了一下,效果和response中写回去是一样的,客户端可以解析