2017-05-23 103 views
0

注解豆虽然从MyFaces的1.1到MyFaces的2.2.12升级旧的Web应用程序的JSF版本,我想与@ManagedBean注释直接在bean替换我faces-config.xml文件<managed-bean>项类。我正在使用Migrating from JSF 1.2 to JSF 2.0作为迁移的一般指南。无法找到@ManagedBean

例如,我与

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean 
@SessionScoped 
public class MyBean { 

更换像

<managed-bean> 
    <managed-bean-name>MyBean</managed-bean-name> 
    <managed-bean-class>some.package.MyBean</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
</managed-bean> 

Are there going to be two instances for a bean if I write @managed bean annotation and define same in faces-config.xml,注释由faces-config.xml相应的条目覆盖。因此,我在faces-config.xml删除<managed-bean>元素。

由于项目包括其部署为一个组合战文件之前分别被包装成瓶的几个Maven的模块,我也试图按照从How does JSF find beans annotated with @ManagedBean?的意见,并添加含有faces-config.xml子模块另一META-INF文件夹包含豆用下面的内容

MainProject 
| SubModule 
|  |src 
|  | main 
|   | resources 
|    | META-INF 
|    | faces-config.xml 

:,在以下位置(尊重接受的答案中How to reference JSF managed beans which are provided in a JAR file?

<faces-config 
     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-facesconfig_2_2.xsd" 
     version="2.2"> 
</faces-config> 
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'MyBean' resolved to null 

由于我使用JSF的豆管理,我也跟着指令的JSF部分Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable,但即使经过:可悲的是,我试图打开使用此特定bean的页面时仍得到以下错误验证那里提到的个别点,我仍然得到相同的错误。

我使用Tomcat 7作为servlet容器,根据http://tomcat.apache.org/whichversion.html,它支持servlet规范到3.0版本,这反过来应该足够用于JSF 2.2,如果我理解正确的话,它至少需要servlet规范2.5,根据http://myfaces.apache.org/core22/

我已经搜索了很多问题的原因(如上所述,我尝试了几个上面提到的SO文章),但仍然无法解决问题。我会非常感谢任何帮助!

+1

小建议:对于JSF 2.3,不赞成使用CDI'@ Named'的'@ ManagedBean'注解。现在不是正确的时间来切换到直接而不是@ManagedBean? – Kukeltje

+0

@Kukeltje感谢您的建议。我考虑升级到CDI和'@ Named'注释,但遇到了许多其他问题。但是,也许你是对的,我应该尝试解决这些问题,而不是去做一些即将被弃用的工作。 – scholt

回答

0

答案可以在@ManagedBean Javadoc中找到:

The value of the ManagedBean.name attribute is taken to be the managed-bean-name . If the value of the name attribute is unspecified or is the empty String , the managed-bean-name is derived from taking the unqualified class name portion of the fully qualified class name and converting the first character to lower case. For example, if the ManagedBean annotation is on a class with the fully qualified class name com.foo.Bean , and there is no name attribute on the annotation, the managed-bean-name is taken to be bean . The fully qualified class name of the class to which this annotation is attached is taken to be the managed-bean-class .

所以你的bean被命名为myBean,而不是MyBean。如果您希望它是MyBean,请提供带注释的名称:@ManagedBean(name = "MyBean")

+0

非常感谢您的回答!不幸的是,我已经试图通过用'(name =“MyBean”)设置名称来解决这个问题(并且只是再次尝试),但我仍然得到相同的错误:'javax.el.PropertyNotFoundException:Target Unreachable ,标识符'MyBean'解析为空' – scholt

+0

我在这里变得疯狂。我曾尝试将META-INF文件夹和包含的“faces-config.xml”放入每个可以想象的位置,但错误仍然相同。 – scholt