无法获得“正确”的信息servlet 中的有状态会话 bean

发布于 2024-12-22 21:18:03 字数 2798 浏览 2 评论 0原文

首先:祝大家圣诞快乐:)

我在 servlet 中接收有状态会话 bean 时遇到了一些问题。简单地说,应该由 JSF 设置的值是 null。我所拥有的是一个 JSF 页面 + 一个有状态会话 bean + 临时 Properties bean 类 + servlet:

首先是 JSF2.0 页面的一部分:

    <h:form id="set_args">
        <h2>Prosze wprowadzic wartosci: (w przyszlosci...)</h2>
        <div class="group">
            <h:outputLabel for="start" value="Wybierz punkt startowy:" />
            <h:inputText id="start" value="#{properties.start}" />
        </div>
        <div class="group">
            <h:outputLabel for="stop" value="Wybierz punkt docelowy:" />
            <h:inputText id="stop" value="#{properties.end}" />
        </div>
        <div class="group">
            <h:commandButton id="start_sim"
                action="#{SimulationBean.setValues()}" value="Rozpocznij" />
        </div>
    </h:form>

Propeties 类:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@RequestScoped
@Named
public class Properties {

private String start;
private String end;

public String getStart() {
    return start;
}

public void setStart(String start) {
    this.start = start;
}

public String getEnd() {
    return end;
}

public void setEnd(String end) {
    this.end = end;
}

}

然后是一个 EJB:

@Named("SimulationBean")
@Stateful
@SessionScoped
@NamedQueries({ @NamedQuery(name = "findAllJunctions", query = "SELECT j FROM Junction") })
public class SimulationBean implements SimulationBeanLocal {

@Inject
private Properties properties;

private String start;

private String end;

public String getStart() {
    return start;
}

public void setStart(String start) {
    this.start = start;
}

public String getEnd() {
    return end;
}

public void setEnd(String end) {
    this.end = end;
}

public void setValues() {
    this.start = this.properties.getStart();
    this.end = this.properties.getEnd();
    System.out.println("Print VALUES:");
    System.out.println(this.properties.getStart());
    System.out.println(this.properties.getEnd());
}

和 servlet:

@WebServlet("/SimulationServlet")
@EJB(name="SimulationBean", beanInterface = SimulationBeanLocal.class)
public class SimulationServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    Context c;
    try {
        c = new InitialContext();
        SimulationBeanLocal bean = (SimulationBeanLocal) c.lookup("java:module/SimulationBean");

当我调用 servlet 时,我得到一个“start”end“end”属性中的空值。 一切都充满了战争。

我真的做错了什么吗?:)我会很感激任何线索。 谢谢。

First of all: I wish you all Merry Christmas :)

I've got some problem while receiving a stateful session bean in servlet. Simply values that should be set by a JSF are nulls. What I have is a JSF page + one stateful session bean + the temporary Properties bean class + servlet:

First a part of JSF2.0 page:

    <h:form id="set_args">
        <h2>Prosze wprowadzic wartosci: (w przyszlosci...)</h2>
        <div class="group">
            <h:outputLabel for="start" value="Wybierz punkt startowy:" />
            <h:inputText id="start" value="#{properties.start}" />
        </div>
        <div class="group">
            <h:outputLabel for="stop" value="Wybierz punkt docelowy:" />
            <h:inputText id="stop" value="#{properties.end}" />
        </div>
        <div class="group">
            <h:commandButton id="start_sim"
                action="#{SimulationBean.setValues()}" value="Rozpocznij" />
        </div>
    </h:form>

The Propeties class:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@RequestScoped
@Named
public class Properties {

private String start;
private String end;

public String getStart() {
    return start;
}

public void setStart(String start) {
    this.start = start;
}

public String getEnd() {
    return end;
}

public void setEnd(String end) {
    this.end = end;
}

}

Then an EJB:

@Named("SimulationBean")
@Stateful
@SessionScoped
@NamedQueries({ @NamedQuery(name = "findAllJunctions", query = "SELECT j FROM Junction") })
public class SimulationBean implements SimulationBeanLocal {

@Inject
private Properties properties;

private String start;

private String end;

public String getStart() {
    return start;
}

public void setStart(String start) {
    this.start = start;
}

public String getEnd() {
    return end;
}

public void setEnd(String end) {
    this.end = end;
}

public void setValues() {
    this.start = this.properties.getStart();
    this.end = this.properties.getEnd();
    System.out.println("Print VALUES:");
    System.out.println(this.properties.getStart());
    System.out.println(this.properties.getEnd());
}

And servlet:

@WebServlet("/SimulationServlet")
@EJB(name="SimulationBean", beanInterface = SimulationBeanLocal.class)
public class SimulationServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    Context c;
    try {
        c = new InitialContext();
        SimulationBeanLocal bean = (SimulationBeanLocal) c.lookup("java:module/SimulationBean");

And when I call the servlet I get a null values in "start" end "end" properties.
Everthing is packed in WAR.

Am I doing something really wrong ?:) I will appreciate any clues.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2024-12-29 21:18:03

servlet 显然是由与 JSF 表单提交不同的 HTTP 请求调用的。由于 Properties bean 是请求范围的,因此每个 HTTP 请求都将创建一个新实例,并且不会像您预期的那样重用以前的 HTTP 请求中的实例。相反,您需要访问在有状态 EJB 或范围比请求范围更宽的其他某个 bean 中设置的值。

但总而言之,整个结构有点奇怪和令人困惑。您在这里混合了几个概念和职责。由于问题中根本没有详细说明功能需求,因此不可能提出正确的方法。

The servlet is clearly invoked by a different HTTP request than the JSF form submit. As the Properties bean is request scoped, every HTTP request will create a new instance and not reuse the instance from previous HTTP requests as you seem to expect. Instead, you need to access the values which are been set in the stateful EJB or some other bean which has a scope broader than the request scope.

But all with all, the whole construct is somewhat odd and confusing. You're mixing several concepts and responsibiities here. Since the functional requirements are not elaborated in the question at all, it's not possible to propose the right approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文