我想学习反向 ajax,我发现了一个名为 ICEPush 的小工具,我认为这可能是一个很好的起点。我在实现一个非常简单的应用程序时遇到了麻烦。我正在关注本教程,但我使用的是 Glassfish 3.1 而不是 Tomcat,而不是 Eclipse 我使用的是 NetBeans 7.1
我完全按照教程中的说明进行操作,请参阅我的代码。这是将成为 Ajax 推送目标的页面:
<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>
以下是需要的 2 个托管 bean: 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);
}
}
还有一个简单的 pojo 来表示正在呈现的文本。 文本模型.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;
}
}
我正在使用 IceFaces 3.0.1 版,这就是我的web.xml的样子:
<?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>
这段代码给了我3个问题:
1 - 当我运行应用程序并单击 3 个按钮中的一些按钮时,会出现一个异常,指出我不能调用一个在另一个内部使用一个托管 bean,因为它们的范围不兼容:
警告:排队异常 javax.faces.FacesException:无法创建托管 bean colorBean。发现以下问题: - 表达式#{messageBean},request所引用的对象的范围比引用的托管bean(colorBean)的视图范围短
2 - 在控制台中我一直看到一条消息:
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /ReverseAjaxExample,因为请求参数已经被读取,或者 ServletRequest.getReader() 已经被调用
3 - 如果我在托管 bean 中将 @ApplicationScope 更改为 @ViewScope 并将 @ViewScope 更改为 @ApplicationScope,第一个问题就会消失,我可以看到应用程序是如何工作的,但是反向 ajax 不起作用,因为其他浏览器不显示变化。而且我总是PWC4011
在控制台中看到警告
我从未使用过反向 Ajax,但我从理论的角度理解它。如果您能帮我修复这个简单的应用程序,我将不胜感激。