简体   繁体   中英

Passing session between jsf backing bean and model

  • Background :

I am having backing bean which has upload method that listen when file is uploaded. Now I pass this file to parser and in parser am doing validation check for row present in csv file.

If validation fails, I have to log information and saving in logging table in database.

  • My end goal :

Is to get session information in logging bean so that I can get initialContext and make call to ejb to save data to database.

  • What is happening :

In my upload backing bean, am getting session but when i call parser, I do not pass session information as I do not want parser to be dependent on session as I want to unit test parser individually.

So in my parser, I do not have session information, from parser am making call to logging bean(just a bean with some ejb methods) but in this logging bean, i need session because i need to get initial context .

Question

  1. Is there a way in JSF, that I can get the session in my logging bean that I have in my upload backing bean ?

I tried doing:

FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);

but session value was null , more generic question would be :

  1. How can I get session information in model bean or other beans that are referenced from backing beans in which we have session?
  2. Do we have generic method in jsf using which we can access session information throughout JSF Application?

So what i did to get around this,

I saved parameters as query string from xhtml like from status.xhtml

<h:link outcome="Log" value="${result.id}">
                <f:param name="jobid" value="${result.id}"/>
                <f:param name="userid" value="${result.userId}"/>
            </h:link>

now in my log.xhtml , i have

<f:metadata>
<f:viewParam name="jobid" value="#{bean.jobId}"/>
<f:viewParam name="userid" value="#{bean.userId}"/>
</f:metadata>

and in my log bean i have,

public void init()
    {
        String userId1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("userid");
        String jobId1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("jobid");
        userId = Long.parseLong(userId1);
        jobId = Long.parseLong(jobId1);

    }

Reference: Processing Get Parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM