我刚开始阅读Core JavaServer Faces,第3版.他们这样说(强调我的):
对于可以在JSF页面中使用的bean,有两种不同的机制,CDI bean和JSF托管bean,这是一个历史事故.我们建议您使用CDI bean,除非您的应用程序必须在像Tomcat这样的普通servlet运行器上运行.
为什么?他们没有提供任何理由.我一直在使用@ManagedBean在GlassFish 3上运行的原型应用程序中的所有bean,我还没有发现任何问题.我不介意迁移@ManagedBean到@Named,但我想知道为什么我应该打扰.
我有一个名为userSession的SessionScoped bean来跟踪用户(用户名,ifLogged等).我想过滤一些页面,因此我需要从我创建的webFilter访问bean.我怎么做?我看起来甚至不可能将豆子导入到潜在的可见区域.
我想问一下我是否将我的托管bean放在会话范围内,然后将它存储在会话中"就像我有这样的bean一样
@ManagedBean
@SessionScoped
public class SessionScopedBean implements Serializable {
.......
} //end of class SessionScopedBean
Run Code Online (Sandbox Code Playgroud)
然后它存储在会话中,在我的会话期间我可以使用它
session.getAttribut("SessionScopedBean");
Run Code Online (Sandbox Code Playgroud)
这将给我SessionScopedBean对象,当会话将被销毁时,我将得到null.现在我想问一下我的bean是否在视野范围内,那我怎么能得到它.喜欢
@ManagedBean
@ViewScoped
public class ViewScopedBean implements Serializable {
.......
} //end of class ViewScopedBean
Run Code Online (Sandbox Code Playgroud)
现在如果视图是持久化的,那么这个bean处于视图状态,当视图发生变化时,这个bean将会被破坏.现在我想问一下,如果视图仍然存在,我怎样才能从视图状态获取这个bean.喜欢
view.getAttrubute("ViewScopedBean"); //just a code. No actual implementation.
Run Code Online (Sandbox Code Playgroud)
谢谢
我需要知道从servlet访问JSF managedBean(定义了应用程序范围)的最佳方法是什么.目前我的servlet中有这样的东西:
MyApplicationScopeBean bean = null;
try {
FacesContext fContext = FacesUtil.getFacesContext(req, resp);
ServletContext sc = (ServletContext) fContext.getExternalContext().getContext();
bean = (MyApplicationScopeBean) sc.getAttribute("myManagedBean");
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
FacesUtil.java(如http://balusc.blogspot.com/2006/06/communication-in-jsf.html中所述):
import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FacesUtil {
// Getters -----------------------------------------------------------------------------------
public static FacesContext getFacesContext(
HttpServletRequest request, HttpServletResponse response)
{
// Get current FacesContext.
FacesContext facesContext = FacesContext.getCurrentInstance();
// Check current FacesContext.
if (facesContext == null) …Run Code Online (Sandbox Code Playgroud) 这是我的commandLink工作方式
<p:dataTable value="#{myBean.users}" var="item">
<p:column>
<h:commandLink value="#{item.name}" action="#{myBean.setSelectedUser(item)}" />
</p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)
然后在 myBean.java
public String setSelectedUser(User user){
this.selectedUser = user;
return "Profile";
}
Run Code Online (Sandbox Code Playgroud)
假设用户名是Peter.然后,如果我点击Peter,我将设置selectedUser为彼得的用户对象,然后重定向到配置文件页面,该页面现在呈现信息selectedUser.我想仅使用创建相同的效果<h:outputText>,因此我想到了GET请求.所以我这样做
<h:outputText value="{myBean.text(item.name,item.id)}" />
Run Code Online (Sandbox Code Playgroud)
那么text(String name, Long id)方法就回来了
"<a href=\"someURL?userId=\"" + id + ">" + name + "</a>"
Run Code Online (Sandbox Code Playgroud)
剩下的就是创建一个servlet,捕获它id,查询数据库以获取user对象,设置为selectedUser重定向.所以这是我的servlet
public class myServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Long userId …Run Code Online (Sandbox Code Playgroud) 我有以下模板(masterLayout.xhtml):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<f:view contentType="text/html">
<ui:insert name="metadata"/>
<h:head>
<title><ui:insert name="windowTitle"/> | MySite</title>
</h:head>
<h:body>
<div id="container">
<div id="header">
<ui:insert name="header">
<ui:include src="/WEB-INF/templates/header.xhtml"/>
</ui:insert>
</div>
<div id="content">
<ui:insert name="content"/>
</div>
<div id="footer">
<ui:insert name="footer">
<ui:include src="/WEB-INF/templates/footer.xhtml"/>
</ui:insert>
</div>
</div>
</h:body>
</f:view>
</html>
Run Code Online (Sandbox Code Playgroud)
和使用它的页面(search.xhtml):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title></title>
</h:head>
<h:body>
<ui:composition template="/WEB-INF/templates/masterLayout.xhtml">
<ui:define name="metadata">
<f:metadata>
<f:viewParam name="address" value="#{searchBean.address}"/>
<f:event type="preRenderView" listener="#{userSessionBean.preRenderViewCookieLogin(e)}"/>
<f:event type="preRenderView" listener="#{searchBean.preRenderView(e)}"/>
</f:metadata>
</ui:define>
<ui:define name="windowTitle">#{searchBean.address}</ui:define>
<ui:define name="content">
<!-- Content goes here --> …Run Code Online (Sandbox Code Playgroud) 我不确定我做的是不是错了,或者我错过了某个地方的注释或配置项.情况如下:
我有一个JSF应用程序与一个名为session的作用域bean SessionData.这个bean ApplicationData在创建时注入了一个应用程序范围的bean引用(类型).首次创建会话时,这可以正常工作.依赖注入是使用文件中的<managed-bean>元素完成的,faces-config.xml如下所示:
<managed-bean>
<managed-bean-name>sessionData</managed-bean-name>
<managed-bean-class>my.package.SessionData</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>applicationData</property-name>
<property-class>my.package.ApplicationData</property-class>
<value>#{applicationData}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>applicationData</managed-bean-name>
<managed-bean-class>my.package.ApplicationData</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
Run Code Online (Sandbox Code Playgroud)
因为在序列化时我的SessionData对象包含ApplicationData对象没有意义,所以我在对象中将ApplicationData引用标记为瞬态SessionData:
transient private ApplicationData applicationData;
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到Web应用程序停止(在我的Tomcat 6.x容器中)并且会话被序列化.当我重新启动应用程序并反序列化会话时,我的引用ApplicationData不会被JSF重新注入.我知道反序列化应该留下没有值的瞬态字段.有没有办法告诉JSF这个会话范围的对象要求在反序列化后再次设置其依赖项?
我使用MyFaces JSF 1.2和Tomcat 6.0.26作为我的Web应用程序容器.
我有一些JSF 1.0/1.1代码:
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding vb = context.getApplication().createValueBinding("#{someBean}");
SomeBean sb = (SomeBean) vb.getValue(context);
Run Code Online (Sandbox Code Playgroud)
从JSF 1.2开始,ValueBinding不推荐使用并替换为ValueExpression.我不知道如何更改上面的代码才能使用ValueExpression.
实际上是否可以在JSF中的托管组件之间传递任何数据?如果是的话,如何实现这一目标?
有人可以提供任何样品吗?
在我看来,@ApplicationScoped只有在第一次使用EL访问页面时才启动bean.
当我查询时ApplicationMap,是否@ApplicationScoped会创建bean?
ExternalContext ec = currentInstance.getExternalContext(); result =
ec.getApplicationMap().get(beanName);
Run Code Online (Sandbox Code Playgroud)
如何在加载XHTML页面之前触发应用程序作用域bean的实例化?
jsf ×8
jsf-2 ×5
java ×4
servlets ×2
binding ×1
cdi ×1
deprecated ×1
facelets ×1
httpsession ×1
java-ee ×1
javabeans ×1
managed-bean ×1