我有一个与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 映射,我会遇到麻烦吗?