gpt4 book ai didi

spring - JSF 注释不适用于 Spring-boot

转载 作者:行者123 更新时间:2023-12-03 01:04:04 25 4
gpt4 key购买 nike

我曾尝试使用 Spring Boot and JSF/Primefaces/Richfaces 中的信息,但对我来说这不起作用。

我使用 Java 8、maven、Spring-boot 和 JSF 以及 PrimeFaces。我想要一个可执行的 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-starter-web 更改为 spring-boot-starter 并拥有 spring-web (根据解决方案)赫里克提到的帖子)我得到了

java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] cannot be opened because it does not exist

我的配置类:

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

ListenerRegistationBean 在我的 spring-boot 中不存在,我已使用 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 注释的替代方案,它非常不麻烦,而且我觉得可以实现您最初的目标。

回答

示例 -- 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 进行所有依赖项注入(inject)。

我使用 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

<!-- 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 应用程序,该应用程序应该在一个共享公共(public)上下文的应用程序中运行 Spring MVC 和 JSF。(我将其包含在答案中,因为您在问题 Spring Boot and JSF/Primefaces/Richfaces 中引用了此链接,其中表示混合 Spring MVC 和 JSF不可能,但我已经在我的示例代码中工作了。

关于spring - JSF 注释不适用于 Spring-boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24671624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com