JSF 注释不适用于 Spring-boot

2024-01-03

我曾试图使用来自Spring Boot 和 JSF/Primefaces/Richfaces https://stackoverflow.com/questions/22544214/spring-boot-and-jsf-primefaces-richfaces,但对我来说这不起作用。

I useJava 8、maven、Spring-boot 和带有 PrimeFaces 的 JSF。 我想要一个可执行的 jar 并通过 main 方法或从命令行运行我的应用程序java -jar myApp.jar.

问题- JSF 注释(@ManagedBean, @ManagedProperty)被忽略。

Pom 文件:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.3.RELEASE</version>
</parent>

<dependencies>
   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>7.0.54</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>7.0.54</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>7.0.54</version>
    </dependency>

    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.0</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.7</version>
    </dependency>

    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
    ...
</dependencies>

我还尝试添加/删除 javax.el-api/javax.el/jstl - 相同的结果。对于 bean 初始化,我已将部分添加到 faces-config.xml

当我改变时spring-boot-启动器-web to 弹簧启动启动器并有弹簧网(根据赫里克提到的帖子的解决方案)我得到了

java.io.FileNotFoundException:类路径资源 [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] 无法打开,因为它不存在

我的配置类:

@Configuration
@EnableAutoConfiguration//(exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class})
@ComponentScan("hello")
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    }

    @Bean
    public FacesServlet facesServlet() {
        return new FacesServlet();
    }

    @Bean
    public ServletRegistrationBean facesServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(facesServlet(), "*.xhtml");
        registration.setName("facesServlet");
        return registration;
    }

    @Bean
      public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener()         {
          return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
      }

}

With (exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class}) web.xml 配置不起作用。 在提到的帖子中是:

@Bean
public ListenerRegistationBean jsfConfigureListener() {
    return new ListenerRegistrationBean(new ConfigureListener());           
}     

ListenerRegistationBean我的弹簧靴中不存在,我已经使用过ServletListenerRegistrationBean反而。

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_1.xsd"     
    version="3.1">
    <display-name>Test</display-name>
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <error-page>
        <location>/error.xhtml</location>
    </error-page>
</web-app>

和 faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

    <managed-bean>
      <managed-bean-name>managedBeann</managed-bean-name>
      <managed-bean-class>hello.ManagedBeann</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>

因为使用了非工作注释。 顺便说一句,PrimeFaces 正在工作。

我的目的是强制 JSF 注释工作,因为在实际项目中没有它们是不可能的。


免责声明

我是根据我认为您想要实现的目标来回答这个问题的,尽管我的答案与问题标题不符。

您说“我的目的是强制 JSF 注释工作,因为在实际项目中没有它们这是不可能的。”我猜你的意思是“不可能”,因为将托管 bean 放入 faces-config.xml 中很麻烦。因此,为此我将不使用 faces-config.xml 来管理 bean。

我将向您展示一种使用 Spring 注释的替代方案,它非常不麻烦,而且我觉得可以实现您最初的目标。

Answer

例子 -https://github.com/Zergleb/Spring-Boot-JSF-Example https://github.com/Zergleb/Spring-Boot-JSF-Example

前几天我查看了你的问题,并决定尝试让这项工作成功,并将我的结果放在 github 上(上面的链接)。这个示例应该允许您使用 Spring 注释而不是 JSF 注释来编写 JSF 应用程序,例如您会说

@Component
@Scope("view")
//The example above contains an implementation of the View Scope in Spring.

代替

@ManagedBean
@ViewScope

然后您就可以使用 Spring 进行所有依赖项注入。

我使用 gradle 而不是 maven,所以这意味着您的依赖项位于 build.gradle 而不是 pom.xml 中,我必须添加这些依赖项才能使一切正常工作。我想这些应该很容易翻译成 pom.xml 。

compile group: 'javax.el', name: 'el-api', version: '1.0'
compile group: 'com.sun.el', name: 'el-ri', version: '1.0'
compile group: "javax.servlet.jsp" name: "jsp-api" version: "2.1"

我的 web.xml 现在只有一个 servlet,我删除了 servlet-mapping 和 web.xml 的所有其他属性

(我仍在研究如何完全删除此 web.xml,请检查示例以了解我是否发现它的任何更新)

<web-app ... same as before>
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
</web-app>

faces-config.xml 现在没有托管 bean

<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">
     <application>
         <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
     </application>
</faces-config>

我现在没有这个,但我们可能想考虑在 web.xml 中留一个空,我没有对此进行大量研究,但 github 上的 spring 项目示例之一包含此代码

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/src/main/webapp/WEB-INF/web.xml https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/src/main/webapp/WEB-INF/web.xml

<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>

我希望这能回答你的问题。如果我遗漏了一些内容,请尝试参考示例代码。

Example

https://github.com/Zergleb/Spring-Boot-JSF-Example https://github.com/Zergleb/Spring-Boot-JSF-Example

运行一个 spring boot 应用程序,该应用程序应该在一个共享公共上下文的应用程序中运行 Spring MVC 和 JSF。(我将其包含在答案中,因为您在问题中引用了此链接Spring Boot 和 JSF/Primefaces/Richfaces https://stackoverflow.com/questions/22544214/spring-boot-and-jsf-primefaces-richfaces这表明混合 Spring MVC 和 JSF 是不可能的,但我已经在我的示例代码中工作了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSF 注释不适用于 Spring-boot 的相关文章

随机推荐

  • 以编程方式添加 SwitchCompat 错误

    我正在尝试添加android support v7 widget SwitchCompat以编程方式 我收到此错误 java lang NullPointerException Attempt to invoke interface met
  • 检查用户输入是否在txt文件中,使用批处理

    我正在为局域网联网计算机批量制作聊天风格的系统 我想检查用户名是否被占用 如果不允许 我如何检查用户在这一行中输入的内容 set p name2 我已经在测试文件中尝试过这个 但无法让它工作 startup set fail set nam
  • findOne 可以工作,但不能获取全部/查找

    findOne 工作正常 db collection updates function err collection collection findOne author req user id function err doc 我正在尝试获
  • getaddrinfo,我没有得到任何 canonname

    我正在尝试读取有关特定主机的所有信息并打印出所有信息 我可以读取并打印出所有地址 但我没有读取任何 ai canonname 首先 我认为我的示例 www google com www irs gov 没有规范名称 但过了一会儿我发现我根本
  • 如何从控制器中包含的模块渲染js模板?

    我在控制器关注点中有一个操作 该操作包含在控制器中 此操作不会呈现在 respond to 块下指定的 js erb 文件 如何正确获取控制器关注点中的操作以成功呈现 js erb 文件 或任何视图 是我的路线有问题吗 模块操作的链接 li
  • String 子字符串在 Swift 中如何工作

    我一直在使用 Swift 3 更新一些旧代码和答案 但是当我使用 Swift 字符串和子字符串索引时 事情变得令人困惑 具体来说 我正在尝试以下操作 let str Hello playground let prefixRange str
  • SVN:创建文件夹的转储文件

    我正在尝试在 SVN 存储库中创建文件夹的转储文件 我的目标是将此转储导入另一个存储库 但那是另一个故事 我读了大概 20 页关于这个的内容 他们都告诉我要使用svndump过滤器 http svnbook red bean com en
  • wkhtmltopdf 每次运行都会生成不同的校验和

    我试图验证从运行到运行时从 wkhtmltopdf 生成的内容是否相同 但是每次运行 wkhtmltopdf 时 我都会针对同一页面获得不同的哈希 校验和值 我们正在谈论一些真正基本的东西 比如使用以下 html 页面 p This is
  • Azure 虚拟机上的 HTTPS

    我正在将我们的网站迁移到 Azure 并在 Windows Server 2012 VM 上运行 我已从 GoDaddy 购买了通配符 SSL 证书 并且已在虚拟机上安装和配置了它 我还配置了 IIS 以利用此证书来处理端口 443 上的所
  • 如何删除数据框中空值数量超过 x 的行? [复制]

    这个问题在这里已经有答案了 我正在尝试删除数据框中具有超过 7 个空值的行 请提出一些有效的方法来实现这一目标 如果我理解正确的话 只有当一行中的 nan 总数超过时 才需要删除行7 df df df isnull sum axis 1 l
  • 使用什么 Java 异常类来处理 HTTP 错误?

    我正在使用阿帕奇Http客户端 http hc apache org httpclient 3 x 并且希望通过 Java 异常机制向调用代码传达 HTTP 错误 400 错误请求 404 未找到 500 服务器错误等 Java 标准库或广
  • WebSockets 版本之间的协议差异是什么?

    是否有任何地方的摘要协议差异各种 WebSockets 草案之间的关系 浏览器支持级别仍然遍布各处 因此仅考虑 RFC 是不够的 显然 Sec WebSocket Version 发生了变化 而且我知道早期的格式非常不同 然而 我指的是协议
  • Angular 9 Google 地图 API 地点自动完成

    我有一个 Angular 9 刚刚从 8 迁移 我需要使用 Google Places API 我有一个 API 密钥 进行地址自动完成 我可以使用 angular googlemaps 库显示地图 但我无法制作自动完成工作 我尝试使用ht
  • 使用 libvlc 将相机流式传输到内存并显示帧

    我正在尝试使用 libvlc 将捕获设备 相机 流式传输到内存中 我无法显示内存中存储的数据的图像 内存内容看起来没问题 我使用 Visual Studio 及其内存窗口检查了它 imshow 创建的图片始终是灰色的 如何显示内存中存储的图
  • jquery按钮不响应click方法

    所以我使用append方法用jquery动态渲染一个段落 我想向它添加一个点击事件 但由于某种原因 点击事件不起作用 我知道解决方案可能很简单 但我是jquery的新手 我会很感激任何帮助 我知道函数内部的代码可以工作 因为我用静态按钮测试
  • 什么是数据对齐?在 C 中类型转换指针时,为什么以及何时应该担心? [复制]

    这个问题在这里已经有答案了 我找不到一个像样的文档来解释对齐系统如何工作以及为什么某些类型比其他类型更严格地对齐 我会尝试简短地解释一下 什么是数据对齐 计算机的体系结构由处理器和内存组成 记忆是按细胞组织的 因此 0x00 data 0x
  • StackExchange 站点如何关联用户帐户和 OpenID 登录信息?

    我喜欢 StackExhchange StackOverflow 集成 OpenID 的方法 据我了解 这个过程是这样的 如果用户向 OpenID 提供商 即 Google 注册 StackOverflow 或类似网站 会为该用户创建一个帐
  • Python 类中的线程

    我最近开始使用python的线程模块 经过一番尝试和错误后 我设法使用大多数教程中给出的以下示例代码来使基本线程工作 class SomeThread threading Thread def init self count threadi
  • 2021年8月18日SDK更改后如何从代码中设置azure实验名称?

    2021 年 8 月 18 日 Microsoft 为了我们的方便 对其 Azure ML SDK 进行了以下更改 https learn microsoft com en us azure machine learning azure m
  • JSF 注释不适用于 Spring-boot

    我曾试图使用来自Spring Boot 和 JSF Primefaces Richfaces https stackoverflow com questions 22544214 spring boot and jsf primefaces