【发布时间】:2018-02-21 14:03:25
【问题描述】:
环境:
雄猫 8
Spring Boot 1.5
JSF 2.2
Apache MyFaces
Spring MVC
代码:
我正在 Servlet 3.0 环境中集成 Spring Boot 和 JSF 2.2。
配置类:
JSFConfig.java - JSF 的配置。
@Configuration
@ComponentScan({"com.atul.jsf"})
public class JSFConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
return new ServletRegistrationBean(servlet, "*.jsf");
}
}
Spring Boot 主类:
@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);
}
}
托管 Bean:
UserBean.java - JSF 的托管 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
为NULL。
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
对于 Spring 是不可发现的
5)我在这里遗漏了什么吗? 6)我没有使用 Spring Boot 提供的嵌入式 tomcat
【问题讨论】:
标签: spring spring-boot jsf-2 jsf-2.2