本文介绍了PostConstruct被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用

  • JSF
  • 春天
  • OCPSoft重写
  • 玻璃鱼4/码头9

我注意到我的bean两次调用@PostConstructinit()方法.这是示例Bean,它被初始化了两次,如果您需要web.xml或其他任何东西,只需将其发布-我就没有想法了.

I've noticed that my beans invoke @PostConstruct's init() method twice. Here's sample bean that got initialized twice, if you'll need web.xml or anything else, just post it - I ran out of ideas.

@ManagedBean(name = "userBean")
public class UserBean implements Serializable {

    private static final long serialVersionUID = -1347081883455053542L;
    @ManagedProperty(value = "#{param.username}")
    private String username;
    private Users user;
    private Authentication authentication;
    private StreamedContent avatar;

    @PostConstruct
    public void init() {
        System.out.println("userbean init and username: " + username);
        user = Users.findByUsername(username);
        authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (user == null) {
            Navigator.redirect("/601");
            return;
        }
        if (user.isKeepPrivate() == true && !username.equals(authentication.getName())) {
            Navigator.redirect("/600");
            return;
        }
        avatar = new DefaultStreamedContent(UserUtils.getAvatar(user), "image/png");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public StreamedContent getAvatar() {
        return avatar;
    }

    public void setAvatar(StreamedContent avatar) {
        this.avatar = avatar;
    }
}

推荐答案

我们在这里遇到了这个问题,但是是WebSphere 6的问题.(从websphere失控:D)

we have this problem here, but is a problem with WebSphere 6. (runaway from websphere :D)

所以...我们做了一些变通办法来使用@PostConstruct ...
也许可以帮助您...

So... we do a little workaround to use @PostConstruct...
Maybe can help you...

public boolean firstInit() {
    boolean firstInit= false;
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            firstInit= context != null  && context.getExternalContext().getRequestParameterMap().containsKey(ResponseStateManager.VIEW_STATE_PARAM);
        } catch (Exception e) {
            firstInit= false;
        }
        return firstInit;
    }
public void init(){
if (firstInit()) return;
//init methods
}

@PostConstruct方法两次调用相同的方法请求这也可以为您提供帮助...

And @PostConstruct method called twice for the same request this can help you too...

obs:我无法发表评论:/

obs: i cant write comments :/

这篇关于PostConstruct被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 18:41