-1

参考这个问题 h:commandLink 在列表中不起作用

我的应用程序中有同样的问题。我想尝试一个 ViewScoped bean,但由于使用 Spring 2.0 我没有机会将我的 bean 放入 View Scope。任何其他解决方法,我可以尝试。

如果你能给我一个提示会很好。

4

1 回答 1

0

您可以将视图范围移植到弹簧:

package com.yourdomain.scope;

import java.util.Map;
import javax.faces.context.FacesContext;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

public class ViewScope implements Scope {

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

    public Object remove(String name) {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }

    public String getConversationId() {
        return null;
    }

    public void registerDestructionCallback(String name, Runnable callback) {
        //Not supported
    }

    public Object resolveContextualObject(String key) {
        return null;
    }
}

在 spring 配置文件中注册新范围

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="view">
                <bean class="com.yourdomain.scope.ViewScope"/>
            </entry>
        </map>
    </property>
</bean>

而不是将它与你的豆子一起使用

@Component
@Scope("view")
public class MyBean {

    ...

}
于 2011-11-09T13:03:18.673 回答