在执行xhtml时获取Illegalstate异常

时间:2013-02-21 05:08:49

标签: java jsf-2 xhtml

我在做一个jsf应用程序时遇到了一些异常。这是我的xhtml代码。

<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">

        <f:view> 
            <h:head><title>Index</title></h:head>
                <body>
                <h:form>
                    <h:outputText value="#{login.userName}"></h:outputText><br/>
                    <h:inputText id="userId" value="#{emp.userId}"></h:inputText>
                    <h:commandButton action="#{emp.check}" value="check"/>
                </h:form>
            </body>
        </f:view>   
 </html>

这里login是托管bean。我从bean获取用户名并尝试显示它。而emp是我在这里给用户ID的另一个bean。 当我在服务器上运行时,我会遇到异常。

Stacktrace:] with root cause
java.lang.IllegalStateException: Component javax.faces.component.UIViewRoot@11a700a not expected type.  Expected: javax.faces.component.UIOutput.  Perhaps you're missing a tag?
    at com.sun.faces.taglib.html_basic.OutputTextTag.setProperties(OutputTextTag.java:126)
    at javax.faces.webapp.UIComponentClassicTagBase.findComponent(UIComponentClassicTagBase.java:686)

2 个答案:

答案 0 :(得分:1)

尝试使用此代码!

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <ui:composition xmlns="http://www.w3.org/1999/xhtml"   
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:ui="http://java.sun.com/jsf/facelets">

    <f:view> 
        <h:head><title>Index</title></h:head>
            <body>
            <h:form>
                <h:outputText value="#{login.userName}"></h:outputText><br/>
                <h:inputText id="userId" value="#{emp.userId}"></h:inputText>
                <h:commandButton action="#{emp.check}" value="check"/>
            </h:form>
        </body>
    </f:view>   
</ui:composition>

答案 1 :(得分:-1)

更新您的代码并将<html>放在<f:view>内,如下所示:

<f:view>
    <?xml version="1.0" encoding="UTF-8"?>
    <!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:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core">

           //facelet code..

         </html>
</f:view>