環境:春季啓動JSF集成
Tomcat的8
春季啓動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");
}
}
春季啓動主類:
@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 - 託管Bean對JSF
@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;
}
}
的Facelets:
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
春季不可發現
5)我在這裏丟失了什麼? 6)我不使用Spring提供的引導
的Xtreme騎自行車的人,你找到一個辦法讓JSF註釋工作?也許與春季啓動2.0? –
不,但自從我寫這個答案以來,我沒有太多挖掘。因爲Spring在這裏管理你的bean,所以你可以嘗試一些生成單例的註釋,就像@Component一樣。 –