使用JSFbean的Spring引导
我试着用Spring Boot和JSF创建一个项目。我添加了如下servlet映射:使用JSFbean的Spring引导,spring,jsf,jsf-2,spring-boot,Spring,Jsf,Jsf 2,Spring Boot,我试着用Spring Boot和JSF创建一个项目。我添加了如下servlet映射: @ComponentScan() @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setShowBanner(fals
@ComponentScan()
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setShowBanner(false);
app.run(args);
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new javax.faces.webapp.FacesServlet();
return new ServletRegistrationBean(servlet, "/faces/*");
}
}
这是我的index.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h1>Hello World</h1>
<h:form>
<p><h:outputText value="#{userView.value}"/></p>
</h:form>
</h:body>
</html>
我添加了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/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
但是spring没有管理我的JSFbean,你能帮我吗
谢谢您的JSFbean是JSF管理的bean,而不是Springbean。所以spring永远也不会成功。我会将
FacesServlet
映射到*.xhtml
。您遇到的问题是什么?请将导入添加到用户视图
。最后用@Configuration
注释您的类。您是对的,spring不会管理my bean,但我的问题是my不会被解释,并且我的userView不会被创建(my System.out.println不会被打印)。所以我的JSF页面没有JSF支持bean。这是我导入的userView import javax.annotation.PostConstruct;导入javax.faces.bean.ManagedBean;导入javax.faces.bean.ViewScoped;正如我提到的,尝试将FacesServlet
映射到*.xhtml
而不是/faces/*
。您的xhtml页面必须通过/必须由FacesServlet
呈现。因此,如果您直接访问它们(并且它不在/faces/
下),它将不会被处理,而是按原样打印。而且正如前面提到的,将@Configuration
添加到您的类中,我希望您的FacesServlet
的当前配置将被忽略。
<?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/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>