JSF标记未呈现为HTML

时间:2014-11-22 05:26:24

标签: jsf

我是JSF的新手,我的First.xhtml中的JSF标签无效。 我的web.xml文件包含以下代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JSFNewProject</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

我的faces.config文件中的代码是

<?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">
</faces-config>

My code in the First.xhtml is 

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

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
   <title>Facelet</title>
   <h:body>
   <h2>hi Amit</h2>
   <h:form>
   <h:outputLabel id="firstNameOutputId" value="firstname:" />
   <h:inputText id="firstNameInputId" value="#{userRegistration.firstName}"> </h:inputText>

   <h:outputLabel id="lastNameOutputId" value="lastname:" />
   <h:inputText id="lastNameInputId" value="#{userRegistration.lastName}"> </h:inputText> 

  <h:outputLabel id="ageOutputId" value="Age" />
  <h:inputText id="ageInputId" value="#{userRegistration.age}"> </h:inputText> 

  <h:outputLabel id="dobOutputId" value="DoB" />
  <h:inputText id="dobInputId" value="#{userRegistration.dob}"> </h:inputText> 

  <h:outputLabel id="cityOutputId" value="City" />
  <h:inputText id="cityInputId" value="#{userRegistration.city}"> </h:inputText> 

  <h:outputLabel id="salaryOutputId" value="Salary" />
  <h:inputText id="salaryInputId" value="#{userRegistration.salary}"> </h:inputText> 

  <h:commandButton id="userRegistrationCmdBtnId" value="Register" action="userRegistration.processRegistration">
  </h:commandButton>
  </h:form>
  </h:body>
</html>

我尝试了很多选项,例如将URL映射更改为/ faces / *并在Web-INF / Lib中包含Jars但没有用... 正在征求帮助....谢谢

1 个答案:

答案 0 :(得分:0)

您应该使用JSF标准代码(h:headh:body),我在下面举例说明。

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html 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"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <f:facet name="first">
            <meta http-equiv="Content-Type" 
                  content="text/html; charset=UTF-8" />
            <meta name="viewport" 
                  content="user-scalable=no, 
                  width=device-width, 
                  initial-scale=1.0, 
                  maximum-scale=1.0"/>
        </f:facet>

        <title>layout</title>
    </h:head>

    <h:body>
        <p:layout fullPage="true">
            <p:layoutUnit position="north" size="50">
                <h:outputText value="Top content." />
            </p:layoutUnit>
            <p:layoutUnit position="south" size="100">
                <h:outputText value="Bottom content." />
            </p:layoutUnit>
            <p:layoutUnit position="west" size="300">
                <h:outputText value="Left content" />
            </p:layoutUnit>
            <p:layoutUnit position="east" size="200">
                <h:outputText value="Right Content" />
            </p:layoutUnit>
            <p:layoutUnit position="center">
                <h:outputText value="Center Content" />
            </p:layoutUnit>
        </p:layout>
    </h:body>
</html>

<h:head>是一个JSF标记(随JSF 2.0引入),用于处理页面的<head>部分。拥有这样的JSF标记的兴趣在于,这个头部成为JSF组件树的一部分,因此,您可以在Java代码中对其进行操作。