+ Start a Discussion
ColinCColinC 

using Apex classes properties in VF pages

Is there any way to use Apex Classes in visualforce pages?   If I have one of my own classes I can refer to it in a page from a property of the controller.   I can also pass it to components - but I can't do much else.   Specifically if my class has properties I would like to use their values.  I can't get this to work.

For example if my class has a "public string Name" variable.   I can even add a "public string getName() {return Name;}" property-like method.   But in the page I can't refer to {!myInstance.Name} if myInstance is a controller property returning an item of my class.

SObjects seem to be the only values where the dot notation to get to properties actually works.

If I retunr a list of my class from the controller property I can then use the properties of my class in a repeat loop.   I just can't with a single item - not unless I do something goofy like return a list of one item.

Am I missing something?



Message Edited by ColinC on 06-19-2008 08:46 AM
ESES
Yup you are missing something :) You don't have to prepend controller's name to access stuff in it.

Following example shows binding to a apex-property as well as using getter to get a value.

Code:
public class HelloWorld{
public String myProperty {get;set;}

public String getAString(){
return 'hello ColinC';
}
}

<apex:page controller="HelloWorld">
<apex:form>
<apex:inputText value="{!myProperty}" />
</apex:form>
<apex:outputText value="{!AString}"/> 
</apex:page>

 

ColinCColinC
Sorry i didn't explain it well.  This is what I what I want to do:

public class Something{
public String myProperty {get;set;}
}

public class myController{
public Something mySomething {get;set;}
}


<apex:page controller="myController">
<apex:outputText value="{!mySomething.myProperty}"/> 
</apex:page>



It works if I say:

public class Something{
public String myProperty {get;set;}
}

public class myController{
public List<Something> mySomethings {get;set;}
}


<apex:page controller="myController">
<apex:repeat value="{!mySomethings}" var="item">
<apex:outputText value="{!item.myProperty}"/>
</apex:repeat> 
</apex:page>



 
Or:

Code:
public class myController{
public Something__c mySomething {get;set;}
}


<apex:page controller="myController">
<apex:outputText value="{!mySomething.myProperty__c}"/> 
</apex:page>


 i.e. if mySomething is an Sobject.

How can I return an Apex Class and access the properties of it without it being a list or an sobject.







Message Edited by ColinC on 06-19-2008 01:09 PM
ESES
It seems to work for me (there is no compile time error and nothing gets shown at runtime because the apex object is null), unless I misunderstood your question. May be you are not initializing you apex object in your controller which is causing the problem? I changed your example to initialize the properties and can see 'hello' when the page renders.

Code:
public class myController{
public Something mySomething {get;set;}
public myController(){
mySomething = new Something();
}
}

public class Something{
public String myProperty {get;set;}
public Something(){
myProperty ='hello';
}
}

<apex:page controller="my1Controller">
<apex:outputText value="{!mySomething.myProperty}"/> 
</apex:page>

 

ColinCColinC
I can see what my problem is......

My class is part of a package (that will eventually go on appex).   The controller isn't.  That setup doesn't work.   If I take the class out of the package it works fine.






Message Edited by ColinC on 06-20-2008 12:10 AM