JSF2.0中的参数传递

JSF2.0中的参数传递,jsf,object,parameter-passing,view-scope,Jsf,Object,Parameter Passing,View Scope,我发现一些参考JSF参数正在传递 但是,这不符合我的要求。我将把对象(Student)一个支持bean传递给另一个bean,如下所示 studentTable.xhtml updateStudent.xhtml pass student object (not `StudentID` string)

我发现一些参考
JSF参数正在传递

但是,这不符合我的要求。我将把
对象(Student)
一个支持bean传递给另一个bean,如下所示

studentTable.xhtml                              updateStudent.xhtml
                        pass student object
                        (not `StudentID` string)
                        ==================> 
StudentTableBean.java                           UpdateStudnetBean.java
这两个支持bean可能是
RequestScope
ViewScope
,而不是
会话
。当我单击
studentTable.xhtml
上的链接(一行数据表)时,我希望将student对象传递给
updateStudent.xhtml


可能吗?你能提供一些参考资料吗?

你认为这怎么可能?Viewscope在您离开页面时结束,该范围内的所有bean都将被销毁,它们所持有的对象也将被销毁

updateStudent.xhtml
创建一个新视图,并获取它自己的一组视图范围bean。如果要在页面之间保留对象,请启动新的长时间运行的对话或将对象推入会话范围


有关使用对话范围的信息,请参见。

您可以通过将学生对象放入请求当前实例的会话映射中来实现:将问题学生的id从
studentTable.xhtml
传递给支持bean
UpdateStudnetBean.java
,然后从资源中搜索对象实例(列表、数据库等),然后将其放入上面的会话映射对象中
在视图
updateStudent.xhtml
中通过隐式对象
sessionScope

HTTP和HTML不理解复杂的Java对象。在Java透视图中,它们只理解字符串。您最好将复杂的Java对象转换为字符串风格的唯一标识符,通常是它的技术ID(例如,自动生成的数据库PK),然后在HTML链接中将该标识符用作HTTP请求参数

给定一个
列表
,该列表表示为带有以下链接的表

<h:dataTable value="#{studentTable.students}" var="student">
    <h:column>
        <h:link value="Edit" outcome="updateStudent.xhtml">
            <f:param name="id" value="#{student.id}" />
        </h:link>
    </h:column>
</h:dataTable>
<f:metadata>
    <f:viewParam name="id" value="#{updateStudent.student}" converter="#{studentConverter}" />
</f:metadata>


我在我的项目中使用了
Jboss-Seam
。现在我正在考虑删除
Jboss-Seam
。这就是为什么,我试图像
Conversation
@CycDemo一样进行研究。我也处于同样的位置,选择了ViewScoped bean和转换器,如下面BalusC所示(ConversationScoped是一个JEE7的补充,我还不确定)。由于所有查找都是通过PK进行的,因此它们对缓存非常友好,并且与JSF需要执行的所有其他操作相比应该具有闪电般的速度。传递
ID字符串
,再次从数据库中检索。否则,请使用
会话范围
应用程序范围
。我说得对吗?感谢您的提供。不要滥用范围,否则您将杀死thrEADS安全和用户体验。进一步阅读:和更多有用的链接:
private Student student;
@ManagedBean
@ApplicationScoped
public class StudentConverter implements Converter {

    @EJB
    private StudentService studentService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }

        if (!value.matches("[0-9]+")) {
            throw new ConverterException("The value is not a valid Student ID: " + value);
        }

        long id = Long.valueOf(value);
        return studentService.getById(id);
    }

    @Override    
    public String getAsString(FacesContext context, UIComponent component, Object value) {        
        if (value == null) {
            return "";
        }

        if (!(value instanceof Student)) {
            throw new ConverterException("The value is not a valid Student instance: " + value);
        }

        Long id = ((Student)value).getId();
        return (id != null) ? String.valueOf(id) : null;
   }

}