熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

解決JSP中拼裝數據為XML出現的問題

2022-06-13   來源: JSP教程 

  一應用背景

  JSP取得Servlet中放入request的List將List中的數據拼裝成XML以下代碼在Eclipse的內置浏覽器中顯示為xml沒有問題

  [java]
/**
* 新聞Servlet
* @author 徐越
*
*/
public class ListServlet extends HttpServlet
{
private static final long serialVersionUID = L;
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 = vsreadNews();
requestsetAttribute("lstnews" news);
requestgetRequestDispatcher("/WEBINF/pages/newsjsp")forward(request response);
}
}

  /**
* 新聞Servlet
* @author 徐越
*
*/
public class ListServlet extends HttpServlet
{
private static final long serialVersionUID = L;
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 = vsreadNews();
requestsetAttribute("lstnews" news);
requestgetRequestDispatcher("/WEBINF/pages/newsjsp")forward(request response);
}
}
[html]
<%@ page language="java" contentType="text/xml; charset=utf" pageEncoding="utf"%>
<%@ taglib prefix="c" uri="%>
<?xml version="" encoding="UTF"?>
<videoNews>
<c:forEach items="${lstnews}" var="n">
<news id="${nid }">
<title>${ntitle }</title>
<length>${ntimeLength }</length>
</news>
</c:forEach>
</videoNews>

  <%@ page language="java" contentType="text/xml; charset=utf" pageEncoding="utf"%>
<%@ taglib prefix="c" uri="%>
<?xml version="" encoding="UTF"?>
<videoNews>
<c:forEach items="${lstnews}" var="n">
<news id="${nid }">
<title>${ntitle }</title>
<length>${ntimeLength }</length>
</news>
</c:forEach>
</videoNews>

  二發現問題

  火狐中報錯XML解析錯誤XML 或文本聲明不在實體的開頭

  chrome報錯XML declaration allowed only at the start of the document

  根據錯誤信息可以知道XML聲明<?xml version="" encoding="UTF"?>必須在文檔的開頭

  
解決問題

  將pagetaglibxml同時放在第一行即可一個接一個的後面雖然不好看但是解決問題哦


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19951.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.