1

I have a class counterBean and I wish to instantiate two instances of counterBean in my jsp (for two seperate counters). How would I do this?

EDIT - Added code

package beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="CounterBean")
@SessionScoped
public class CounterBean implements Serializable
{
    private static final long serialVersionUID = 1L;
    private static int hitCount;

    public CounterBean() 
    {
        CounterBean.hitCount = 0;
    }

    public static int getCounter() 
    {
        hitCount++;
        return hitCount;
    }

    public static void setCounter(int hitCount) 
    {
        CounterBean.hitCount = hitCount;
    }

    public static int getValue() 
    {
        return hitCount;
    }
}
omegaFlame
  • 245
  • 3
  • 9
  • 21
  • The only thing I could think of is creating two different counter classes but that seems a bit too much. – omegaFlame Apr 29 '12 at 17:47
  • You tagged the question `[jsf-2.0]`. Are you *really* using the deprecated JSP view technology? Why not its successor Facelets? (which is XML based). – BalusC Apr 30 '12 at 18:41
  • It is purely educational I realize there are better ways of doing it, as you just mentioned, just curious of how to do it in jsp as I have been following tutorials. – omegaFlame Apr 30 '12 at 19:08

3 Answers3

1

It is better not to create objects directly in code of JSP. Since the JSP should only be view. The using of scriptlets in JSP page is not good practice. It's better to use the <jsp:useBean> tag:

<jsp:useBean id="firstCounterId' class="yourpackagename.CounterBean" />
<jsp:useBean id="secondCounterId' class="yourpackagename.CounterBean" />

And to change the value of a particular counter apply the tag <jsp:set Property>

<jsp:setProperty name="firstCounterId" property="myNumber" value="123"/>

But I think that almost any problem with changing and using a bean from the JSP can be solved by the use of tags <c:set> and <c:out>

kapandron
  • 3,546
  • 2
  • 25
  • 38
  • Thanks i'm trying to use the getCounter() method of the class to increment the counter. How would I do this with using the above method? – omegaFlame Apr 29 '12 at 19:29
  • In this way to change the value of your counter you can use the ``. And if you want to get the current value then use the tag ``. Where `number` is the property of your counterBean. For a more detailed answer, I must see your code. – kapandron Apr 30 '12 at 06:10
0

Create the counter bean as you are normally used to do:

@Named(value="counterBean")
@SessionScoped
public class CounterBeanClass implements Serializable {

    private int counter = 0;

    public CounterBeanClass() {
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public String addCounterValue() {
        this.counter++;
        return "";
    }
}

Then, create or add a new managed bean in your faces-config.xml file

<managed-bean>
    <managed-bean-name>anotherCounterBean</managed-bean-name>
    <managed-bean-class>my.backingbean.CounterBeanClass</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now you can call both counters as separated beans.

<h:form>
    <h:panelGrid columns="2">
        <h:outputText value="Counter 1" />
        <h:outputText value="#{counterBean.counter}" />
        <h:outputText value="Counter 2" />
        <h:outputText value="#{anotherCounterBean.counter}" />
        <h:commandButton value="Add Counter 1"
            action="#{counterBean.addCounterValue}" />
        <h:commandButton value="Add Counter 2"
            action="#{anotherCounterBean.addCounterValue}" />
    </h:panelGrid>
</h:form>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
-1

How about

CounterBean cb1 = new CounterBean();
CounterBean cb2 = new CounterBean();
Nitram76
  • 421
  • 3
  • 13