Struts2.Method
public String stat(){ // 固定的二句 ActionContext ctx = ActionContext.getContext(); HttpServletRequest request = (HttpServletRequest) ctx .get(ServletActionContext.HTTP_REQUEST); List<News> weekList=new ArrayList<News>();//周 List<News> monthList=new ArrayList<News>();//月 List<News> quarterList=new ArrayList<News>();//季度 request.setAttribute("week", weekList.size()); request.setAttribute("month", monthList.size()); request.setAttribute("quarter", quarterList.size()); return "stat"; }
Jsp.jsp
<%@ page contentType="text/html" pageEncoding="UTF-8"%><% StringBuffer content = new StringBuffer(""); content.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); content.append("<roots>"); content.append("<Week>"); content.append(request.getAttribute("week")); content.append("</Week>"); content.append("<Month>"); content.append(request.getAttribute("month")); content.append("</Month>"); content.append("<Quarter>"); content.append(request.getAttribute("quarter")); content.append("</Quarter>"); content.append("</roots>"); response.setCharacterEncoding("UTF-8"); out.print(content);%>
最終顯示.xml
<?xml version="1.0" encoding="UTF-8" ?> <roots> <Week>47</Week> <Month>22</Month> <Quarter>22</Quarter> </roots>