spring - JSF 2 托管 bean 及其在 EL 表达式中使用时的方法命名约定(与 Spring 结合使用)

标签 spring jsf-2 naming-conventions javabeans

据我所知,JSF EL 中使用了用于转换属性/类名的标准 JavaBeans 约定,即方法对 (get|is)Property + setProperty 被转换为 property :

public class SomeComponent {
    public String getAttribute() { ... }
    public void setAttribute(String attribute) { ... }
}

<h:outputText value="#{comp.attribute}" />

bean 名称也是如此(除非这些名称直接在 @ManagedBean 注释中指定):

@ManagedBean
public class UsefulBean {
    ...
}

<h:outputText value="#{usefulBean.doSomething()}" />

这种行为大部分都有记录。但是,当 bean 或属性名称以首字母缩略词开头时,我无法找到有关转换约定的任何内容,例如

@ManagedBean
public class URLManagerBean {
}

<h:outputText value="#{...ManagerBean.doSomething()}" />

前面的代码片段中应该用什么来代替省略号?

令人惊讶的是,但互联网上几乎没有这方面的信息。几乎所有 JSF 托管 Bean 使用示例都使用开头没有多个相邻大写字母的 Bean 名称。在我设法找到的几个地方,建议不要以任何方式转换以多个大写字母开头的 bean 名称,即前面的代码片段中的 URLManagerBean

不幸的是,我们有一个项目,其中很多 bean 都是这样命名的。在使用这些 bean 的任何地方,它们至少都遵循这种奇怪的约定:

class URLManagerBean -> uRLManagerBean

也就是说,只有首字母缩略词的第一个字母被取消大写。该项目似乎运行良好,因此该约定在某种程度上有效。但 IntelliJ IDEA 不喜欢这样;它认为 JSF bean 确实应该像 URLManagerBean 那样命名,除非我通过注释显式重命名它。

我尝试将 class URLManagerBean 的使用从 uRLManagerBean 更改为 URLManagerBean,但显然这不起作用 - 我尝试的那些页面停止了工作。

现在我认为这种奇怪约定背后的原因是我们通过 SpringBeanFacesELResolver 使用 Spring 集成。但是,这种行为没有在任何地方记录。

对于这种行为有什么解释吗?

最佳答案

您使用的是哪个@ManagedBean注释?来自 javax.faces.beanjavax.annotation 的那个?

在第一种情况下,Spring 与它无关,因为它将是一个 JSF 托管 bean,并且将遵循 JSF 指定的约定。

The value of the 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.example.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. Source: @ManagedBean javadocs

在第二种情况下,Spring 处于混合状态,然后行为会根据您使用的 spring 版本和/或配置选项而有所不同。使用 AnnotationConfigApplicationContext 时,会使用遵循准则的 AnnotationBeanNameGenerator(并且会生成“URLManagerBean”作为 bean 名称)。使用其他方法,您最终可能会得到 DefaultBeanNameGenerator,它具有更简单的算法。

简单地说,这可能取决于您使用的 spring 版本和注释。

链接

关于spring - JSF 2 托管 bean 及其在 EL 表达式中使用时的方法命名约定(与 Spring 结合使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692667/

相关文章:

java - 通用 Spring Data JPA 存储库 findAll

java - 访问mybatis的Mapper文件中的变量值

java - Hibernate join 工作时间太长

c - 为什么星号在变量名之前,而不是在类型之后?

按钮/标签等的 iOS 命名约定

spring - 如何使用 JSTL 获取 jsp 中列表的元素?

java - spring-data-jpa 1.9.4 升级到 1.10.2 后出现问题

java - 使用maven使jar成为jsf中的自定义组件

javascript - 如何创建 p :tabView with progressbar

C# 检查或强制命名约定