JSF ، تجاوز رؤوس HTTP
سؤال
أحتاج بشدة إلى تجاوز JSF 2.0 نوع المحتوى رأس. الافتراضي هو
Content-Type:application/xhtml+xml; charset=UTF-8
لكني بحاجة
Content-Type:text/html; charset=UTF-8
شكرًا.
المحلول
ماذا عن
<f:view contentType="text/html" />
نصائح أخرى
استخدم ال Doctype الحق.
<!DOCTYPE html>
لا شيء آخر. أيضا لا تضع <?xml?>
إعلان في الأعلى. هذا قالب أدنى:
<!DOCTYPE html>
<html
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Insert your title</title>
</h:head>
<h:body>
<h1>Hello World</h1>
</h:body>
</html>
إنه HTML5 Doctype. إنه متوافق تمامًا مع علامة XHTML 1.x ويضيف المزيد من المزايا.
النهج التالي يعمل في جميع المتصفحات:
اكتب phaselistener:
public class ContentTypePhaseListener implements PhaseListener {
public PhaseId getPhaseId()
{
return PhaseId.RENDER_RESPONSE;
}
public void afterPhase(PhaseEvent event)
{
}
public void beforePhase(PhaseEvent event)
{
FacesContext facesContext = event.getFacesContext();
HttpServletResponse response = (HttpServletResponse) facesContext
.getExternalContext().getResponse();
response.addHeader("Content-Type", "text/html; charset=UTF-8");
}
}
وتسجيله في الوجوه context.xml:
<lifecycle>
<phase-listener>com.mycompnay.listener.ContentTypePhaseListener </phase-listener>
</lifecycle>
لا تنتمي إلى StackOverflow