简体   繁体   English

如何让xhtml页面与JSF一起工作?

[英]How to make xhtml page work with JSF?

Anyone knows why my xhtml page does not work with its named bean? 任何人都知道为什么我的xhtml页面不适用于它的命名bean?

I get Welcome #{indexBean.userName} in the browser. 我在浏览器中收到了Welcome #{indexBean.userName} IndexBean is @Named, @SessionScoped and implements Serializable. IndexBean是@ Name,@ SessionScoped并实现Serializable。

index.xhtml 的index.xhtml

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

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

<h:head>
    <h:outputStylesheet library="css" name="default.css"/>
    <title>Admin Panel</title>
</h:head>

<h:body>
<h:form>
    <h2>Welcome #{indexBean.userName}</h2>
</h:form>
</h:body>

</html>

web.xml 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_3_0.xsd" 
        id="WebApp_ID" 
        version="3.0">

<display-name>WebAdmin</display-name>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<servlet>
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>/WEB-INF/faces/index.xhtml</welcome-file>
</welcome-file-list>

</web-app>

Bean 豆角,扁豆

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class IndexBean implements Serializable {
private static final long serialVersionUID = 1L;

MyFaces MyFaces的

INFO: Reading config : jar:file:/C:/tomee16/lib/openwebbeans-el22-1.2.1.jar!/META-INF/faces-config.xml

Dec 15, 2013 10:27:54 PM org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig INFO: Reading config : jar:file:/C:/tomee16/lib/openwebbeans-jsf-1.2.1.jar!/META-INF/faces-config.xml Dec 15, 2013 10:27:54 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact INFO: Artifact 'myfaces-api' was found in version '2.1.13' from path 'file:/C:/tomee16/lib/myfaces-api-2.1.13.jar' Dec 15, 2013 10:27:54 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact INFO: Artifact 'myfaces-impl' was found in version '2.1.13' from path 'file:/C:/tomee16/lib/myfaces-impl-2.1.13.jar' 2013年12月15日下午10:27:54 org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig INFO:读取配置:jar:file:/ C:/tomee16/lib/openwebbeans-jsf-1.2.1.jar!/ META- INF / faces-config.xml 2013年12月15日下午10:27:54 org.apache.myfaces.config.LogMetaInfUtils logArtifact INFO:在路径'文件的版本'2.1.13'中找到了工件'myfaces-api':/ C:/tomee16/lib/myfaces-api-2.1.13.jar'Dec 15,2013 10:27:54 PM org.apache.myfaces.config.LogMetaInfUtils logArtifact INFO:在版本'中找到了工件'myfaces-impl' 2.1.13'from path'文件:/ C:/tomee16/lib/myfaces-impl-2.1.13.jar'

XHTML files are only treated as JSF views if you access them via the FacesServlet . 如果您通过FacesServlet访问XHTML文件,则它们仅被视为JSF视图。 Your FacesServlet is mapped to: 您的FacesServlet映射到:

<url-pattern>/faces/*</url-pattern>

So for a file foo/bar.xhtml your would have to access it via the URL http://host/app/faces/foo/bar.xhtml . 因此,对于文件foo/bar.xhtml您必须通过URL http://host/app/faces/foo/bar.xhtml访问它。

Consider changing the mapping to: 考虑将映射更改为:

<url-pattern>*.xhtml</url-pattern>

Assumes all XHTML files in the app are JSF views. 假设应用程序中的所有XHTML文件都是JSF视图。

First check the package for the annotations: javax.inject.Named and javax.enterprise.context.SessionScoped. 首先检查包的注释:javax.inject.Named和javax.enterprise.context.SessionScoped。 Second If you'r using JEE 6 and because you're using CDI, you need to activate the CDI adding the file WEB-INF/beans.xml, this file can be empty. 第二,如果您使用的是JEE 6并且因为您正在使用CDI,则需要激活CDI添加文件WEB-INF / beans.xml,此文件可以为空。

Probably you are calling your page without the FacesServlet handling it. 可能你在没有FacesServlet处理它的情况下调用你的页面。

You should include /faces/ before your page name url 您应该在页面名称url之前包含/ faces /

localhost:8080/WebAdmin/faces/index.xhtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM