JSTL:如何解释 <C:OUT> 上的 HTML 转义序列?
JSTL: How do I account for HTML escape sequences on <C:OUT>?
我有一个 Java 动态 Web 项目,其中一个 Servlet 执行以下操作:
/**
* Servlet implementation class DataAPIServlet
*/
@WebServlet(name = "data", urlPatterns = { "/data" })
public class DataAPIServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String format;
/**
* @see HttpServlet#HttpServlet()
*/
public DataAPIServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
format = request.getParameter("format").replaceAll("\"", "");
// create data model and add to request object
RequestDispatcher requestDispatcher = null;
if (format.equals(null)) {
requestDispatcher = jsonDispatcher(request);
response.setContentType("text/json");
} else {
System.out.println("SERVING FORMATTED DATA : " + format);
String returnString;
switch (format.toLowerCase()) {
case "xml":
returnString = Films.getFilmsXML();
request.setAttribute("data", returnString);
requestDispatcher = xmlDispatcher(request);
response.setContentType("text/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
break;
case "csv":
returnString = Films.getFilmsCSV();
request.setAttribute("data",returnString);
requestDispatcher = csvDispatcher(request);
response.setContentType("text");
break;
case "json":
returnString = Films.getFilmsJSON();
request.setAttribute("data", returnString);
requestDispatcher = jsonDispatcher(request);
response.setContentType("text/json");
response.setContentType("text/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
break;
}
}
// Forward the request to the view
requestDispatcher.forward(request, response);
}
private RequestDispatcher xmlDispatcher(HttpServletRequest request){
return request.getRequestDispatcher("xml.jsp");
}
private RequestDispatcher jsonDispatcher(HttpServletRequest request) {
return request.getRequestDispatcher("json.jsp");
}
private RequestDispatcher csvDispatcher(HttpServletRequest request){
return request.getRequestDispatcher("csv.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doGet(request, response);
}
}
其中一个 servlet 根据 url 查询以 xml/json/csv 格式吐出数据(例如 /data?format=json 将 return json数据库中所有电影的数据)。
通过调试,我发现我的 JAXB/GSON 方法正确地从我的模型创建了一个数据集,但是当在对浏览器的响应中查看时,出现了一些错误,可能看起来像 HTML特殊字符的转义序列,例如 xml 标签上的“<”。
这将它缩小到与 JSTL 中的 "" 方法有关的事情。
我的XML显示如下jsp:
<%@page contentType="application/xml" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page trimDirectiveWhitespaces="true"%>
<c:set var="data" value="${data}"></c:set>
<c:out value="${data}" />
我可以通过调试告诉 JSP 响应传递的数据是正确的:
除非我正在做的事情的问题可以通过某种更好的机制来解决 xml/json/csv 数据,而不是简单地将它提供给 jsp 文件?欢迎提出建议。
在JSTL的标签中将escapeXML设置为false以保持原有字符。否则 escapeXML true 将执行相反的操作。
<c:out value='${foo(someParameter)}' escapeXml="false"/>
我有一个 Java 动态 Web 项目,其中一个 Servlet 执行以下操作:
/**
* Servlet implementation class DataAPIServlet
*/
@WebServlet(name = "data", urlPatterns = { "/data" })
public class DataAPIServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String format;
/**
* @see HttpServlet#HttpServlet()
*/
public DataAPIServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
format = request.getParameter("format").replaceAll("\"", "");
// create data model and add to request object
RequestDispatcher requestDispatcher = null;
if (format.equals(null)) {
requestDispatcher = jsonDispatcher(request);
response.setContentType("text/json");
} else {
System.out.println("SERVING FORMATTED DATA : " + format);
String returnString;
switch (format.toLowerCase()) {
case "xml":
returnString = Films.getFilmsXML();
request.setAttribute("data", returnString);
requestDispatcher = xmlDispatcher(request);
response.setContentType("text/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
break;
case "csv":
returnString = Films.getFilmsCSV();
request.setAttribute("data",returnString);
requestDispatcher = csvDispatcher(request);
response.setContentType("text");
break;
case "json":
returnString = Films.getFilmsJSON();
request.setAttribute("data", returnString);
requestDispatcher = jsonDispatcher(request);
response.setContentType("text/json");
response.setContentType("text/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
break;
}
}
// Forward the request to the view
requestDispatcher.forward(request, response);
}
private RequestDispatcher xmlDispatcher(HttpServletRequest request){
return request.getRequestDispatcher("xml.jsp");
}
private RequestDispatcher jsonDispatcher(HttpServletRequest request) {
return request.getRequestDispatcher("json.jsp");
}
private RequestDispatcher csvDispatcher(HttpServletRequest request){
return request.getRequestDispatcher("csv.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doGet(request, response);
}
}
其中一个 servlet 根据 url 查询以 xml/json/csv 格式吐出数据(例如 /data?format=json 将 return json数据库中所有电影的数据)。
通过调试,我发现我的 JAXB/GSON 方法正确地从我的模型创建了一个数据集,但是当在对浏览器的响应中查看时,出现了一些错误,可能看起来像 HTML特殊字符的转义序列,例如 xml 标签上的“<”。
这将它缩小到与 JSTL 中的 "" 方法有关的事情。
我的XML显示如下jsp:
<%@page contentType="application/xml" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page trimDirectiveWhitespaces="true"%>
<c:set var="data" value="${data}"></c:set>
<c:out value="${data}" />
我可以通过调试告诉 JSP 响应传递的数据是正确的:
除非我正在做的事情的问题可以通过某种更好的机制来解决 xml/json/csv 数据,而不是简单地将它提供给 jsp 文件?欢迎提出建议。
在JSTL的标签中将escapeXML设置为false以保持原有字符。否则 escapeXML true 将执行相反的操作。
<c:out value='${foo(someParameter)}' escapeXml="false"/>