请先关注.jsf:
<ui:repeat var="prod" value="#{showProducts.decoys}">
<h:form>
{prod.price}
{prod.weight}
{prod.size} >
<h:commandButton value="Buy" action="shoppingCart"/>
</h:form>
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
有以下shoppingCart.jsf:
<h:form>
<h:dataTable value="#{prod}">
<h:column>
#{prod.name}<br/>
</h:column>
<h:column>
#{prod.price}<br/>
</h:column>
<h:column>
<h:inputText value="#{prod.count}" size="3"/>
</h:column>
</h:dataTable>
<h:inputText value="#{order.phone}"/><br/>
<h:inputText value="#{order.mail}"><br/>
<h:inputText value="#{order.city}"/><br/>
<h:commandButton value="Order" action="#{showProducts.persistOrder}">
</h:form>
Run Code Online (Sandbox Code Playgroud)
面,配置:
<managed-bean>
<managed-bean-name>showProducts</managed-bean-name>
<managed-bean-class>main.ShowProducts</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
...
<managed-property>
<property-name>product</property-name>
<value>#{product}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>product</managed-bean-name>
<managed-bean-class>main.Product</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
...
Run Code Online (Sandbox Code Playgroud)
问题:
定义为product
迭代的托管bean名称是这样的(shoppingCart.jsf):
h:dataTable value="#{prod}">
所以这意味着这个迭代没有与bean命名product无论如何
如何将属性设置prod.price,prod.weight,prod.count为真正的托管bean属性:
product.price,product.weight,product.size
Run Code Online (Sandbox Code Playgroud) 我正在研究库中的方法.该方法传递一个bean名称和属性名称,我想使用ELResolver从指定的bean中检索属性的值.由于代码在库中(并且为了灵活性),代码中没有硬编码的bean名称.
这是我正在使用的代码:
elResolver.getValue( facesContext.getELContext(), elResolver.getValue(facesContext.getELContext(), null, myBean ), beanProperty )
Run Code Online (Sandbox Code Playgroud)
bean和属性由myBean和指定beanProperty.
这似乎有效,但有更好的方法吗?我目前正在使用JSF 1.2.
谢谢!
好的,这是我的会话bean.我总是可以从任何Servlet或Filter中检索currentUser.这不是问题问题是fileList和currentFile.我用简单的int和Strings进行了测试,并且它的效果相同.如果我从视图范围bean中设置一个值,我可以从另一个类中获取数据.
@ManagedBean(name = "userSessionBean")
@SessionScoped
public class UserSessionBean implements Serializable, HttpSessionBindingListener {
final Logger logger = LoggerFactory.getLogger(UserSessionBean.class);
@Inject
private User currentUser;
@EJB
UserService userService;
private List<File> fileList;
private File currentFile;
public UserSessionBean() {
fileList = new ArrayList<File>();
currentFile = new File("");
}
@PostConstruct
public void onLoad() {
Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
String email = principal.getName();
if (email != null) {
currentUser = userService.findUserbyEmail(email);
} else {
logger.error("Couldn't find user information from login!");
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个例子.
我的观点是scoped bean.这就是它的装饰方式.
@ManagedBean
@ViewScoped
public …Run Code Online (Sandbox Code Playgroud) 如何动态更改"value"属性的托管bean?例如,我有h:inputText,并且根据输入的文本,托管bean必须是#{studentBean.login}或#{lecturerBean.login}.以简化形式:
<h:inputText id="loginField" value="#{'nameofbean'.login}" />
Run Code Online (Sandbox Code Playgroud)
我试图嵌入另一个el-expression而不是'nameofbean':
value="#{{userBean.specifyLogin()}.login}"
Run Code Online (Sandbox Code Playgroud)
但它没有成功.
我似乎无法使视图作用域托管bean与setPropertyActionListener一起使用:
<h:commandButton value="Edit" action="edit-company.xhtml">
<f:setPropertyActionListener target="#{companyHolder.item}" value="#{company}"/>
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)
如果companyHolder是会话或请求作用域,但如果其视图作用域,则无效.这是正常的吗?
我有一个我在inputtext中显示的Employee对象.例如,员工的名字显示在inputtext中.当这个名字的值改变时,它调用一个方法.在此之前,我想调用一个方法,该方法将员工的ID保存在managedbean中,以便我知道哪个员工需要更改.我怎么做到这一点,到目前为止我得到了这个:
<h:outputText value="First name:"/>
<p:inplace id="firstname" editor="true">
<p:ajax event="save" onsuccess="#{employeeController.saveName()}"/>
<p:inputText id="firstName" value="#{emp.firstName}"
required="true" label="text"
valueChangeListener="#{employeeController.firstNameChanged}">
<p:ajax event="valueChange" listener="#{employeeController.onValueChangedStart}"/>
</p:inputText>
</p:inplace>
Run Code Online (Sandbox Code Playgroud)
我想我应该使用onValueChangedStart或firstNameChanged方法传递ID.我该怎么做呢?或者有更好的方法吗?emp有一个吸气剂.所以#{emp} .id得到它.
我对JSF很新,并没有真正"习惯"不同的思维,所以我在努力(我假设)是基本的.
假设我有一个类User,它是一个会话bean.
假设我有10000个对象的控制器,比如Factory,它需要能够将它们中的一些设置为"锁定",在我们的例子中,它意味着"锁定"字段不再变为空,而是引用"LockedItem"宾语.
这是我无法工作的地方:LockedItem,当你实现它时,应该引用当前登录的用户.我该怎么做?
我尝试使用@managedproperty进行注入,但是在LockedItem.constructor中它是null(这是正常的我假设)然后我尝试了@PostConstruct方法,但是这个方法永远不会被调用(为什么?即使我把它变成了一个托管bean ...是只有在".xhtml"创建对象时调用的postconstruct方法?)或者我应该使用"java se"技巧,比如使用户静态?
代码澄清为什么没有调用@PostConstruct("Seat"之一):
.xhtml
<h:outputLabel id="user" value="Hello #{user.name}" />
<h:outputLabel id="car" value="you have #{car.brand}" />
Run Code Online (Sandbox Code Playgroud)
用户
package test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class User implements Serializable {
private String name ;
public User()
{
name = "toto";
System.out.println("User constructor");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
汽车
package test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
@ManagedBean
public class Car implements Serializable …Run Code Online (Sandbox Code Playgroud) jsf design-patterns code-injection postconstruct managed-bean
情况:我有一个JavaServer Faces页面和一个会话范围的托管bean,它有两个ArrayList<Integer>属性:一个用于保存可能值列表,另一个用于保存选定值列表.在JSF页面上有一个<h:selectManyListBox>绑定了这两个属性的组件.
问题:提交表单后,所选的值将转换为字符串(ArrayList类型的属性实际上包含几个字符串!); 但是,当我使用转换器时,我收到如下错误消息:
验证错误:值无效
问题:如何正确地将ArrayList<Integer>属性绑定到<h:selectManyListBox>组件?
谢谢你的帮助.
具体代码
JSF页面:
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form>
<h:selectManyListbox value="#{testBean.selection}">
<f:selectItems value="#{testBean.list}"></f:selectItems>
</h:selectManyListbox>
<h:commandButton action="#{testBean.go}" value="go" />
<ui:repeat value="#{testBean.selection}" var="i">
#{i}: #{i.getClass()}
</ui:repeat>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
托管bean:
import java.io.Serializable;
import java.util.ArrayList;
@javax.faces.bean.ManagedBean
@javax.enterprise.context.SessionScoped
public class TestBean implements Serializable
{
private ArrayList<Integer> selection;
private ArrayList<Integer> list;
public ArrayList<Integer> getList()
{ …Run Code Online (Sandbox Code Playgroud) 我是JSF-2和CDI的新手(我来自Spring世界).
我想从@ManagedBean拦截一个方法,但我的Interceptor类永远不会被调用.有可能吗?
LogInterceptor.java
@Interceptor
public class LogInterceptor {
@AroundInvoke
public Object log(InvocationContext ctx) throws Exception {
System.out.println("begin method interceptor");
Object methodReturn = ctx.proceed();
System.out.println("end method interceptor");
return methodReturn;
}
}
Run Code Online (Sandbox Code Playgroud)
RoleMB
@ManagedBean
@ViewScoped
public class RoleMB extends BaseMB {
@Interceptors(LogInterceptor.class)
public void preEditRole(Role role) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
beans.xml中
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>br.com.preventsenior.services.log.LogInterceptor</class>
</interceptors>
</beans>
Run Code Online (Sandbox Code Playgroud)
将log(InvocationContext ctx)永远不会被调用.
我正在使用JSF 2.1和Primefaces:
我有一个带有托管属性的视图作用域托管bean,以及一个在其他视图范围内的托管bean上设置内容并转发到引用该托管bean的其他页面的方法:
@ManagedBean
@ViewScoped
public class HelloMB {
@ManagedProperty("otherMB")
private OtherMB other;
public String changeOtherMB() {
otherMB.setAnyObject(new Object());
return "otherPage.xhtml";
}
}
@ManagedBean
@ViewScoped
public class OtherMB {
private Object o;
public void setAnyObject(Object o) {
this.o = o;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当渲染otherPage时o为null.
你知道我怎么能解决这个问题?如何在@ViewScoped托管bean中保留对象并将其保存在其他页面上而不使用@SessionScoped?
managed-bean ×10
jsf ×8
jsf-2 ×4
java ×3
cdi ×2
el ×1
glassfish ×1
inject ×1
interceptor ×1
primefaces ×1
properties ×1
scope ×1
set ×1
view-scope ×1
xhtml ×1