JSF注释不适用于Spring-boot
我曾尝试使用 Spring Boot和JSF/Primefaces/Richfaces ,但是对我来说,它是行不通的.
I had been tried to use info from Spring Boot and JSF/Primefaces/Richfaces, but for me it doesn't work.
我将Java 8,maven,Spring-boot和JSF与PrimeFaces结合使用.
我想要一个可执行jar并通过main方法或从命令行java -jar myApp.jar
运行我的应用程序.
I use Java 8, maven, Spring-boot and JSF with PrimeFaces.
I would like to have executable jar and run my application via main method or from command line java -jar myApp.jar
.
问题-JSF注释(@ManagedBean
,@ManagedProperty
)被忽略.
The problem - JSF-annotations (@ManagedBean
, @ManagedProperty
) are ignored.
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
I also have tried to add/remove javax.el-api/javax.el/jstl - the same result. For bean initialization I have added section to faces-config.xml
当我将 spring-boot-starter-web 更改为 spring-boot-starter 并具有 spring-web 时(根据解决方案来自提到了Herick的帖子)我得到了
When I change spring-boot-starter-web to spring-boot-starter and have spring-web (according to solution from mentioned post from Herick) I got
java.io.FileNotFoundException:类路径资源 [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] 无法打开,因为它不存在
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的配置不起作用.
在提到的帖子中是:
With (exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class}
) web.xml configuration doesn't work.
In mentioned post was:
@Bean
public ListenerRegistationBean jsfConfigureListener() {
return new ListenerRegistrationBean(new ConfigureListener());
}
ListenerRegistationBean
在我的弹簧靴中不存在,而我改用了ServletListenerRegistrationBean
.
ListenerRegistationBean
is absent in my spring-boot and I have used ServletListenerRegistrationBean
instead.
我的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:
And 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的工作方式.
Because nonworking annotations is used. By the way PrimeFaces is working.
我的目的是强制JSF注释起作用,因为在没有它们的真实项目中是不可能的.
My purpose is force JSF-annotation to work, because in real project without them it is impossible.
免责声明
即使我的答案与问题标题不匹配,我也会根据我认为您要达到的目标进行回答.
Disclaimer
I am answering this based on what I think you were trying to acheive even though my answer does not match the question title.
您说我的目的是强制JSF注释起作用,因为在没有它们的真实项目中这是不可能的."我猜你的意思是不可能的",因为将受管bean放在faces-config.xml中很麻烦.因此,为此,我将不使用faces-config.xml来管理bean.
You said "My purpose is force JSF-annotation to work, because in real project without them it is impossible." I'm guessing you mean "impossible" because putting managed beans in the faces-config.xml is cumbersome. So to this end I am going to not use the faces-config.xml to manage beans.
我将向您展示一个使用Spring注释的替代方法,该方法非常简单,并且可以实现您的原始目标.
I'm going to show you an alternative that uses Spring annotations which is very non-cumbersome and I feel accomplishes your original goal.
示例- https://github.com/Zergleb/Spring-Boot-JSF-Example
前几天,我查看了您的问题,并决定尝试进行这项工作,然后将结果放在github(上面的链接)上.这个示例应该允许您使用Spring注释而不是JSF注释编写JSF应用程序,例如您将说
I looked over your question the other day and decided to try and make this work and I put my results on github (Link above). This example should allow you to write a JSF application using Spring annotations instead of JSF annotations for example you'll say
@Component
@Scope("view")
//The example above contains an implementation of the View Scope in Spring.
代替
@ManagedBean
@ViewScope
然后您将可以使用Spring进行所有依赖注入.
and you'll then be able to use Spring for all of your dependency injection.
我使用gradle而不是maven,这意味着您的依赖项位于build.gradle中,而不是pom.xml中,我必须添加它们才能使所有工作正常进行.这些应该足够容易以转换为我想象的pom.xml.
I used gradle instead of maven so this means your dependencies are in the build.gradle instead of the pom.xml I had to add these in order to make everything work. Those should be easy enough to translate to a pom.xml I imagine.
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映射以及web.xml的所有其他属性
My web.xml only has one servlet now and I removed the servlet-mapping and all of the other attributes of the web.xml
(我仍在研究如何删除此web.xml,一并检查示例是否有任何更新)
(I'm still working on how to remove this web.xml altogether check the example for any updates on whether I figured it out or not)
<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.xml now has no managed beans
<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示例中包含了此代码
I do not have this right now but we might want to consider having an empty in the web.xml I haven't researched this a ton but one of the spring-project examples on github contains this code
我希望能回答您的问题.如果我遗漏了一些内容,请尝试参考示例代码.
I hope that answers your question. If I left something out try and reference the example code.
https://github.com/Zergleb/Spring-Boot-JSF-Example
运行一个Spring Boot应用程序,该应用程序应在一个共享公共上下文的应用程序中同时运行Spring MVC和JSF.(我将其包含在答案中,因为您在问题
Runs a spring boot application that should both run Spring MVC and JSF in one application sharing a common context.(I included this in the answer because you referenced this link in your question Spring Boot and JSF/Primefaces/Richfaces which says that mixing Spring MVC and JSF is impossible but I have working in my example code.