I want to learn about reverse ajax, I found a gadget called ICEPush and I thought it could be a good starting point. I am having trouble implementing a very simple application. I am following this tutorial, but instead of Tomcat, I am using Glassfish 3.1 and instead of Eclipse I am using NetBeans 7.1
I did exactly as it says in the tutorial, see my code. This is the page that will be the target of the Ajax push:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Easy Ajax Push - Color</title>
</h:head>
<h:body>
<h:dataTable value="#{messageBean.textList}" var="current">
<h:column>
<h:outputText value="#{current.text}"
style="color: #{current.color};"/>
</h:column>
</h:dataTable>
<hr width="100%"/>
<h:form>
<h:panelGrid columns="4">
Choose a Color:
<h:commandButton value="Red"
action="#{colorBean.chooseColor}"
style="color: white; background-color: red;">
<f:setPropertyActionListener target="#{colorBean.color}" value="red"/>
</h:commandButton>
<h:commandButton value="Blue"
action="#{colorBean.chooseColor}"
style="color: white; background-color: blue;">
<f:setPropertyActionListener target="#{colorBean.color}" value="blue"/>
</h:commandButton>
<h:commandButton value="Green"
action="#{colorBean.chooseColor}"
style="color: white; background-color: green;">
<f:setPropertyActionListener target="#{colorBean.color}" value="green"/>
</h:commandButton>
</h:panelGrid>
</h:form>
</h:body>
</html>
Here are the 2 managed beans that are needed: ColorBean.java
@ManagedBean(name="colorBean")
@ViewScoped
public class ColorBean implements Serializable {
private static final String PUSH_GROUP = "colorPage";
@ManagedProperty(value="#{messageBean}")
private MessageBean messageBean;
private String color = "black";
private String sessionId;
public ColorBean() {
PushRenderer.addCurrentSession(PUSH_GROUP);
FacesContext fcontext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)fcontext.getExternalContext().getSession(false);
sessionId = session.getId();
}
public void setMessageBean(MessageBean messageBean) {
this.messageBean = messageBean;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String chooseColor() {
messageBean.addToList(sessionId, color);
PushRenderer.render(PUSH_GROUP);
return null;
}
}
MessageBean.java
@ManagedBean(name="messageBean")
@ApplicationScoped
public class MessageBean implements Serializable {
private static final int MAX_SIZE = 25;
private List<TextModel> textList = new ArrayList<TextModel>(0);
public MessageBean() {
}
public List<TextModel> getTextList() {
return textList;
}
public void setTextList(List<TextModel> textList) {
this.textList = textList;
}
public void addToList(String sessionId, String color) {
textList.add(makeTextModel(sessionId, color));
if (textList.size() > MAX_SIZE) {
textList.clear();
}
}
private TextModel makeTextModel(String sessionId, String color) {
return new TextModel("User with session ID of " + sessionId + " selected color \"" + color + "\".",
color);
}
}
Also there is a simple pojo to represent the text being presented. TextModel.java
public class TextModel implements Serializable {
private String text;
private String color;
public TextModel() {
}
public TextModel(String text, String color) {
this.text = text;
this.color = color;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return text;
}
}
I am using IceFaces version 3.0.1 and this is how my web.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.icefaces.mandatoryResourceConfiguration</param-name>
<param-value/>
</context-param>
<context-param>
<param-name>org.icefaces.ace.theme</param-name>
<param-value>sam</param-value>
</context-param>
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>com.icesoft.faces.gmapKey</param-name>
<param-value>ABQIAAAADlu0ZiSTam64EKaCQr9eTRTOTuQNzJNXRlYRLknj4cQ89tFfpxTEqxQnVWL4k55OPICgF5_SOZE06A</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/icefaces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/xmlhttp/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
This code gives me 3 problems:
1 - When i run the app and i click in some of the 3 buttons,get an exception that says that i cannot call one use one managed bean inside the other, because their scopes are not compatible:
WARNING: queued exception javax.faces.FacesException: Unable to create managed bean colorBean. The following problems were found: - The scope of the object referenced by expression #{messageBean}, request, is shorter than the referring managed beans (colorBean) scope of view
2 - In the console all the time i see a message that says:
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /ReverseAjaxExample, because request parameters have already been read, or ServletRequest.getReader() has already been called
3 - If i change the @ApplicationScope to @ViewScope and the @ViewScope to @ApplicationScope in the managed beans, the first problem disappears and I can see how the application works, but the reverse ajax does not work, because other browsers do not display the changes. And also I always keep seeing the warning PWC4011
in the console
I never worked with reverse Ajax but I understand it from the theoretical point of view. I'll really appreciate it if you could give me a hand fixing this simple app.
So many times it happens to me that i expend hours debugging and the reason of the bug was just the most simple thing.
Here i answer to my own question show how silly my mistake was:
Instead of using
javax.enterprise.context.ApplicationScoped
I should use
javax.faces.bean.ApplicationScoped
The reason is that i am not using Context and dependency injection, i am just using JSF managed beans.
Thanks to this i remembered it and i just refactored the imports, that's all Ctrl+Space doesn't like me :)