【问题标题】:Error deploying on Glassfish v.3 after using @SessionScoped使用 @SessionScoped 后在 Glassfish v.3 上部署时出错
【发布时间】:2013-09-24 18:10:06
【问题描述】:

我是 JSF、Java EE 的新手,在尝试使用 Faces.xml、JSF 和 ManagedBeans 的一个小测试用例时遇到了一点问题。

我有一个名为 beanManager.java 的 managedBean,其中有两个字段(名称和测试名称)和一个名为 testcase1() 的方法,它只返回一个字符串。现在,在 frontpage.xhtml 中,我有一个输入文本框,它从用户那里获取名称,一旦用户单击提交按钮,就会调用 testcase1() 方法,它所做的只是设置 BeanManager 的 testname 字段.java 类与用户的输入,但出于某些原因 - 显然为什么我要发送此消息 - 当用户输入名称并点击搜索按钮时,页面导航到 displaypage.xhtml 并且页面不显示任何内容。它应该显示用户输入的名称,但它是空白的。有人建议我使用 @SessionScoped 而不是 @RequestScoped,但现在的问题是,当我在 glassfish 上部署 Java EE 应用程序时,出现以下错误:

Exception Occurred :Error occurred during deployment: Exception while loading the app : 
java.lang.IllegalStateException: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: 
javax.servlet.ServletException: com.sun.enterprise.container.common.spi.util.InjectionException: 
Error creating managed object for class: class org.jboss.weld.servlet.WeldListener

以下是我的文件..

BeanManager.java

    import javax.enterprise.context.SessionScoped;
    import javax.faces.bean.ManagedBean;


    @ManagedBean(name = "user")
    @SessionScoped

public class BeanManager {
    private String name = "";
    private String testname = "";


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String testcase1(){
        setTestname(this.name);
        return "test1";
    }


    public String getTestname() {
        return testname;
    }

    public void setTestname(String testname) {
        this.testname = testname;
    }

}

frontpage.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="WEB-INF/templates/origin.xhtml">
    <ui:define name="content">
        <f:view>
            <h:form>
                <h:panelGrid>
                    <h:inputText value="#{user.name}" required = "true"/>
                </h:panelGrid>
                <h:commandButton

                    action="#{user.testcase1()}"
                    value="Search"></h:commandButton>
            </h:form>
            </f:view>


    </ui:define>
</ui:composition>
</html>

displaypage.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/templates/origin.xhtml">
    <ui:define name="content">
       <h:panelGrid>
        <h:outputText value="#{user.testname}"/>
        <h:commandButton id="back" value="GoBack" action="frontpage"/>
       </h:panelGrid>
    </ui:define>
</ui:composition>
</html>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>


<faces-config
    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-facesconfig_2_0.xsd"
    version="2.0">

    <navigation-rule>
        <from-view-id>/frontpage.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{user.testcase1()}</from-action>
            <from-outcome>test1</from-outcome>
            <to-view-id>/displaypage.xhtml</to-view-id>
            <redirect/>
        </navigation-case>
         </navigation-rule>
    <navigation-rule>
    <from-view-id>/displaypage.xhtml</from-view-id>
            <navigation-case>
                <from-outcome>GoBack</from-outcome>
                <to-view-id>/frontpage.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

【问题讨论】:

    标签: jsf cdi session-scope


    【解决方案1】:

    您不能将 CDI 注释 javax.enterprise.context.SessionScoped 与 JSF javax.faces.bean.ManagedBean 一起使用。你要么去纯 JSF 与

    • javax.faces.bean.ManagedBean(命名)和javax.faces.bean.SessionScoped(范围)

      或纯 CDI

    • javax.inject.Named(命名)和javax.enterprise.context.SessionScoped(范围)

    相关:

    【讨论】:

    • 它现在可以工作,但看起来就像名字所暗示的那样,SessionScoped 阻止了更改。假设我尝试 action = "#{user.testcase2()}" 而不是 action = #{user.testcase1()} 并保存它,每次编译和打包时,我都会得到 action = #{user.testcase1() } 而不是更新的 action = #{user.testcase2()} 如何影响更改?
    • 您的编辑未显示在磁盘上的物理文件上与托管 bean 的范围无关。在您的 ide 中清理并重建您的项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2013-02-03
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多