Seam 未注入有状态 EJB 依赖项

发布于 2024-08-23 05:14:13 字数 10350 浏览 4 评论 0原文

我一直没能找到这个问题的解决方案,而且我在 Seam 论坛上发的帖子也没有得到答复。我希望这里有人可以提供帮助。

我是一个使用 Seam 的菜鸟,所以我确信我只是做了一些愚蠢的事情。任何人能给我的任何帮助将不胜感激......我浪费了时间却一无所获。我使用jboss工具在eclipse中生成了一个seam项目。该工具采用预先创建的 JPA 实体并创建了一个基本的 Web 应用程序。我的第一步是添加一个注册页面,以便我可以创建一些用户并使用数据库身份验证登录(我对此进行了设置,但在注册页面工作后将对其进行测试)。我使用 seam-booking 示例来指导我(基本上将表单内容和其他字段集成到 seam-gen 应用程序的视图中)。当我测试注册页面时,我收到所有注入字段的 NullPointerExceptions。我浏览了整个接缝预订示例,在网上搜索示例,并快速阅读了一本书的某些部分,但我没有看到哪里需要任何额外的配置信息。我到底做错了什么?请帮忙!!!

我正在使用 JBoss Server(社区版)5.1.0GA 和 Seam 2.2.0GA。

如果您需要比我发布的内容更多的信息,请告诉我。提前感谢大家的帮助!

有状态EJB:

@Stateful
@Scope(EVENT)
@Name("register")
public class RegisterAction implements Register {
    @In
    private User user;

    @PersistenceContext
    private EntityManager entityManager;

    @In 
    private FacesMessages facesMessages;

    private String verify = null;

    private boolean registered = false;

    public void registerUser() {
        if (user.getPassword().equals(verify)) {
            List existing = entityManager
                    .createQuery(
                            "select u.userName from User u where u.userName=#{user.userName}")
                    .getResultList();
            if (existing.size() == 0) {
                entityManager.persist(user);
                facesMessages
                        .add("Successfully registered as #{user.userName}");
                registered = true;
            } else {
                facesMessages.addToControl("userName",
                        "Username #{user.userName} already exists");
            }
        } else {
            facesMessages.addToControl("verify", "Re-enter your password");
            verify = null;
        }
    }

    public void invalid() {
        facesMessages.add("Please try again");
    }

    public boolean isRegistered() {
        return registered;
    }

    public String getVerify() {
        return verify;
    }

    public void setVerify(String verify) {
        this.verify = verify;
    }

    @Remove
    @Destroy
    public void destroy() {
    }
}

EJB本地接口:

@Local
public interface Register
{
   public void registerUser();
   public void invalid();
   public String getVerify();
   public void setVerify(String verify);
   public boolean isRegistered();
   public void destroy();
}

注册页面XHTML:

<ui:define name="body">
    <rich:panel>
        <f:facet name="header">Register</f:facet>
        <h:form id="registration">
            <fieldset><s:decorate id="firstNameDecorate"
                template="layout/edit.xhtml">
                <ui:define name="label">First Name:</ui:define>
                <h:inputText id="firstName" value="#{user.firstName}"
                    required="true">
                    <a:support id="onblur" event="onblur" reRender="firstNameDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="lastNameDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Last Name:</ui:define>
                <h:inputText id="lastName" value="#{user.lastName}" required="true">
                    <a:support id="onblur" event="onblur" reRender="lastNameDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="emailDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Email:</ui:define>
                <h:inputText id="emailAddress" value="#{user.emailAddress}"
                    required="true">
                    <a:support id="onblur" event="onblur" reRender="emailDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="usernameDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Username:</ui:define>
                <h:inputText id="username" value="#{user.userName}" required="true">
                    <a:support id="onblur" event="onblur" reRender="usernameDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="passwordDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Password:</ui:define>
                <h:inputSecret id="password" value="#{user.password}"
                    required="true" />
            </s:decorate> <s:decorate id="verifyDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Verify Password:</ui:define>
                <h:inputSecret id="verify" value="#{register.verify}"
                    required="true" />
            </s:decorate>

            <div class="buttonBox"><h:commandButton id="register"
                value="Register" action="#{register.registerUser}" /> &#160; <s:button
                id="cancel" value="Cancel" view="/index.xhtml" /></div>
        </fieldset>
        </h:form>
    </rich:panel>
    </ui:define>
</ui:composition>

模板 XHTML(注册页面使用此):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j"
xmlns:s="http://jboss.com/products/seam/taglib"
contentType="text/html">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>MyApp</title>
  <link rel="shortcut icon" href="#{request.contextPath}/favicon.ico"/>
  <a:loadStyle src="resource:///stylesheet/theme.xcss"/>
  <a:loadStyle src="/stylesheet/theme.css"/>
  <ui:insert name="head"/>
</head>
<body>
  <ui:include src="menu.xhtml">
     <ui:param name="projectName" value="MyApp"/>
  </ui:include>
  <div class="body">
     <h:messages id="messages" globalOnly="true" styleClass="message"
        errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"
        rendered="#{showGlobalMessages != 'false'}"/>
     <ui:insert name="body"/>
  </div>
  <div class="footer">
     <p>Powered by <a href="http://seamframework.org">Seam</a> #{org.jboss.seam.version} and <a href="http://www.jboss.org/jbossrichfaces">RichFaces</a>. Generated by seam-gen.</p>
     <s:fragment rendered="#{init.debug}">
     <a:log hotkey="D"/>
     <p style="margin-top: -0.5em;">
        Conversation: id = #{conversation.id}, #{conversation.longRunning ? 'long running' : 'temporary'}#{conversation.nested ? ', nested, parent id = '.concat(conversation.parentId) : ''}
        #{' - '}
        Ajax4jsf Log (Ctrl+Shift+D)
        #{' - '}
        <s:link id="debugConsole" view="/debug.xhtml" value="Debug console" target="debugConsole"/>
        #{' - '}
        <s:link id="resetSession" view="/home.xhtml" action="#{org.jboss.seam.web.session.invalidate}" propagation="none" value="Terminate session"/>
     </p>
     </s:fragment>
  </div>
 </body>
</html>
</f:view>

Seam Components.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
        xmlns:core="http://jboss.com/products/seam/core"
        xmlns:persistence="http://jboss.com/products/seam/persistence"
        xmlns:drools="http://jboss.com/products/seam/drools"
        xmlns:bpm="http://jboss.com/products/seam/bpm"
        xmlns:security="http://jboss.com/products/seam/security"
        xmlns:mail="http://jboss.com/products/seam/mail"
        xmlns:web="http://jboss.com/products/seam/web"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
             http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
             http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
             http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
             http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">

 <core:init debug="true" jndi-pattern="@jndiPattern@"/>

 <core:manager concurrent-request-timeout="500"
             conversation-timeout="120000"
             conversation-id-parameter="cid"
             parent-conversation-id-parameter="pid"/>

 <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
 <web:hot-deploy-filter url-pattern="*.seam"/>

 <persistence:managed-persistence-context name="entityManager" auto-create="true"
                  persistence-unit-jndi-name="java:/MyAppEntityManagerFactory"/>

 <drools:rule-base name="securityRules">
  <drools:rule-files>
     <value>/security.drl</value>
  </drools:rule-files>
 </drools:rule-base>

 <security:rule-based-permission-resolver security-rules="#{securityRules}"/>

<security:identity-manager identity-store="#{jpaIdentityStore}" />

<security:jpa-identity-store
    entity-manager="#{entityManager}" user-class="my.app.path.dao.profiles.User"
    role-class="my.app.path.dao.profiles.Role" />

 <event type="org.jboss.seam.security.notLoggedIn">
  <action execute="#{redirect.captureCurrentView}"/>
 </event>
 <event type="org.jboss.seam.security.loginSuccessful">
  <action execute="#{redirect.returnToCapturedView}"/>
 </event>

 <mail:mail-session host="localhost" port="25"/>
</components>

I have not been able to find a solution to this problem and my post to the Seam forum has gone unanswered. I am hope someone here can help.

I am a noob working with Seam, so I am sure I am just doing something stupid. Any help anyone can give me would be greatly appreciated... I have wasted hours and gotten nowhere. I used the jboss tools to generate a seam project in eclipse. The tool took pre-created JPA entities and created a basic webapp. My first step was to add a registration page so that I can create some users and login using database authentication (I set this up, but will test it once the registration page works). I used the seam-booking example to guide me (basically integrating the form stuff plus additional fields into the view of the seam-gen app). When I test the registration page, I get NullPointerExceptions for all of the injected fields. I have looked through the entire seam-booking example, scoured the web looking at examples, and quickly read through some sections of a book and I do not see where there is any additional configuration information needed. What in the world am I doing wrong? Please help!!!

I am using JBoss Server (community edition) 5.1.0GA and Seam 2.2.0GA.

If you need any more information than what I am posting, please let me know. Thanks to all ahead of time for your help!!

Stateful EJB:

@Stateful
@Scope(EVENT)
@Name("register")
public class RegisterAction implements Register {
    @In
    private User user;

    @PersistenceContext
    private EntityManager entityManager;

    @In 
    private FacesMessages facesMessages;

    private String verify = null;

    private boolean registered = false;

    public void registerUser() {
        if (user.getPassword().equals(verify)) {
            List existing = entityManager
                    .createQuery(
                            "select u.userName from User u where u.userName=#{user.userName}")
                    .getResultList();
            if (existing.size() == 0) {
                entityManager.persist(user);
                facesMessages
                        .add("Successfully registered as #{user.userName}");
                registered = true;
            } else {
                facesMessages.addToControl("userName",
                        "Username #{user.userName} already exists");
            }
        } else {
            facesMessages.addToControl("verify", "Re-enter your password");
            verify = null;
        }
    }

    public void invalid() {
        facesMessages.add("Please try again");
    }

    public boolean isRegistered() {
        return registered;
    }

    public String getVerify() {
        return verify;
    }

    public void setVerify(String verify) {
        this.verify = verify;
    }

    @Remove
    @Destroy
    public void destroy() {
    }
}

EJB local interface:

@Local
public interface Register
{
   public void registerUser();
   public void invalid();
   public String getVerify();
   public void setVerify(String verify);
   public boolean isRegistered();
   public void destroy();
}

XHTML of registratin page:

<ui:define name="body">
    <rich:panel>
        <f:facet name="header">Register</f:facet>
        <h:form id="registration">
            <fieldset><s:decorate id="firstNameDecorate"
                template="layout/edit.xhtml">
                <ui:define name="label">First Name:</ui:define>
                <h:inputText id="firstName" value="#{user.firstName}"
                    required="true">
                    <a:support id="onblur" event="onblur" reRender="firstNameDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="lastNameDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Last Name:</ui:define>
                <h:inputText id="lastName" value="#{user.lastName}" required="true">
                    <a:support id="onblur" event="onblur" reRender="lastNameDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="emailDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Email:</ui:define>
                <h:inputText id="emailAddress" value="#{user.emailAddress}"
                    required="true">
                    <a:support id="onblur" event="onblur" reRender="emailDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="usernameDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Username:</ui:define>
                <h:inputText id="username" value="#{user.userName}" required="true">
                    <a:support id="onblur" event="onblur" reRender="usernameDecorate" />
                </h:inputText>
            </s:decorate> <s:decorate id="passwordDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Password:</ui:define>
                <h:inputSecret id="password" value="#{user.password}"
                    required="true" />
            </s:decorate> <s:decorate id="verifyDecorate" template="layout/edit.xhtml">
                <ui:define name="label">Verify Password:</ui:define>
                <h:inputSecret id="verify" value="#{register.verify}"
                    required="true" />
            </s:decorate>

            <div class="buttonBox"><h:commandButton id="register"
                value="Register" action="#{register.registerUser}" />   <s:button
                id="cancel" value="Cancel" view="/index.xhtml" /></div>
        </fieldset>
        </h:form>
    </rich:panel>
    </ui:define>
</ui:composition>

Template XHTML (Registration page uses this):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j"
xmlns:s="http://jboss.com/products/seam/taglib"
contentType="text/html">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>MyApp</title>
  <link rel="shortcut icon" href="#{request.contextPath}/favicon.ico"/>
  <a:loadStyle src="resource:///stylesheet/theme.xcss"/>
  <a:loadStyle src="/stylesheet/theme.css"/>
  <ui:insert name="head"/>
</head>
<body>
  <ui:include src="menu.xhtml">
     <ui:param name="projectName" value="MyApp"/>
  </ui:include>
  <div class="body">
     <h:messages id="messages" globalOnly="true" styleClass="message"
        errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"
        rendered="#{showGlobalMessages != 'false'}"/>
     <ui:insert name="body"/>
  </div>
  <div class="footer">
     <p>Powered by <a href="http://seamframework.org">Seam</a> #{org.jboss.seam.version} and <a href="http://www.jboss.org/jbossrichfaces">RichFaces</a>. Generated by seam-gen.</p>
     <s:fragment rendered="#{init.debug}">
     <a:log hotkey="D"/>
     <p style="margin-top: -0.5em;">
        Conversation: id = #{conversation.id}, #{conversation.longRunning ? 'long running' : 'temporary'}#{conversation.nested ? ', nested, parent id = '.concat(conversation.parentId) : ''}
        #{' - '}
        Ajax4jsf Log (Ctrl+Shift+D)
        #{' - '}
        <s:link id="debugConsole" view="/debug.xhtml" value="Debug console" target="debugConsole"/>
        #{' - '}
        <s:link id="resetSession" view="/home.xhtml" action="#{org.jboss.seam.web.session.invalidate}" propagation="none" value="Terminate session"/>
     </p>
     </s:fragment>
  </div>
 </body>
</html>
</f:view>

Seam Components.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
        xmlns:core="http://jboss.com/products/seam/core"
        xmlns:persistence="http://jboss.com/products/seam/persistence"
        xmlns:drools="http://jboss.com/products/seam/drools"
        xmlns:bpm="http://jboss.com/products/seam/bpm"
        xmlns:security="http://jboss.com/products/seam/security"
        xmlns:mail="http://jboss.com/products/seam/mail"
        xmlns:web="http://jboss.com/products/seam/web"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
             http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
             http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
             http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
             http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">

 <core:init debug="true" jndi-pattern="@jndiPattern@"/>

 <core:manager concurrent-request-timeout="500"
             conversation-timeout="120000"
             conversation-id-parameter="cid"
             parent-conversation-id-parameter="pid"/>

 <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
 <web:hot-deploy-filter url-pattern="*.seam"/>

 <persistence:managed-persistence-context name="entityManager" auto-create="true"
                  persistence-unit-jndi-name="java:/MyAppEntityManagerFactory"/>

 <drools:rule-base name="securityRules">
  <drools:rule-files>
     <value>/security.drl</value>
  </drools:rule-files>
 </drools:rule-base>

 <security:rule-based-permission-resolver security-rules="#{securityRules}"/>

<security:identity-manager identity-store="#{jpaIdentityStore}" />

<security:jpa-identity-store
    entity-manager="#{entityManager}" user-class="my.app.path.dao.profiles.User"
    role-class="my.app.path.dao.profiles.Role" />

 <event type="org.jboss.seam.security.notLoggedIn">
  <action execute="#{redirect.captureCurrentView}"/>
 </event>
 <event type="org.jboss.seam.security.loginSuccessful">
  <action execute="#{redirect.returnToCapturedView}"/>
 </event>

 <mail:mail-session host="localhost" port="25"/>
</components>

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

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

发布评论

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

评论(1

酒儿 2024-08-30 05:14:13

我很着急,快速回答一下:

首先要解决Seam问题,“User”是一个自动创建的Seam组件还是有一个工厂方法来创建它?用 @In 注释字段只是所需内容的一半,您仍然需要提供值的另一端。

从更大的角度来看:

  • 假设 User 是一个实体,将其作为 Seam 组件并不是一个好的做法(Seam 造成了太多的开销)。
  • 您的有状态 bean 的范围是一个事件。这不太可能是您想要的,Seam 中的 EVENT 范围与 servlet 的请求相同。

看看你是否可以获得“Seam in Action”的副本,它很好地解释了基础知识。

A quick answer as I'm on the rush:

To address the Seam question first, is "User" a Seam component that will be auto-created or is there a factory method to create one? Annotating a field with @In is just one half of what's required, you still need the other end which supplies the value.

In the bigger picture:

  • presuming User is an entity, having it as a Seam component is not a good practice (way too much overhead caused by Seam).
  • your stateful bean is scoped as an EVENT. This is unlikely to be your desire, the EVENT scope in Seam is the same as a request for a servlet.

See if you can get a copy of "Seam in Action", it explains the fundamentals very well.

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