我在JSF 2.0中創建了一個自定義組件。其目的是在輸入文本框中顯示會話屬性值。我想要在HttpSessionListener sessionCreated
方法中創建會話屬性。問題是encodeAll
方法在sessionCreated
方法之前被調用。在encodeAll
之前調用sessionCreated
應該怎麼做?在sessionCreated()之前調用的JSF 2.0 encodeAll()
的web.xml
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/components.taglib.xml</param-value>
</context-param>
<listener>
<listener-class>mycomp.jsf.listener.MyListener</listener-class>
</listener>
components.taglib.xml
<facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://mycomp/jsf/components</namespace>
<tag>
<tag-name>mycomp</tag-name>
<component>
<component-type>MyComp</component-type>
</component>
</tag>
</facelet-taglib>
MyListener.java
public class MyListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
session.setAttribute("sessionid", session.getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
}
}
MyComp.java
@FacesComponent(value = "MyComp")
public class MyComp extends UIComponentBase {
public MyComp() {
setRendererType(null);
}
protected Class getComponentClass() {
return this.getClass();
}
@Override
public void decode(FacesContext context) {
//some logic
}
@Override
public void encodeAll(FacesContext context) throws IOException {
String clientId = getClientId(context) + ":token";
HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
String sessionId = (String) session.getAttribute("sessionid");
if (sessionId == null || "".equals(sessionId)) {
throw new RuntimeException("sessionid is missing!");
}
ResponseWriter writer = context.getResponseWriter();
writer.startElement("input", this);
writer.writeAttribute("type", "text", "type");
writer.writeAttribute("name", clientId, "name");
writer.writeAttribute("value", sessionId, "value");
writer.endElement("input");
}
@Override
public String getFamily() {
return null;
}
}
的index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://mycomp/jsf/components">
<h:head>
</h:head>
<h:body>
<h:form>
<t:mycomp />
</h:form>
</h:body>
</html>
設置會話ID,純粹是示範性的,我承擔?你有什麼具體問題?你是否在'session.getAttribute()'行上得到了'NullPointerException'?還是你得到了一個你在那裏編碼的'RuntimeException'? – BalusC 2012-04-12 21:10:29
由於'session'對象爲空,我得到'NullPointerException'。這意味着即使在會話創建之前,encodeAll方法也會執行嗎?無論如何,我的實際目標是創建一個隨機數並將其放在會話範圍內以防止CSRF。 JSF 1.2中的類似代碼在'encodeBegin'之前調用'sessionCreated',所以我沒有這個問題。 – Praneeth 2012-04-13 13:44:05
你應該在問題中更清楚。我已經猜到了,但你對此沒有明確表態。例外情況不應該被忽視,就好像它們是裝飾一樣。我發佈了一個答案。 – BalusC 2012-04-13 14:01:20