사용중인 JSF 버전은 무엇입니까? JSF 2.0, 2.1 또는 2.2를 사용하면 CDI 빈을 사용할 수 있고 인터셉터를 사용하여 메서드 반환을 변경할 수 있습니다.
저는 비즈니스 로직을 기반으로 JSF 라이프 사이클을 변경하는 것이 좋습니다.
인터셉터를 사용할 수 있습니다.
@RedirectInterceptor
@Interceptor
public class MyInterceptor {
@Inject
HttpServletRequest req;
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
Logger.getLogger("CdiInterceptor").log(Logger.Level.INFO, "REQUEST "+req);
req.setAttribute("irA", "index.xhtml");
Logger.getLogger("CdiInterceptor").log(Logger.Level.INFO, "FACES CONT "+FacesContext.getCurrentInstance());
for(Map.Entry<String, Object> ent : context.getContextData().entrySet()){
Logger.getLogger("CdiInterceptor").log(Logger.Level.INFO, ent.getKey()+ " --- "+ent.getValue());
}
Logger.getLogger("CdiInterceptor").log(Logger.Level.INFO, " Init logger");
Object value = context.proceed();
Logger.getLogger("CdiInterceptor").log(Logger.Level.INFO, " Value : "+value);
Logger.getLogger("CdiInterceptor").log(Logger.Level.INFO, req.getAttribute("irA"));
return req.getAttribute("irA");
}
}
을 그리고 마지막으로 당신은 사용 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://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:inputText id="txtNro" value="#{testCDI.value}"></h:inputText>
<h:commandButton value="Send" action="#{testCDI.doSomething()}" />
</h:form>
</h:body>
</html>
예를 들어
그런 다음 CDI 인터셉터 차단 FacesContext와 HttpServletRequest의은 (당신은 그들 중 하나를 사용할 수 있습니다) CDI 컨트롤러에서 HTTP 서블릿 요청을 분석하기위한 "HttpServletRequest"를 사용하면 put reque에 사용할 수도 있습니다. st 특성.
@Named(value = "testCDI")
@RequestScoped
public class CdiInterceptor implements Serializable{
@Inject HttpServletRequest req;
private int value;
@RedirectInterceptor
public String doSomething(){
if(value%3 ==0){
req.setAttribute("irA", "index.xhtml");
}else if(value%3==1){
req.setAttribute("irA", "page1.xhtml");
}else{
req.setAttribute("irA", "page2.xhtml");
}
return null;
}
이렇게하면 HTTP Servlet 요청을 기반으로 다른 JSF 페이지로 리디렉션 할 수 있습니다.
당신은 하나 이상의 방법에 적용해야 할 때 당신이 그것을 사용하므로, beans.xml 환경
<?xml version="1.0" encoding="UTF-8"?>
<beans 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 http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
<interceptors>
<class>cdi.test.MyInterceptor</class>
</interceptors>
</beans>
인터셉터가 교차 절단 문제에 사용되는 UPDATE에 CDI 인터셉터를 가능하게 기억하고있다. 모든 방법에 가로 채기에 자신 만의 논리가 있다면 데코레이터를 사용할 수 있습니다.