首页 > 编程 > JSP > 正文

解决JSP中拼装数据为XML出现的 问题

2019-11-02 15:46:25
字体:
来源:转载
供稿:网友

 一、应用背景

JSP取得Servlet中放入request的List,将List中的数据拼装成XML。以下代码在Eclipse的内置浏览器中显示为xml,没有问题。

[java]
/**
* 新闻Servlet
* @author 徐越
*
*/
public class ListServlet extends HttpServlet
{
private static final long serialVe

7788电影网[www.aikan.tv/special/7788dianyingwang/]
rsionUID = 1L;
private VideoNewsService vs = new VideoNewsServiceImpl();

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<VideoNews> news = vs.readNews();
request.setAttribute("lstnews", news);
request.getRequestDispatcher("/WEB-INF/pages/news.jsp").forward(request, response);
}
}

/**
* 新闻Servlet
* @author 徐越
*
*/
public class ListServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
private VideoNewsService vs = new VideoNewsServiceImpl();

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<VideoNews> news = vs.readNews();
request.setAttribute("lstnews", news);
request.getRequestDispatcher("/WEB-INF/pages/news.jsp").forward(request, response);
}
}
[html]
<%@ page language="java" contentType="text/xml; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<?xml version="1.0" encoding="UTF-8"?>
<videoNews>
<c:forEach items="${lstnews}" var="n">
<news id="${n.id }">
<title>${n.title }</title>
<length>${n.timeLength }</length>
</news>
</c:forEach>
</videoNews>

<%@ page language="java" contentType="text/xml; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<?xml version="1.0" encoding="UTF-8"?>
<videoNews>
<c:forEach items="${lstnews}" var="n">
<news id="${n.id }">
<title>${n.title }</title>
<length>${n.timeLength }</length>
</news>
</c:forEach>
</videoNews>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表