1

我有一个与JSF 2相关的问题。我正在使用带有Tomcat 6服务器的 2.1.4 版本。我正在迁移最初为 jsf 1.2 编写的应用程序。据此,应用程序的每个 JSF 托管 bean 都在 faces-managed-beans.xml 文件中声明。当我尝试使用新的 JSF 2 @ManagedProperty注释设置属性时,问题就出现了。例如,这段代码对我有用。

<managed-bean>
    <managed-bean-name>navegableSessionParamsManager</managed-bean-name>
    <managed-bean-class>com.tesicnor.system.view.beans.navegable.NavegableSessionParamsManager</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>

    <managed-property>
        <property-name>_LoggedBean</property-name>
        <value>#{loggedBean}</value>
    </managed-property>
    <managed-property>
        <property-name>_LocaleBean</property-name>
        <value>#{localeBean}</value>
    </managed-property>

</managed-bean>

会话范围的 navegableSessionParamsManager bean 正在接收 loggingBean 和 localeBean 值,它们也是会话范围的 bean。但是,如果我使用注释,我声明了三个@ManagedBean@SessionScoped,并且我使用了@ManagedProperty("#{loggedBean}")@ManagedProperty("#{localeBean}"),但是没有设置属性。

LoggedBean 类。

@ManagedBean
@SessionScoped
public class LoggedBean extends BaseBean implements Observer {

 /**
 * 
 */
private static final long serialVersionUID = 1L; 
...

NavegableSessionParamsManager 类

@SuppressWarnings("serial")
@ManagedBean
@SessionScoped
public class NavegableSessionParamsManager extends SystemNavegable {

@ManagedProperty("#{loggedBean}")
private LoggedBean _LoggedBean;

public void set_LoggedBean(LoggedBean _LoggedBean) {
    this._LoggedBean = _LoggedBean;
}
...

那是我的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>System_V4</display-name>
<context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml, /WEB-INF/faces-managed-beans.xml, /WEB-INF/faces-navigation.xml</param-value>
</context-param>
<context-param>
    <description>Do not render comments in facelets (xhtml) pages. Default is false.</description>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <description>
        Define the value returned by Application.getProjectStage(). Allowed values: Production, Development,
        UnitTest, SystemTest, Extension. Default value is Production.
    </description>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>pepper-grinder</param-value>
</context-param>

<!-- filter enforcing charset UTF-8 - must be first filter in the chain! -->
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


<!-- Advanced Security Filter -->
<!-- FILTRO PROPIO. -->
<filter>
    <filter-name>SystemFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>SystemFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>SystemUrlFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>SystemUrlFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Filter to set character encoding on each request "Set Character Encoding" 
    for "X-UA-Compatible" de microsoft -->
<filter>
    <filter-name>SetHttpHeadersFilter</filter-name>
    <filter-class>com.tesicnor.system.view.filters.SetHttpHeadersFilter</filter-class>
    <init-param>
        <param-name>X-UA-Compatible</param-name>
        <param-value>IE=EmulateIE7</param-value>
    </init-param>
</filter>
<!-- Primefaces -->
<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SetHttpHeadersFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
    <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>com.tesicnor.system.view.session.WebSessionListener</listener-class>
</listener>
<listener>
    <listener-class>com.tesicnor.system.view.session.WebRequestListener</listener-class>
</listener>

<!-- FIN JavaServer Faces Faces Servlet -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<!-- SERVLET DE CXF (Web Services) -->
<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!-- FIN Faces Servlet Faces 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>CXFServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>60</session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<error-page>
    <error-code>404</error-code>
    <location>/404.xhtml</location>
</error-page>

我不知道在 JSF 2 中使用注释有什么样的优势,但每个人都在这样做。另外,如果我对同一个 bean 使用注释和 xml bean 映射,我会遇到麻烦吗?

4

3 回答 3

1

确保您的三个faces-config.xml文件是 2.0 或更高版本。另请参阅JSR 314规范中的注释类扫描要求一章。

于 2012-12-13T11:23:45.230 回答
0

此答案基于您仍然在 faces-config.xml 中定义 bean 的假设。

您提到您已迁移到 JSF2。那么,您是否仍然NavegableSessionParamsManager像在您的 OP 中一样在 faces-config.xml 中定义了 bean。如果您 NavegableSessionParamsManager在您的faces-config.xml和同一个中定义,则使用服务器进行注释@ManagedProperty将首先实例化 faces-config.xml 中定义的任何内容。

原因xml文件将在 servlet 容器启动时优先,在这种情况下,Java 代码中的任何内容都将被简单地忽略。

如果您在您使用的任何注释中注释掉代码faces-config.xml将正常工作。也许您必须这样做。

 <--- <managed-bean>
        <managed-bean-name>navegableSessionParamsManager</managed-bean-name>
        <managed-bean-class>com.tesicnor.system.view.beans.navegable.NavegableSessionParamsManager</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>

        <managed-property>
            <property-name>_LoggedBean</property-name>
            <value>#{loggedBean}</value>
        </managed-property>
        <managed-property>
            <property-name>_LocaleBean</property-name>
            <value>#{localeBean}</value>
        </managed-property>

    </managed-bean>---->

现在尝试使用注释。

于 2012-12-15T08:08:28.303 回答
0

终于过了一会儿,我解决了它在我的 web.xml 中添加 * .faces和 * .xhmtml servlet-mappings,这在以前不需要。

接着就,随即:

<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>*.xhtml</url-pattern>
</servlet-mapping>

现在注释正在工作。

于 2012-12-21T08:19:54.027 回答