0

我有一个小的 JSF 2.2 webapp。现在我需要一个自定义组件,它只是在页面上显示以下文本:

由 Nazar Sobchuk 创建 - 2013 年 12 月 7 日(当前日期)

我有以下组件类:

package com.dataart.mediaportal.components;

import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

@FacesComponent(value = "components.FooterComponent", createTag = true)
public class FooterComponent extends UIComponentBase {

    @Override
    public String getFamily() {
        return "my.custom.component";
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String date = dateFormat.format(new java.util.Date());

            ResponseWriter writer = context.getResponseWriter();
            writer.write("Created by Nazar Sobchuk - 2013");
            writer.write(date);
    }
}

我应该如何在我的 JSF 页面上使用这个组件?为了使用组件,应该包含哪些 xmlns url?

如果需要,这里是 web.xml 和 faces-config.xml 文件:

Web.xml:

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

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

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

    <!--        
        responsible for handling multipart/form-data requests which 
        is required to be able to send files over HTTP.
        <servlet-name> must match the exact <servlet-name> 
        of the FacesServlet as you've definied in web.xml.
    -->
    <filter>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>

    <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>

    <servlet>
        <servlet-name>ImgLoaderServlet</servlet-name>
        <servlet-class>com.dataart.mediaportal.servlet.ImgLoaderServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ImgLoaderServlet</servlet-name>
        <url-pattern>/load</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>pages/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

面孔-config.xml:

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

<faces-config version="2.2"
              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_2.xsd">

    <navigation-rule>
        <from-view-id>/pages/login.xhtml</from-view-id>

        <navigation-case>
            <from-outcome>home</from-outcome>
            <to-view-id>/pages/home.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

        <navigation-case>
            <from-outcome>register</from-outcome>
            <to-view-id>/pages/registration.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

    </navigation-rule>

    <navigation-rule>
        <from-view-id>/pages/registration.xhtml</from-view-id>

        <navigation-case>
            <from-outcome>home</from-outcome>
            <to-view-id>/pages/home.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

        <navigation-case>
            <from-outcome>registration</from-outcome>
            <to-view-id>/pages/registration.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

    </navigation-rule>

    <navigation-rule>
        <from-view-id>/pages/index.xhtml</from-view-id>

        <navigation-case>
            <from-outcome>go</from-outcome>
            <to-view-id>/pages/login.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

    </navigation-rule>

    <navigation-rule>
        <from-view-id>/pages/home.xhtml</from-view-id>

        <navigation-case>
            <from-outcome>login</from-outcome>
            <to-view-id>/pages/login.xhtml</to-view-id>
            <redirect/>
        </navigation-case>

    </navigation-rule>

    <application>

        <message-bundle>
            AppErrorMessages
        </message-bundle>

        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>ru</supported-locale>
        </locale-config>

        <resource-bundle>
            <base-name>i18n.MediaPortal</base-name>
            <var>bundle</var>
        </resource-bundle>

    </application>

</faces-config>

我会永远感谢这个解决方案。纳扎尔

4

0 回答 0