jsp

位置:IT落伍者 >> jsp >> 浏览文章

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


发布日期:2019年09月24日
 
解决JSP中拼装数据为XML出现的问题

应用背景

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同时放在第一行即可一个接一个的后面虽然不好看但是解决问题哦

               

上一篇:JSP和JSF合并打造完美Web应用

下一篇:JSP总结一:HTTP协议