How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1

后端 未结 5 1506
醉话见心
醉话见心 2020-11-27 13:45

I\'m currently evaluating Java EE 6 / JSF 2.1 with RichFaces.

A bean which is declared as

@ManagedBean
@ViewScoped
  1. Gets an I
5条回答
  •  没有蜡笔的小新
    2020-11-27 14:30

    Inject the conversation into your bean and in the @PostConstructor method start the conversation if the conversation is transient.

    And after deleting the record, end your conversation and navigate to your destination page. When beginning a conversation. Here is an example

    public class BaseWebBean implements Serializable {
    
    private final static Logger logger = LoggerFactory.getLogger(BaseWebBean.class);
    @Inject
    protected Conversation conversation;
    
    @PostConstruct
    protected void initBean(){
    }
    
    public void continueOrInitConversation() {
            if (conversation.isTransient()) {
                conversation.begin();
                logger.trace("conversation with id {} has started by {}.", conversation.getId(), getClass().getName());
            }
        }
    
    public void endConversationIfContinuing() {
            if (!conversation.isTransient()) {
                logger.trace("conversation with id {} has ended by {}.", conversation.getId(), getClass().getName());
                conversation.end();
            }
    }
    

    }

    @ConversationScoped
    @Named
    public class yourBean extends BaseWebBean implements Serializable {
        @PostConstruct
        public void initBean() {
            super.initBean();
            continueOrInitConversation();
        }
    
        public String deleteRow(Row row)
        {
            /*delete your row here*/
            endConversationIfContinuing();
            return "yourDestinationPageAfter removal";
        }
    
    }
    

提交回复
热议问题