gpt4 book ai didi

java - 我的第一个反向 ajax 应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:52 24 4
gpt4 key购买 nike

我想了解反向 ajax,我发现了一个名为 ICEPush 的小工具,我认为它可能是一个很好的起点。我在实现一个非常简单的应用程序时遇到了问题。我正在关注这个 tutorial ,但我使用的不是 Tomcat,而是 Glassfish 3.1,我使用的是 NetBeans 7.1 而不是 Eclipse

我完全按照教程中的说明进行操作,请参阅我的代码。这是将成为 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 来表示所呈现的文本。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;
}
}

我正在使用 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,因为它们的范围不兼容:

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 - 在控制台中,我一直看到一条消息:

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 - 如果我将托管 bean 中的 @ApplicationScope 更改为 @ViewScope 并将 @ViewScope 更改为 @ApplicationScope,第一个问题就会消失,我可以看到应用程序是如何工作的,但是反向 ajax 不起作用,因为其他浏览器不显示更改。而且我总是在控制台中看到警告 PWC4011

我从未使用过反向 Ajax,但我从理论的角度理解它。如果您能帮助我修复这个简单的应用程序,我将不胜感激。

最佳答案

很多次发生在我身上,我花了几个小时调试,而错误的原因只是最简单的事情。

在这里我回答我自己的问题表明我的错误是多么愚蠢:

而不是使用

javax.enterprise.context.ApplicationScoped

我应该用

javax.faces.bean.ApplicationScoped

原因是我没有使用上下文和依赖项注入(inject),我只是使用 JSF 托管 bean。

多亏了这个,我才记住它,我只是重构了导入,仅此而已Ctrl+Space 不喜欢我 :)

关于java - 我的第一个反向 ajax 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10354503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com