You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSF 2.2页面无法渲染及无法获取JavaBean内容问题求助

Troubleshooting Blank Browser Rendering for JSF 2.x Application

Hey there, let's dig into why your JSF app is showing a blank page across both Tomcat/IntelliJ and GlassFish/NetBeans setups. Based on what you've shared, here are the most likely issues to check:


1. Incomplete or Faulty web.xml Configuration

Your provided web.xml is truncated, and JSF applications rely on a properly configured FacesServlet to process requests. Without this mapping, the server won't recognize JSF pages and will return them as unprocessed static files—resulting in a blank browser.

Here's the standard, required configuration for web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- Core JSF Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map JSF to .xhtml files -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <!-- Optional but helpful: Enable development mode for better error messages -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
</web-app>

2. Missing JSF/CDI Dependencies (Critical for Tomcat)

Tomcat is a pure servlet container—it doesn't include JSF or CDI out of the box. If you're using Tomcat, you need to manually add these libraries:

For Maven projects, add these to your pom.xml:

<dependencies>
    <!-- JSF API -->
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.3</version>
        <scope>provided</scope>
    </dependency>
    <!-- JSF Implementation (Mojarra) -->
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.3.14</version>
    </dependency>
    <!-- CDI Implementation (Weld, required for @Named beans) -->
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet-shaded</artifactId>
        <version>3.1.9.Final</version>
    </dependency>
</dependencies>

Also, add an empty beans.xml in WEB-INF to enable CDI:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       version="2.0" bean-discovery-mode="all">
</beans>

GlassFish includes JSF and CDI by default, but double-check for conflicting dependencies if the issue persists there.


3. Incorrect Facelets (XHTML) Page Structure

You didn't share your XHTML file, but make sure it uses the proper JSF namespace and references your bean correctly. Save this as index.xhtml in your webapp directory:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<head>
    <title>JSF Time Demo</title>
</head>
<body>
    <h1>Current Time: #{timeBean.time}</h1>
</body>
</html>

Also, ensure you're accessing the correct URL—like http://localhost:8080/your-app-name/index.xhtml (note the .xhtml extension, not .html).


4. Check Server Logs for Hidden Errors

Blank pages often mask underlying exceptions. Look at the server logs (IntelliJ/NetBeans console or Tomcat/GlassFish log files) for errors like:

  • ClassNotFoundException for FacesServlet
  • CDI injection failures for your TimeBean
  • Facelets compilation errors

These logs will give you exact clues about what's breaking.


If you fix any of these and still run into issues, sharing your full web.xml and XHTML page would help narrow things down further!

内容的提问来源于stack exchange,提问作者idjevm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:22:44