当前位置: 首页 > 知识库问答 >
问题:

JSF注释不能与Spring-boot一起使用

萧元徽
2023-03-14

我曾尝试使用来自Spring Boot和JSF/PrimeFaces/RichFaces的信息,但对我来说这不起作用。

我使用Java8、maven、Spring-boot和JSF和PrimeFaces。我希望有可执行的jar,并通过main方法或从命令行java-jar myapp.jar运行我的应用程序。

问题-JSF注释(@managedbean@managedproperty)被忽略。

<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中添加了section

当我将spring-boot-starter-webs改为spring-boot-starter并拥有spring-webs(根据Herick提到的帖子中的解决方案)时,我得到了

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());
      }

}

使用(exclude={webmvCautoConfiguration.class,DispatcherServleTautoConfiguration.class})web.xml配置不起作用。所提到的帖子是:

@Bean
public ListenerRegistationBean jsfConfigureListener() {
    return new ListenerRegistrationBean(new ConfigureListener());           
}     
<?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>
<?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>

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

共有1个答案

农波涛
2023-03-14

我的回答是基于我认为你试图获得的,即使我的回答不符合问题的标题。

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

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

@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-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-project示例中有一个包含这段代码

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-tradition/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>

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

https://github.com/zergleb/spring-boot-jsf-example

运行一个spring boot应用程序,该应用程序应该在共享公共上下文的一个应用程序中同时运行spring MVC和JSF。(我在答案中包含了这一点,因为您在spring boot和JSF/PrimeFaces/RichFaces问题中引用了这个链接,它说混合spring MVC和JSF是不可能的,但我已经在示例代码中工作了。

 类似资料:
  • 问题内容: 我的应用程序使用Spring自动装配来配置Bean。我刚刚尝试添加@Transactional,预期的代理似乎没有被调用。我希望PersonalController用事务代理包装的UpdatePublicMapService调用UpdatePublicMapService。 我看到的是PersonalController实例化了两次。第一次获得代理,但是第二次获得未代理的目标。我究竟做

  • 我尝试使用SmtpAuthenticator创建邮件服务。组件已正确启动,但用户名和密码字段中存在空值。为什么会这样?

  • 我试图让Ehcache 3在不使用Spring Boot的情况下与Spring 4一起工作。 这里有一个使用Spring Boot的工作示例,但是我正在处理一个不使用Spring Boot的现有应用程序。 问题在于,sping-encent-support(它添加了Spring的缓存注释)期望Ehache的CacheManager位于这个类路径上:net.sf.ehcache.CacheManag

  • 我使用AWS加密客户端。下面的代码抛出错误为 错误:@DoNotTouch不适用于现场 然而,如果我给出@DoNotTouch配置,如下所示[使用getter和setter] 这很好用。我猜lombok生成的getter和setter不会被AmazoneCryptionClient识别 我在跟踪这个aws doc:https://aws.amazon.com/blogs/developer/cli

  • 是否可以通过注释在Spring Cloud Circuit Breaker上使用Resilience4j?我找不到任何关于它的留档,只有关于通过代码使用弹性4j的示例

  • 更新:Oook,首先,非常感谢。我不知道用户是postgres中的保留关键字。我把名字改成了CustomUser,但现在问题是另外一个了,应用程序可以工作,但我注意到它创建了一个名为custom_user的相同的CustomUser表,因为它没有使用现有的表? 我刚开始使用Springboot,我不明白我错在哪里。这是我的模型: 希望在您的帮助下,非常感谢大家。