简体   繁体   中英

Passing Parameter From One Method To Another

I have a rather silly question, I need to pass a parameter from one method to another method. I have the following method

public String test(Employees emp)
{
   return emp.getempno();
}

I need to pass emp.getempno() to my another method loadDetails();

My problem is I cannot add an argument in loadDetails() method because I am calling this method in couple of other places.

How can I achieve this? I tried putting emp.getempno() in collecion object but problem is test(Employees emp) methood is not being invoked in my second method.

Excuse me for my ignorance, any help is highly appreciable.

Thanks

Update 1

This is how I assign value to test method and getTestValues method is being called from another class when I pass parameter from one page to another.

public void getTestValues(List<Employees> paramList)  {
    for (Employees dataItem: paramList) {
        test(dataItem);
    }
}

Update 2

This is my loadDetails() method where I am fetching db values and to display as datatable in jsf page.

private void loadDetails() {

        try {

            dataDetails = anotherclass.deptDetails(passempno);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("error from loadDetails" + e.getMessage());
        }

    }

OK, I'm still trying to wrap my head around this, but I think it's becoming a bit clearer. In order to make loadDetails() function properly, you need to know an employee number (shown above in passempno ). I have two responses:

  1. Best case, it appears that passempno is really a parameter of the loadDetails() method. Ie, the value of passempno is part of the method's behavior. As such, the strongly preferred option is to simply add the employee number as an argument. You state that you don't want to do this because other places are using it -- how are the other clients of the method using it without having an employee number specified?

  2. If this is part of some non-trivial state of the parent class, then possibly you need to sock away the employee number in a field of the object. This is less than desirable because it's hiding (or at least making implicit) important state that loadDetails() needs to function.

  3. If there is a stateful interaction with the backing database, and the employee number here is a piece of that state, I'd recommend factoring out the state of the database interaction into a subsidiary class that holds the employee number and any other state (as suggested in 2 above).

Let me know if this helps; if not... let me know what I missed.

Basically what I am trying to do is when I click a row in a datatable I would like to pass that row's primary key to second page's bean class so that I could populate datatable in second page.

JSF 1.1 and would like to pass as POST GET with commandlink outputlink in datatable.

Use the following (assuming JSF 1.1):

EmployeeBacking

public class EmployeeBacking {

    private List<Employee> list;

    public EmployeeBacking() {
        list = employeeService.list();
    }

    // ...
}

employees.jsp

<h:dataTable value="#{employeeBacking.list}" var="employee">
    <h:column>
        <h:outputText value="#{employee.name}" />
    </h:column>
    <h:column>
        <h:outputLink value="departments.jsp">
            <h:outputText value="Show departments" />
            <f:param name="employeeId" value="#{employee.id}" />
        </h:outputLink>
    </h:column>
</h:dataTable>

DepartmentBacking

public class DepartmentBacking {

    private Long employeeId;
    private List<Department> list;

    private void load() {
        list = departmentService.list(employeeId);
    }

    public List<Department> getList() {
        if (list == null) load();
        return list;
    }

    // ...
}

(please note the lazy loading in getter, in JSF 1.2 you could better use @PostConstruct method for this)

departments.jsp

<h:dataTable value="#{departmentBacking.list}" var="department">
    <h:column>
        <h:outputText value="#{department.name}" />
    </h:column>
</h:dataTable>

faces-config.xml

<managed-bean>
    <managed-bean-name>employeeBacking</managed-bean-name>
    <managed-bean-class>com.example.EmployeeBacking</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>departmentBacking</managed-bean-name>
    <managed-bean-class>com.example.DepartmentBacking</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>employeeId</property-name>
        <value>#{param.employeeId}</value>
    </managed-property>
</managed-bean>

What happens here, the outputlink will navigate to departments.jsp with the current employee ID as request parameter and the <managed-property> in faces-config.xml will set it in the department backing and finally the getter on the list will lazily load the right departments based on the employee ID.

I work on an ERP software for a living. Your LoadDetails method shouldn't take an Employee as a parameter.

I'm assuming loadDetails is on a Load class, and that you have an Employee who scheduled the load, or filled the load. If that's the case, the Load class should have a reference to that Employee from creation time.

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