环境:
Tomcat 8
弹簧启动1.5
JSF 2.2
Apache MyFaces
Spring
代码:
我正在Servlet3.0环境中集成Spring和JSF2.2。
配置类:
JSFConfig.java -用于JSFConfig.java。
@Configuration
@ComponentScan({"com.atul.jsf"})
public class JSFConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
return new ServletRegistrationBean(servlet, "*.jsf");
}
}
弹簧启动主级:
@SpringBootApplication
@Import({ // @formatter:off
JPAConfig.class,
ServiceConfig.class, // this contains UserServiceImpl.java class.
WebConfig.class,
JSFConfig.class,
})
public class SpringbootJpaApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(SpringbootJpaApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootJpaApplication.class);
}
}
管理豆:
用于JSF的UserBean.java管理Bean
@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
@ManagedProperty(value="#{userServiceImpl}")
private UserServiceImpl userServiceImpl;
public void addUser(){
System.out.println("User Gets added "+this.name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public UserServiceImpl getUserServiceImpl() {
return userServiceImpl;
}
public void setUserServiceImpl(UserServiceImpl userServiceImpl) {
this.userServiceImpl = userServiceImpl;
}
}
实况调查:
home.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h2>JSF 2.0 Hello World Example - hello.xhtml</h2>
<h:form>
<h:inputText value="#{userBean.name}"></h:inputText>
<h:commandButton value="Submit" action="#{userBean.addUser}"></h:commandButton>
</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>
<lifecycle>
<phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener>
</lifecycle>
</faces-config>
发行:
1)当我在home.xhtml , userBean.addUser
中提交表单时,调用。2)使用用户输入的值设置userBean.name
。3)但userServiceImpl
为空。4)这是否意味着Spring和JSF没有得到集成?我还注册了SpringBeanFacesELResolver
,如
faces-config.xml
我还尝试从UserBean.java中删除所有JSF特定的注释,并且只使用Spring特定的注释,如下所示-
@Component
@SessionScoped
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
@Autowired
private UserServiceImpl userServiceImpl;
}
但是,当我提交表单时,我得到的是#{userBean)
无法到达的目标错误。这意味着userBean
在春季是不可发现的
(5)我在这里遗漏了什么吗?6)我没有使用带有Spring的嵌入式tomcat
发布于 2017-09-13 06:56:48
这就是JSF使用Spring (完全使用JSF2.3和Spring 2更新Github的示例项目)的方式:
1.依赖关系
除了标准的web启动依赖之外,您还需要包括标记为提供的tomcat嵌入式jasper (感谢@Fencer在这里中进行评论)。否则,您将在应用程序启动时得到一个异常,因为JSF依赖于JSP处理器(请参见我的答案末尾的第一个链接)。
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. Servlet注册
注册JSF并将其配置为在启动时加载(不需要web.xml)。如果使用JSF2.2,最好是使用*.xhtml
映射,至少在使用facelets时是这样的:
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new FacesServlet(), "*.xhtml");
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}
使您的配置类实现ServletContextAware
,以便您可以设置init参数。在这里,您必须强制JSF加载配置:
@Override
public void setServletContext(ServletContext servletContext) {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration",
Boolean.TRUE.toString());
servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true");
//More parameters...
}
3. EL集成
在Facs-config.xml中声明EL解析器。这将成为视图文件与托管bean属性和方法之间的粘合剂:
<?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>
4.视图范围
编写一个自定义Spring作用域来模拟JSF视图作用域(请记住您的bean将由Spring管理,而不是JSF)。它看起来应该是这样的:
public class ViewScope implements Scope {
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String, Object> viewMap = FacesContext.getCurrentInstance()
.getViewRoot().getViewMap();
if (viewMap.containsKey(name)) {
return viewMap.get(name);
} else {
Object object = objectFactory.getObject();
viewMap.put(name, object);
return object;
}
}
@Override
public String getConversationId() {
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
}
@Override
public Object remove(String name) {
return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
}
@Override
public Object resolveContextualObject(String arg0) {
return null;
}
}
并在配置类中注册它:
@Bean
public static CustomScopeConfigurer viewScope() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.setScopes(
new ImmutableMap.Builder<String, Object>().put("view", new ViewScope()).build());
return configurer;
}
5.准备出发!
现在,您可以按照下面的方式声明托管bean。记住使用@Autowired
(最好是在构造函数中)而不是使用@ManagedProperty
,因为您正在处理Spring。
@Component
@Scope("view")
public class MyBean {
//Ready to go!
}
仍在等待实现
我无法让JSF特定的注释在Spring上下文中工作。因此不能使用@FacesValidator
、@FacesConverter
、@FacesComponent
等。尽管如此,还是有机会在faces-config.xml (参见柯萨德)中声明它们,这是一种老式的方法。
还请参见:
https://stackoverflow.com/questions/46187725
复制相似问题