Spring Boot JSF 一体化

环境 :

Tomcat 8

Spring Boot 1.5

JSF 2.2

Apache MyFaces

Spring MVC

代码 :

我整合了 Spring Boot 和 JSF 2.2 在星期三 Servlet 3.0.

配置类 :

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/;
}
}


管理鲍勃 :

userbean.java-managed bean for 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;
}
}


其他 :

home.xhtml-homepage.


xml version="1.0" encoding="UTF-8"?
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]http://www.w3.org/TR/xhtml1/DT ... ot%3B[/url]
<html xmlns="[url=http://www.w3.org/1999/xhtml"]http://www.w3.org/1999/xhtml"[/url] xmlns:f="[url=http://java.sun.com/jsf/core"]http://java.sun.com/jsf/core"[/url] xmlns:h="[url=http://java.sun.com/jsf/html">]http://java.sun.com/jsf/html">[/url]
<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 action="#{userBean.addUser}" value="Submit"></h:commandbutton>
</h:form>
</h:body>
</html>


person-config.xml. :


xml version="1.0" encoding="UTF-8"?
<faces-config version="2.2" xmlns="[url=http://xmlns.jcp.org/xml/ns/javaee"]http://xmlns.jcp.org/xml/ns/javaee"[/url] xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance"]http://www.w3.org/2001/XMLSchema-instance"[/url] xsi:schemalocation="[url=http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">]http://xmlns.jcp.org/xml/ns/ja ... gt%3B[/url]
<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

, 如提到的B.


faces-config.xml


我也试图删除一切 JSF 具体的注释 UserBean.java 只使用 Spring 具体的注释,如下所示 -


@Component
@SessionScoped
public class UserBean implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
@Autowired
private UserServiceImpl userServiceImpl;


}


但是当我发送形式时,我弄错了 target Unreachable 为了
#{userBean/

. 代表着
userBean

未检测到 Spring

5/ 我不错过任何东西 ?
6/我不使用内置的 tomcat, 提供的S. Spring Boot
已邀请:

八刀丁二

赞同来自:

所以我有 JSF 合作 Spring Boot /满的
https://github.com/xtremebiker/jsf-spring-boot
/:

1. 依赖性

除了标准依赖 web starter 你需要打开 tomcat embedded 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.

注册servlet. JSF 并在启动时调整它以下载 /不需要 web.xml/. 在工作时。 JSF 2.2 最佳使用映射
*.xhtml

, 至少在使用方面时:


@Bean
public ServletRegistrationBean servletRegistrationBean// {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean/
new FacesServlet//, "*.xhtml"/;
servletRegistrationBean.setLoadOnStartup/1/;
return servletRegistrationBean;
}


使您的配置类实现
ServletContextAware

https://docs.spring.io/spring- ... .html
因此,您可以设置初始化设置。 在这里,你必须强迫 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 一体化

调整
https://docs.spring.io/spring- ... .html
EL 在faces config.xml中。 它将是您的演示文稿文件和您的托管属性和Bob方法之间的链接:


xml version="1.0" encoding="UTF-8"?
<faces-config version="2.2" xmlns="[url=http://xmlns.jcp.org/xml/ns/javaee"]http://xmlns.jcp.org/xml/ns/javaee"[/url] xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance"]http://www.w3.org/2001/XMLSchema-instance"[/url] xsi:schemalocation="[url=http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">]http://xmlns.jcp.org/xml/ns/ja ... gt%3B[/url]
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>


4. 审查区

写一个自定义区域 Spring 模仿演示区域 JSF /请记住,您的豆类将被管理 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. 准备工作!

现在,您可以如下声明托管豆。 不要忘记使用
@Autowired

/优选地,在构造师中/ 反而
@ManagedProperty

, 因为你处理 Spring 鲍勃。


@Component
@Scope/"view"/
public class MyBean {

//Ready to go!

}


仍然没有实现

我无法强迫 JSF 具体的注释在上下文中工作 Spring Boot. 因此 ,
@FacesValidator

,
@FacesConverter

,
@FacesComponent

等等不能使用。 但是,有机会在人员中声明它们 - config.xml /厘米。
http://xmlns.jcp.org/xml/ns/ja ... 2.xsd
/, 老式的方式。

也可以看看

:

https://coderoad.ru/25479986/
https://www.primefaces.org/por ... -3-0/
https://coderoad.ru/20602010/
Spring Boot
</string,></string,>

要回复问题请先登录注册