How can i access to a method or getter/setter of a managed bean (e.g. session scoped) in an ivy-script-step ??
I know from an xhtml page it is possible, but i want to start a process in ivy (event) and read some data in a db-Step and write the data back to the managed bean !!

asked 06.06.2014 at 13:11

Wanderhirt's gravatar image

Wanderhirt
46337
accept rate: 100%


The creation of the object (managed bean) happens automatically, you must not do that by yourself (because you will receive/create another instance!). The point is now to get the correct object (created by faces context usually by the first access!).

The code in the script step looks like follows:

import javax.faces.context.FacesContextFactory;
import javax.faces.context.FacesContext;
import ch.soreco.mbean.mb.SimpleBean;

SimpleBean sb;
FacesContext context = FacesContext.getCurrentInstance();

sb = context.getApplication().evaluateExpressionGet(context, "#{simpleBean}", SimpleBean.class) as SimpleBean;

// invoke method of the managed bean
sb.setS1(in.input);

The code of the managed bean:

package ch.soreco.mbean.mb;

import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;

import ch.ivyteam.ivy.environment.Ivy;


@ManagedBean(name = "simpleBean")
@SessionScoped
public class SimpleBean 
{
    private String s1;
    private int counterSet = -1;
    private int counterGet = -1;


    public SimpleBean() 
    {
        super();
        s1 = "--";
        counterSet = 0;
        counterGet = 0;
        Ivy.log().warn("BEAN created!");
    }

    public String getS1() {
        counterGet++;
        Ivy.log().warn("BEAN getter called: "+counterGet);
        return s1;
    }

    public void setS1(String s1) {
        counterSet++;
        Ivy.log().warn("BEAN setter called: "+counterSet);
        this.s1 = s1;
    }

    public int getCounterSet() {
        return counterSet;
    }

    public void setCounterSet(int counterSet) {
        this.counterSet = counterSet;
    }

    public int getCounterGet() {
        return counterGet;
    }

    public void setCounterGet(int counterGet) {
        this.counterGet = counterGet;
    }
}

This approach works surely with session or application scoped beans.

link

answered 11.06.2014 at 09:27

Wanderhirt's gravatar image

Wanderhirt
46337
accept rate: 100%

We do it in the same way. But be allways clear on the context of your script step. The FacesContext is clearly definied just after your first JSF-UI appears.

Moreover, we have in some cases the problem, that we need special ivy system permission to read SessionBeans in this way, when you call it from different ivy projects (models). The behavior is, that the first bean could be loaded, but for the next one, the server search the Bean in a strange way. So you get an exception like this : ..does not fulfill the permission rule SESSION OWNS ProcessModelVersionReadAll PERMISSION.. --> Issue 24860

(11.06.2014 at 11:12) Adrian Imfeld Adrian%20Imfeld's gravatar image

The Issue 24860 should be fixed in Version 5.0.12

(17.06.2014 at 11:34) Adrian Imfeld Adrian%20Imfeld's gravatar image

Additional thinkings: If the jsf context scope is a problem and you need to have a session bean after all jsf uis are closed, you can add your bean to the ivy session. Add "Ivy.session().setAttribute("SimpleBean",this);" in the @PostConstruct methode of the bean. To read it you can use "SimpleBean sb = ivy.session().getAttribute("SimpleBean") as SimpleBean;"

It is not really sweet way, but it works. Beware of the Ivy context when you use the Ivy-class.

(23.07.2015 at 11:26) Adrian Imfeld Adrian%20Imfeld's gravatar image

In your Script-Step:

import de.path.to.MyBean;
MyBean instanceOfMyBean = new MyBean(); 
instanceOfMyBean.setWhatever(10);

If you want to use this bean in your view just declare a data-object of the given type (de.azt.exampleData.MapBean) in your data-class and bind this as model in your view code, f.e.:

<p:gmap zoom="6" type="TERRAIN" center="#{data.geoLat}, #{data.geoLng}" id="gmap" model="#{data.mapModel}">

The corresponding code in your script step would then be:

import de.azt.exampleData.MapBean;
out.mapModel = new MapBean(out.projects, true, out.auditors, false, true);

Whereas you need to pass the attributes from your database-query as parameters to the constructor of your bean.

Hope that helps.

link

answered 06.06.2014 at 13:43

Nikel%20Weis's gravatar image

Nikel Weis
321262933
accept rate: 57%

Usually i do that way: First i will create instance:

FacesContext context = FacesContext.getCurrentInstance();       
            FileManagerBean fileManagerBean=new FileManagerBean();
            context.getExternalContext().getSessionMap().put("fileManagerBean", fileManagerBean);

then i get it when i want to use like this:

import javax.faces.context.FacesContext;
import ch.ivyteam.ivy.addons.filemanager.html.managedbean.FileManagerBean;
FacesContext context = FacesContext.getCurrentInstance();               
FileManagerBean fileManagerBean=context.getExternalContext().getSessionMap().get("fileManagerBean") as FileManagerBean;
in.enablePreviewLink = fileManagerBean.enablePreviewLink;
link

answered 10.06.2014 at 11:12

trungdv's gravatar image

trungdv
450628398
accept rate: 52%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×72
×53

Asked: 06.06.2014 at 13:11

Seen: 2,969 times

Last updated: 23.07.2015 at 11:54