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

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:jsp如何解決Form表單亂碼問題
      網站程序中的Form表單,是重要的組成內容之一,很多時候,Form表單會出現亂碼的現象,會給網站造成一定的困擾。如何解決亂碼的問題? JSP和Servlet的六種中文亂碼處理方法 一、表單提交時出現亂碼: 在進行表單提交的時候,經常提交一些中文,自然就避免不了出現中文亂

       一、應用背景

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

      [java]
      /**
      * 新聞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);
      }
      }

      /**
      * 新聞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>

       

      二、發現問題

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

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

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


      三、解決問題

      將page、taglib、xml同時放在第一行即可,一個接一個的后面。雖然不好看,但是解決問題哦

      分享:jsp上傳圖片即時顯示效果代碼
      scriptfunction setImagePreview() { var docObj=document.getElementById(doc); var imgObjPreview=document.getElementById(preview);if(docObj.files docObj.files[0]){ //火狐下,直接設img屬性 imgObjPreview.style.display = 'block'; imgObjPreview.style.width

      來源:模板無憂//所屬分類:JSP教程/更新時間:2013-04-17
      相關JSP教程