Access Arrays inside Java Objects - arrays

How do I obtain values of an array that is located inside a java object in a jsp page?
I have set an object attribute so that in the jsp page I can call the object like so
${obj.property}
My question is how would I obtain property String [] example from Object obj?
<c:forEach var="prop" items="${obj.example}">
<td>${prop}</td>
</c:forEach>
I get Errors that tell me the class obj.Obj does not have the property property 'example'
and obviously I don't get the data out.
Actual errors:
org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: The class 'roommate.Roommate' does not have the property 'favProfessors'.
javax.el.PropertyNotFoundException: The class 'roommate.Roommate' does not have the property 'favProfessors'
And my actual class:
package roommate;
public class Roommate{
public String firstname;
public String lastname;
public String gender;
public String place;
public String[] favProfessors;
public Roommate(String fname, String lname, String roommateGender, String hangout,String[] professors) {
firstname= fname;
lastname= lname;
gender= roommateGender;
place= hangout;
favProfessors= professors;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String newFirstname)
{
this.firstname = newFirstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String newLastname)
{
this.lastname = newLastname;
}
public String getGender()
{
return gender;
}
public void setGender(String newGender)
{
this.gender = newGender;
}
public String getHangout()
{
return place;
}
public void setHangout(String newPlace)
{
this.place = newPlace;
}
public String[] getProfessors()
{
return favProfessors;
}
public void setProfessors(final String[] newfavProfessors)
{
this.favProfessors = newfavProfessors;
}
public void addRoommate(String fname, String lname, String roommateGender, String hangout,String[] professors)
{
}
}
I create the object in my servlet as well ass the Atrribute
String [] profArray = request.getParameterValues("professor");
Roommate roommate= new Roommate(
session.getAttribute("fname").toString(),
session.getAttribute("lname").toString(),
session.getAttribute("gender").toString(),
session.getAttribute("hangout").toString(),
profArray);
session.setAttribute("roommate",roommate);
I asked this earlier but did not receive a clear answer. I think my issue is in pulling the data out in the jsp alone in my forEach that I mentioned at the top

javax.el.PropertyNotFoundException: The class 'roommate.Roommate' does not have the property 'favProfessors'
Java is right. You do not have a getFavProfessors() method in that class. It's instead the following:
public String[] getProfessors()
{
return favProfessors;
}
You have 2 options: use ${roommate.professors} instead, or fix the getter method name to be getFavProfessors().
In contrary to what most starters think, EL does not access private properties directly. EL just calls the public getter/setter methods according the Javabeans specification. The real private property behind it can have a completely different name or even not exist at all.

Related

How to update a string property in the Model from an aggregated ViewModel

I'm working on a GUI application in WPF/MVVM. Let's say i have a Model class which is populated deserializing a (third-party) XML file
class Person
{
public string Name { get; set; }
public string Address { get; set; }
public string ReservationId { get; set; }
}
and a ViewModel which exposes to its View a lot of properties and commands to manipulate the Model, in this case strings (such as ReservationId):
class StringManipulatorViewModel
{
string modelString; //here's the problem
public StringManipulatorViewModel(string modelString)
{
this.modelString = modelString;
}
//Stuff to let the view manipulate the string
}
StringManipulatorViewModel is highly reusable and it is used by a lot of ViewModels, e.g.
class PersonViewModel
{
Person model;
public StringManipulatorViewModel ReservationManipulatorVM; //aggregated ViewModel
public StringManipulatorViewModel AddressManipulatorVM; //aggregated ViewModel
public PersonViewModel(Person model)
{
this.model = model;
ReservationManipulatorVM = new StringManipulatorViewModel(model.ReservationId); //here's the problem
AddressManipulatorVM = new StringManipulatorViewModel(model.Address); //here's the problem
}
}
Obviously passing the string as "model" to the ViewModel isn't effective, and C# doesn't seem to allow string references as fields.
What is the best/right way to let member ViewModels manipulate the Model when dealing with string types?
Thank you
Your problem is that you are trying to reference a property, not a string field.
But you can pass a delegate to the setter of the property.
If you also change the modelString field to a property, you can call this delegate automatically when the string is changed.
class StringManipulatorViewModel
{
private string modelString
{
get { return _modelString; }
set { _modelString = value; if (SetModelString != null) SetModelString(value); }
}
private string _modelString;
Action<string> SetModelString;
public StringManipulatorViewModel(string initialValue, Action<string> setModelString)
{
this.modelString = initialValue;
SetModelString = setModelString;
}
//Stuff to let the view manipulate the string
}
You initiate the StringManipulatorViewModel in PersonViewModel like this:
ReservationManipulatorVM = new StringManipulatorViewModel(model.ReservationId, value => model.ReservationId = value); //here's the problem
Here are some other ideas when you want to pass a property.
Passing properties by reference in C#
I can't think of a "right" way to manipulate the string as a reference inside the StringManipulatorViewModel, once you pass the string as a value it has nothing to do with the model.
But a way to legitimately change the model string value whenever the StringManipulatorViewModel manipulates it, is by raising an Event in the view model when it manipulates the string and then add an event handler to update the model with the new value:
class StringManipulatorViewModel
{
string modelString;
public event EventHandler<string> StringManipulated;
public StringManipulatorViewModel(string modelString)
{
this.modelString = modelString;
}
public ManipulateString()
{
// Manipulate the string
StringManipulated?.Invoke(this, modelString);
}
}
And in the PersonViewModel constructor:
class PersonViewModel
{
Person model;
public StringManipulatorViewModel ReservationManipulatorVM;
public StringManipulatorViewModel AddressManipulatorVM;
public PersonViewModel(Person model)
{
this.model = model;
ReservationManipulatorVM = new StringManipulatorViewModel(model.ReservationId);
AddressManipulatorVM = new StringManipulatorViewModel(model.Address);
ReservationManipulatorVM.StringManipulated += (sender, e) => model.ReservationId = e;
AddressManipulatorVM.StringManipulated += (sender, e) => model.Address = e;
}
}

Salesforce apex class Unexpected token error

I want to create a keyword search referring to "UserName__c" api on Salesforce Apex class.
compile error Unexpected token 'UserName__c'.
public with sharing class AccountListCon {
static List<String> TARGET_FIELDS = new List<String>{
'Name'
,'UserName__c'
,'CompanyName__c'
};
public SearchCondition condition{ get;set; }
public String UserName__c results { get;set; }
public String sortingField { get;set; }
public void init(){
this.condition = new SearchCondition();
this.results = new String UserName__c();
}
public PageReference clear(){
init();
return null;
}
There is no such type String UserName__c. It's not entirely clear what you want to do here, but I suspect you intend just to declare a String variable. The fact that you're looking for values in some field whose API name is UserName__c is not relevant to the type system
public String UserName__c results { get;set; } is wrong. Is this supposed to be just
public String results { get;set; } ?

getter setter member variable not visible salesforce

When I bind data with custom controller, the following code works well.
public class searchContactCtrl {
String searchText;
public String getSearchText(){
return searchText;
}
public void setSearchText(String s ){
searchText =s;
}
}
but if I changed getter method of "searchText" as below, an error was occured.
public class searchContactCtrl {
public String searchText{get;}
public void setSearchText(String s ){
searchText =s;
}
}
Error message:
Compile Error: member variable not visible for assignment
Why is this happening?
You will need to define a private setter for the searchText property. E.g.
public class searchContactCtrl {
public String searchText {
get;
private set;
}
public void setSearchText(String s) {
searchText = s;
}
}
Or you could just remove the private modifier and directly set searchText without the setSearchText method.

Appengine query returning no matching index - Objectify

I have some entities that I wish to do an ancestor query on and filter a parameter by the ">" operator.
The entity in question inherits another object(I don't think this should matter). Below are my entity classes:
#Indexed
public class ValidatedObject {
public Long timeCreated=System.currentTimeMillis();
public Long timeUpdated=System.currentTimeMillis();
public Long getTimeUpdated() {
return timeUpdated;
}
public void setTimeUpdated(Long timeUpdated) {
this.timeUpdated = timeUpdated;
}
public Boolean validated=false;
#Unindexed
public String validatedID;
#Unindexed
private Long validatedTime;
#Unindexed
private String creatorID;
public Long getTimeCreated() {
return timeCreated;
}
public void setTimeCreated(Long timeCreated) {
this.timeCreated = timeCreated;
}
public boolean isValidated() {
return validated;
}
public void setValidated(boolean validated) {
this.validated = validated;
}
public String getValidatedID() {
return validatedID;
}
public void setValidatedID(String validatedID) {
this.validatedID = validatedID;
}
public Long getValidatedTime() {
return validatedTime;
}
public void setValidatedTime(Long validatedTime) {
this.validatedTime = validatedTime;
}
public String getCreatorID() {
return creatorID;
}
public void setCreatorID(String creatorID) {
this.creatorID = creatorID;
}
}
#Cached
#Entity
public class PersonnelInfo extends ValidatedObject{
#Id
public String keyName;
#Parent Key<Department> department;
private Long fdID;
#Unindexed
private String userKeyName;
private String firstName;
private String lastName;
#Unindexed
private String address,city,county,state;
#Unindexed
private String cellPhone,homePhone,otherPhone;
public PersonnelInfo(){
}
public PersonnelInfo(String email){
keyName=email;
}
#Override
public Long getTimeUpdated() {
return timeUpdated;
}
#Override
public void setTimeUpdated(Long time) {
timeUpdated=time;
}
}
My query code is as follows:
Query<PersonnelInfo> q = ofy.query(PersonnelInfo.class).ancestor(tmp).filter("timeUpdated >", lastSync);
I am getting the "no matching index found" error everytime. The query works fine without the filter. Some of the entities are missing the "timeUpdated" field because I changed the schema. There are some entities that have been created after the schema change with timeUpdated values and they are not returned. Also I can do a GQL query on the datastore viewer like this:
select * where FROM PersonnelInfo timeUpdated > 0
and I am returned entities, which makes me believe the index is created. What am I doing wrong here? Any help would be appreciated!
You need a multi-property index defined in your datastore-indexes.xml. It should be added to datastore-indexes-auto.xml when you run the query in dev mode, but the result should look like this:
<datastore-index kind="PersonnelInfo" ancestor="true">
<property name="timeUpdated" direction="asc"/>
</datastore-index>

Why can't I add attributes when serializing ObservableCollection<T>?

I'm trying to extend an ObservableCollection with a few custom properties and have it serialize. However, I can't seem to get it to serialize these properties. I'm using .NET 4.0 where they fixed the serialization issues of ObservableCollection, but am still having problems. My hunch is that GetObjectData is being called on the base class and not mine. Any ideas?
[Serializable]
[XmlRoot(ElementName = "MyCollection")]
public class MyCollection : ObservableCollection<MyItem>, ISerializable
{
private string name;
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", Name);
}
private MyCollection()
{
Name = string.Empty;
}
public MyCollection(string name)
{
Name = name;
}
public MyCollection(SerializationInfo info, StreamingContext context)
{
Name = (string)info.GetValue("Name", typeof(string));
}
[XmlAttribute]
public string Name
{
get { return name; }
protected set
{
string originalName = name;
name = value;
if (originalName != name)
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
public void SaveToFile(string path)
{
string directory = Path.GetDirectoryName(path);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
XmlSerializer serializer = new XmlSerializer(typeof(MyCollection));
using (TextWriter textWriter = new StreamWriter(path))
{
serializer.Serialize(textWriter, this);
textWriter.Close();
}
}
public static MyCollection LoadFromFile(string path)
{
XmlSerializer deserializer = new XmlSerializer(typeof(MyCollection));
using (TextReader textReader = new StreamReader(path))
{
MyCollection myCollection = (MyCollection)deserializer.Deserialize(textReader);
textReader.Close();
return myCollection;
}
}
}
XML Serialization does not support this scenario. You simply cannot add anything to a class implementing ICollection.
If you require this, then you will have to implement IXmlSerializable and do the work yourself.
Note that you may be confusing XML Serialization with runtime serialization. XML Serialization doesn't care about the [Serializable] attribute or GetObjectData, etc.

Resources