【问题标题】:JSF web project <h:xxx> tag invisibleJSF web 项目 <h:xxx> 标签不可见
【发布时间】:2012-05-02 09:41:24
【问题描述】:

我按照教程制作了一个 JSF 网络项目,但它不起作用,&lt;h : xxx&gt; 标记中的所有内容在 Firefox 或 googlechrome 等浏览器中都是不可见的。

应该这样做: 首先您访问 index.html,输入您的姓名并按下按钮。 bean 将您的姓名传输到 JSP 和 JSP 打印 欢迎“你的名字”

但如前所述,输入字段和按钮是不可见的(它们在页面代码内但它们的长度为 0px,手动设置新宽度也不起作用)

在 Eclipse 图形预览工具中一切看起来都很正常。 :/

my index.html

<?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:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body>
        <h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
        <h:outputText value="test"  />
        <h:form>
           <h:inputText value="#{helloBean.name}"></h:inputText>
           <h:commandButton value="Welcome Me" action="welcome"></h:commandButton>
        </h:form>
    </h:body>
</html>

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
 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-app_2_5.xsd">
 <display-name>webApp</display-name>
 <!-- JSF mapping -->
 <servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!-- Map these files with JSF -->
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.faces</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>
 <!-- Welcome page -->
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
</web-app>

我的豆子:

package net.viralpatel.maven;



import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;

@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

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

生成的 HTML:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body bgcolor="white">
        <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
        <h4>Welcome #{helloBean.name}</h4>
    </h:body>
</html>

在我的pom.xml你可以找到

  • myfaces-api 1.2.2
  • 战斧 1.1.9
  • myfaces-impl 1.2.2
  • jstl 1.1.2
  • taglib 标准 1.1.2
  • geronimo_servlet_2.5_spec 1.2
  • jboss-jsf-api_2.0_spec

我使用的是 tomcat 7 服务器

【问题讨论】:

    标签: java jsf-2 maven-3 tomcat7 myfaces


    【解决方案1】:

    您的错误是您对 JSF 视图文件使用了 .html 扩展名而不是 .xhtml。默认的 JSF 后缀是.xhtml

    有两种方法可以解决这个问题:

    1. 告诉 JSF 使用 .html 作为默认后缀。

      <context-param>
          <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
          <param-value>.html</param-value>
      </context-param>
      
    2. 或者,将 index.html 文件重命名为 index.xhtml 并相应地修复您的 web.xml

      <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>
      <welcome-file-list>
          <welcome-file>index.xhtml</welcome-file>
      </welcome-file-list>
      

    相对于 IDE 工具(自动完成等),使用 XHTML 具有更多优势。

    【讨论】:

    • 我根据你的BalusC修改了我的web.xml,它成功了!谢谢 =D 但是为什么呢?如果我的映射错误,为什么我会被发送到 index.html?
    【解决方案2】:

    在您的 web.xml 中,不应注释此 servlet 映射:

     <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>*.html</url-pattern>
     </servlet-mapping>
    

    【讨论】:

    • 它没有被淘汰。这是无效语言突出显示的结果(Java 而不是 XML)。 /* 在 Java 中表示注释的开始。
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多