JsonMappingException: N/A when trying to deserialize JSON file - java

I have a User class and a Json file containing an array of Users. When trying to deserialize those users and get a List I'm getting a JsonMappingException, I don't understand what's wrong.
This is my User class:
public class User {
private StringProperty username;
private StringProperty password;
private StringProperty name;
private StringProperty surname;
private StringProperty email;
private StringProperty company;
private StringProperty phone;
private BooleanProperty canMonitorize;
private BooleanProperty canCreateProject;
private BooleanProperty canOpenProject;
private BooleanProperty admin;
public User() {}
public User(String user, String pass, String name, String surname, String email, String company, String phone,
boolean monitorize, boolean createP, boolean openP, boolean admin) {
this.username = new SimpleStringProperty(user);
this.password = new SimpleStringProperty(pass);
this.name = new SimpleStringProperty(name);
this.surname = new SimpleStringProperty(surname);
this.email = new SimpleStringProperty(email);
this.company = new SimpleStringProperty(company);
this.phone = new SimpleStringProperty(phone);
this.canMonitorize = new SimpleBooleanProperty(monitorize);
this.canCreateProject = new SimpleBooleanProperty(createP);
this.canOpenProject = new SimpleBooleanProperty(openP);
this.admin = new SimpleBooleanProperty(admin);
}
public String getUsername() {
return username.get();
}
public void setUsername(String username) {
this.username.set(username);
}
public String getPassword() {
return password.get();
}
public void setPassword(String password) {
this.password.set(password);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getSurname() {
return surname.get();
}
public void setSurname(String surname) {
this.surname.set(surname);
}
public String getEmail() {
return email.get();
}
public void setEmail(String email) {
this.email.set(email);
}
public String getCompany() {
return company.get();
}
public void setCompany(String company) {
this.company.set(company);
}
public String getPhone() {
return phone.get();
}
public void setPhone(String phone) {
this.phone.set(phone);
}
public boolean canMonitorize() {
return canMonitorize.get();
}
public void setCanMonitorize(boolean canMonitorize) {
this.canMonitorize.set(canMonitorize);
}
public boolean canCreateProject() {
return canCreateProject.get();
}
public void setCanCreateProject(boolean canCreateProject) {
this.canCreateProject.set(canCreateProject);
}
public boolean canOpenProject() {
return canOpenProject.get();
}
public void setCanOpenProject(boolean canOpenProject) {
this.canOpenProject.set(canOpenProject);
}
public boolean isAdmin() {
return admin.get();
}
public void setAdmin(boolean isAdmin) {
this.admin.set(isAdmin);
}
}
And this is an example of the Json file:
[{"username":"admin","password":"blablabla","name":"admin","surname":"admin","email":"admin#admin.com","company":"admin","phone":"admin","admin":true}]
This is the method that should obtain the list of users:
public static List<User> getUsers(String jsonArrayStr) {
ObjectMapper mapper = new ObjectMapper();
List<User> ret;
try {
User[] userArray = mapper.readValue(jsonArrayStr, User[].class);
ret = new ArrayList<>(Arrays.asList(userArray));
} catch (IOException e) {
return new ArrayList<User>();
}
return ret;
}
The error I get when executing the code:
com.fasterxml.jackson.databind.JsonMappingException: N/A (through reference chain: object.User["username"])

When you have a public 0-args constructor it is used by default to create new POJO instance. But in your case you should not allow to create instance with default constructor because all internal fields are null and when Jackson tries to set first property, username, NullPointerException is thrown. Try to declare your constructor as below and remove default one:
#JsonCreator
public User(#JsonProperty("username") String user,
#JsonProperty("password") String pass,
#JsonProperty("name") String name,
#JsonProperty("surname") String surname,
#JsonProperty("email") String email,
#JsonProperty("company") String company,
#JsonProperty("phone") String phone,
#JsonProperty("monitorize") boolean monitorize,
#JsonProperty("createP") boolean createP,
#JsonProperty("openP") boolean openP,
#JsonProperty("admin") boolean admin) {
//your code;
}
Also, your getUsers method could look like this:
public static List<User> getUsers(String json) {
final ObjectMapper mapper = new ObjectMapper();
try {
final JavaType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, User.class);
return mapper.readValue(json, collectionType);
} catch (IOException e) {
//You should not hide exceptions. Try to log it at least.
//But probably application should not start when app configuration is missing or wrong.
e.printStackTrace();
return Collections.emptyList();
}
}

Related

Sort Array list using POJO class with particular status [duplicate]

This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 5 years ago.
I have status like "In consultation","Waiting in Queue". all the data coming from json API and add all that data into array list using Pojo class.then how to sort this array list by Consultation status.
Patient Class:
public class Patient_Get_Set implements Serializable {
private String fullName;
private String age;
private String waitTime;
private String sex;
private String patientID;
private String gender;
private String dateOfBirth;
private String phoneNo;
private String Diagnosis;
private String Email;
private String token_id;
private String priority;
private String consultation_status;
private String case_number;
public String getCase_number() {
return case_number;
}
public void setCase_number(String case_number) {
this.case_number = case_number;
}
public String getConsultation_status() {
return consultation_status;
}
public void setConsultation_status(String consultation_status) {
this.consultation_status = consultation_status;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
public String getToken_id() {
return token_id;
}
public void setToken_id(String token_id) {
this.token_id = token_id;
}
public String getConsultationstatus() {
return consultationstatus;
}
public void setConsultationstatus(String consultationstatus) {
this.consultationstatus = consultationstatus;
}
private String consultationstatus;
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getVisitorType() {
return visitorType;
}
public void setVisitorType(String visitorType) {
this.visitorType = visitorType;
}
private String visitorType;
public String getDiagnosis() {
return Diagnosis;
}
public void setDiagnosis(String diagnosis) {
Diagnosis = diagnosis;
}
private String patientJsonArray;
public void setFullName(String fullName) {
this.fullName = fullName;
}
public void setAge(String age) {
this.age = age;
}
public void setWaitTime(String waitTime) {
this.waitTime = waitTime;
}
public String getFullName() {
return fullName;
}
public String getAge() {
return age;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public String getWaitTime() {
return waitTime;
}
public void setPatientID(String patientID) {
this.patientID = patientID;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getPhoneNo() {
return phoneNo;
}
public String getPatientID() {
return patientID;
}
public void setPatientJsonArray(String patientJsonArray) {
this.patientJsonArray = patientJsonArray;
}
public String getPatientJsonArray() {
return patientJsonArray;
}
public String getGender() {
return gender;
}
}
Here is code what you can use for your custom need -
Collections.sort(list, new Comparator<Patient_Get_Set>() {
#Override
public int compare(final Patient_Get_Set object1, final Patient_Get_Set object2) {
List<String> statusList = new ArrayList();
statusList.add("consultation");statusList.add("queue"); statusList.add("waiting");
return new Integer(statusList.indexOf(object1.getConsultation_status())).compareTo(new Integer(statusList.indexOf(object2.getConsultation_status())));
}
});
add elements in statusList in same order in which order you want pojo class to be sorted.
You can use Collections to sort your array list objects.
for example your list as below.
ArrayList<Patient> list = new ArrayList<Patient>();
Then you can sort list using below code.
Collections.sort(list, new Comparator<Patient>() {
#Override
public int compare(final Patient object1, final Patient object2) {
return object1.getConsultation_status().compareTo(object2.getConsultation_status());
}
});
OR
If you are using Java version 1.8 then you can sort list using below code.
list.stream()
.sorted((object1, object2) -> object1.getConsultation_status().compareTo(object2.getConsultation_status()));
import static java.util.Comparator.comparing;
Collections.sort(list, comparing(MyObject::getStartDate));
compare data with comparing in java8 follow http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html

How do I return an object when I only have access to one field of it?

Sorry if this doesn't make sense, I can't think of a better way to phrase it. Hopefully my code illustrates my problem better.
So in this method I want to return a Myuser object. A Myuser object has about 8 string fields, one of which is userId.
As you can see, when this method is called, a string is passed in. If this string has the same value as the userId field in the Myuser object, then I want it to return the full Myuser object. Otherwise it returns null.
public Myuser getRecord(String userId) {
Connection cnnct = null;
PreparedStatement pStmnt = null;
Myuser myusr = null;
boolean result = false;
try {
cnnct = getConnection();
String preQueryStatement = "SELECT * FROM MYUSER WHERE USERID = ?";
pStmnt = cnnct.prepareStatement(preQueryStatement);
pStmnt.setString(1, userId);
ResultSet rslt = pStmnt.executeQuery();
result = rslt.next();
if (result) {
//return myusr in its entirety
}
} catch (SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (pStmnt != null) {
try {
pStmnt.close();
} catch (SQLException e) {
}
}
if (cnnct != null) {
try {
cnnct.close();
} catch (SQLException sqlEx) {
}
}
}
return myusr;
}
Edit: I thought for the heck of it I would post the Myuser class as well.
public class Myuser {
private String userid;
private String name;
private String password;
private String email;
private String phone;
private String address;
private String secQn;
private String secAns;
public Myuser(String userid, String name, String password, String email, String phone, String address, String secQn, String secAns) {
this.userid = userid;
this.name = name;
this.password = password;
this.email = email;
this.phone = phone;
this.address = address;
this.secQn = secQn;
this.secAns = secAns;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSecQn() {
return secQn;
}
public void setSecQn(String secQn) {
this.secQn = secQn;
}
public String getSecAns() {
return secAns;
}
public void setSecAns(String secAns) {
this.secAns = secAns;
}
}
You can easily use result.getXXX(field_name):
if (rslt.next()) {
myusr = new Myuser(result.getString("userid"), result.getString("name"),
result.getString("password"), result.getString("phone"),
...);
}
Note don't need to use result = rslt.next(); before if(result), you have to use directly
if (rslt.next()) {
...
}

Storing an arraylist of custom objects (which have Strings, Dates, Lists, and more as fields) in shared preferences

I am trying to save an arraylist of clients in shared preferences, however I am getting out of memory error. I am new to this and can't figure out how to do this? I looked at a lot of pages on stackoverflow, but couldn't find one that would work for me or that also had an ArrayList of custom objects, where every object contains more ArrayLists with custom objects.
client object:
public class Client implements Serializable, Comparable<Client> {
private int clientID;
private String name;
private String phone;
private String email;
private String url;
private Double turnover;
private String visitAddress;
private String visitCity;
private String visitZipcode;
private String visitCountry;
private String postalAddress;
private String postalCity;
private String postalZipcode;
private String postalCountry;
private Drawable clientImage;
private List<Contact> contactList = new ArrayList<Contact>();
private List<Project> projectList = new ArrayList<Project>();
private List<Task> taskList = new ArrayList<Task>();
private List<Order> orderList = new ArrayList<Order>();
public Client(int clientID, String Name, String Phone, String Email, String URL, Double Turnover,
String VisitAddress, String VisitCity, String VisitZipcode, String VisitCountry,
String PostalAddress, String PostalCity, String PostalZipcode, String PostalCountry,
List contactList, List projectList, List taskList, List orderList){
super();
this.clientID = clientID;
this.name = Name;
this.phone = Phone;
this.email = Email;
this.url = URL;
this.turnover = Turnover;
this.visitAddress = VisitAddress;
this.visitCity = VisitCity;
this.visitZipcode = VisitZipcode;
this.visitCountry = VisitCountry;
this.postalAddress = PostalAddress;
this.postalCity = PostalCity;
this.postalZipcode = PostalZipcode;
this.postalCountry = PostalCountry;
this.contactList = contactList;
this.projectList = projectList;
this.taskList = taskList;
this.orderList = orderList;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getEmail() {
return email;
}
public String getUrl() {
return url;
}
public Double getTurnover() {
return turnover;
}
public String getVisitAddress() {
return visitAddress;
}
public String getVisitCity() {
return visitCity;
}
public String getVisitZipcode() {
return visitZipcode;
}
public String getVisitCountry() {
return visitCountry;
}
public String getPostalAddress() {
return postalAddress;
}
public String getPostalCity() {
return postalCity;
}
public String getPostalZipcode() {
return postalZipcode;
}
public String getPostalCountry() {
return postalCountry;
}
public List<Contact> getContactList(){
return contactList;
}
public List<Project> getProjectList(){
return projectList;
}
public List<Task> getTaskList(){
return taskList;
}
public List<Order> getOrderList() {
return orderList;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setEmail(String email) {
this.email = email;
}
public void setUrl(String url) {
this.url = url;
}
public void setTurnover(Double turnover) {
this.turnover = turnover;
}
public void setVisitAddress(String visitAddress) {
this.visitAddress = visitAddress;
}
public void setVisitCity(String visitCity) {
this.visitCity = visitCity;
}
public void setVisitZipcode(String visitZipcode) {
this.visitZipcode = visitZipcode;
}
public void setVisitCountry(String visitCountry) {
this.visitCountry = visitCountry;
}
public void setPostalAddress(String postalAddress) {
this.postalAddress = postalAddress;
}
public void setPostalCity(String postalCity) {
this.postalCity = postalCity;
}
public void setPostalZipcode(String postalZipcode) {
this.postalZipcode = postalZipcode;
}
public void setPostalCountry(String postalCountry) {
this.postalCountry = postalCountry;
}
public Drawable getClientImage() {
return clientImage;
}
public void setClientImage(Drawable clientImage) {
this.clientImage = clientImage;
}
public int getClientID() {
return clientID;
}
public void setClientID(int clientID) {
this.clientID = clientID;
}
underlying project object: (also with list of custom objects)
public class Project implements Serializable, Comparable<Project>{
private String clientName;
private String projectName;
private String projectDiscription;
private String projectStatus;
private GregorianCalendar projectDate;
private List<TimeSheet> projectTimeRegestrationList = new ArrayList<>();
private List<WorkOrder> workOrderList = new ArrayList<WorkOrder>();
public Project(String clientName, String projectName, String projectDiscription, String projectStatus, GregorianCalendar projectDate, List projectTimeRegestrationList, List workOrderList) {
this.projectName = projectName;
this.projectDiscription = projectDiscription;
this.projectStatus = projectStatus;
this.projectDate = projectDate;
this.clientName = clientName;
this.projectTimeRegestrationList = projectTimeRegestrationList;
this.workOrderList = workOrderList;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDiscription() {
return projectDiscription;
}
public void setProjectDiscription(String projectDiscription) {
this.projectDiscription = projectDiscription;
}
public String getProjectStatus() {
return projectStatus;
}
public void setProjectStatus(String projectStatus) {
this.projectStatus = projectStatus;
}
public GregorianCalendar getProjectDate() {
return projectDate;
}
public void setProjectDate(GregorianCalendar projectDate) {
this.projectDate = projectDate;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public List<TimeSheet> getProjectTimeRegestrationList() {
return projectTimeRegestrationList;
}
public List<WorkOrder> getWorkOrderList() {
return workOrderList;
}
now my specific question: is it possible to save this inside shared preferences, if yes, how do i do this, if no, how should i then locally safe this data.
you can use json to store them as a string in sharedpreference
for example if you want to store an object of your client class contaning a url ,country and phone
just write the following
try {
JSONArray jsonArray=new JSONArray();
JSONObject object1=new JSONObject();
object1.put("url","myurl");
object1.put("phone","myphonenumber");
object1.put("country","mycountry");
jsonArray.put(object1);
//adding another object
JSONObject object2=new JSONObject();
object2.put("url","anotherurl");
object2.put("phone","anotherphonenumber");
object2.put("country","anothercountry");
jsonArray.put(object2);
} catch (JSONException e) {
e.printStackTrace();
}
and than simply save it as a string by calling jsonArray.toString() in sharedpreference
and if your object that you want to store contains a list of another object you can easily save like for example if you have a url, country, and phone number which you want to save plus a list of another object containing the first and lastname you can save list as json object itself just like you did for the url,phone number and country as shown below
try {
JSONArray jsonArray=new JSONArray();
JSONObject object1=new JSONObject();
object1.put("url","myurl");
object1.put("phone","myphonenumber");
object1.put("country","mycountry");
//saving a list of another object contaning firstname and lastname
JSONArray listArray=new JSONArray();
//supposing your list contains 10 objects
for(int f=0;f<10;f++){
JSONObject listobject=new JSONObject();
listobject.put("firstname","myfirstname");
listobject.put("lastname","mylastname");
listArray.put(listobject);
}
//now the list array we added as an object of the jsonArray variable
JSONObject object2=new JSONObject(); //object2 containing the list of firstname and lastname
object2.put("list",listArray.toString());
jsonArray.put(object1);//object1 contains the url, phone, and country
jsonArray.put(object2);//object2 contains the list of firstname and lastname
} catch (JSONException e) {
e.printStackTrace();
}

Infinite Recursion with Jackson JSON ResponseBody and Spring-MVC (non-bidireccional)

I'm testing a new rest service that I've implemented. This service returns a Json through #ResponseBody. In each controller, I return the same structure:
-code
-message
-list of objects
The object that i return is a message with a list which extends to its parent class getting the code and message. These are my classes:
private class SimpleMessage{
ResponseCode code;
String message;
public ResponseCode getCode() {
return code;
}
public void setCode(ResponseCode code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void setCodeAndMessage(ResponseCode code, String message){
this.code = code;
this.message = message;
}
}
private class MessageResponse<T> extends SimpleMessage{
List<T> list;
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
If I try to return the SimpleMessage, it works fine. But the problem is relative to the MessageResponse class. If I return this class with an instance of the class User for example in the list, it runs in an infinite loop until I stop the tomcat server.
This is my controller
public #ResponseBody SimpleMessage isUserSuscribed(#RequestBody String data){
MessageResponse<User> msg = new MessageResponse<User>();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
JsonNode node;
try {
node = mapper.readTree(data);
BasicUser manager = mapper.convertValue(node.get("manager"), BasicUser.class);
User user = userService.getUserByOpenid(mapper.convertValue(node.get("openid"), String.class));
msg.setList(Lists.newArrayList(user));
} catch (JsonProcessingException e) {
log.debug(e.toString());
msg.setCodeAndMessage(ResponseCode.error, "Malformed JSON \n" + e.getMessage());
} catch (IOException e1) {
log.debug(e1.toString());
msg.setCodeAndMessage(ResponseCode.error, "Application error");
}
msg.setCodeAndMessage(ResponseCode.success, "success");
return msg;
}
The User class:
public class User extends AbstractPropertySearcher{
private String username;
private String password;
private String email;
private Boolean active;
private String metadata;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getMetadata() {
return getMeta().getMetadata();
}
public void setMetadata(String metadata) {
this.metadata = metadata;
}
public enum KnownUsers{
TEST, SYSTEM
}
public User() {
this.roles = new HashSet<String>();
this.profiles = new HashSet<String>();
this.geoareas = new HashSet<String>();
this.properties = new HashSet<Property>();
}
public User(String username) {
this.username = username;
this.roles = new HashSet<String>();
this.profiles = new HashSet<String>();
this.geoareas = new HashSet<String>();
this.properties = new HashSet<Property>();
}
public User(User o) {
try{
PropertyUtils.copyProperties(this, o);
}
catch (Exception e) {
e.printStackTrace();
}
}
private Set<String> roles;
private Set<String> profiles;
private Set<String> geoareas;
public Set<String> getRoles() {
return roles;
}
public void setRoles(Set<String> roles) {
this.roles = roles;
}
public Set<String> getProfiles() {
return profiles;
}
public void setProfiles(Set<String> profiles) {
this.profiles = profiles;
}
public Set<String> getGeoareas() {
return geoareas;
}
public void setGeoareas(Set<String> geoareas) {
this.geoareas = geoareas;
}
private Set<Property> properties;
private Map<String,Property> mappedProperties;
public Map<String,Property> getMappedProperties(){
if(mappedProperties==null){
mappedProperties = new HashMap<String,Property>();
for(Property prop : getProperties()){
mappedProperties.put(prop.getProperty(),prop);
}
}
return mappedProperties;
}
public Property getPropertyByName(KnownProperty knownProperty) throws PropertyNotFoundException{
return getPropertyByName(knownProperty.getPropertyName());
}
public void setProperty(Property property){
setProperty(properties, property);
}
public boolean hasRole(Role.KnownRoles role){
return roles.contains(role.name());
}
public Set<Property> getProperties() {
return properties;
}
public void setProperties(Set<Property> properties) {
this.properties = properties;
}
public String toString(){
return getUsername();
}
public int hashCode(){
return toString().hashCode();
}
public boolean equals(Object o){
if(o instanceof User){
return getUsername().equals(((User) o).getUsername());
}
return false;
}
public Property getBannerProperty() throws PropertyNotFoundException{
return AbstractPropertySearcher.getPropertyByName(getProperties(), KnownProperty.BANNER.getPropertyName());
}
private Metadata meta;
public Metadata getMeta(){
if(meta == null){
meta = new Metadata(metadata);
}
return meta;
}
public enum KnownMetaProperty{
REGISTRATION_DATE, LAST_ACCESS_DATE
}
public String getRegistrationDate(){
return getMeta().getVariable(KnownMetaProperty.REGISTRATION_DATE.name());
}
public String getLastAccessDate(){
return getMeta().getVariable(KnownMetaProperty.LAST_ACCESS_DATE.name());
}
}
This is the error in the log:
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.StackOverflowError
Could anybody help me with this issue?
Thank you

Struts2 How to get selected value from dropdownlist to other jsp page

This is register.jsp page. Here setting the countryList in dropdownlist from Action class
<s:select name="country" list="countryList" listKey="countryId" listValue="countryName" headerKey="0" headerValue="Country" label="Select a country">
Here I am getting all the list from Action;
when I submit action , I am getting key instead of value on success.jsp.
<s:property value="country"/>
Here I am getting selected key like 0,1,2 instead of country value.
My Action class
public class RegisterAction extends ActionSupport {
private List<String> communityList;
private List<Country> countryList;
private String country;
private String userName;
private String password;
private String gender;
private String about;
private String[] community;
private boolean mailingList;
public String execute() {
return SUCCESS;}
public String populate(){
communityList = new ArrayList<String>();
countryList = new ArrayList<Country>();
countryList.add(new Country(1,"India"));
countryList.add(new Country(2,"US"));
countryList.add(new Country(3,"UK"));
communityList.add("JAVA");
communityList.add(".NET");
communityList.add("SOA");
community=new String[]{"JAVA",".NET"};
mailingList = true;
return "populate";
}
public List<String> getCommunityList() {
return communityList;
}
public void setCommunityList(List<String> communityList) {
this.communityList = communityList;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAbout() {
return about;
}
public void setAbout(String about) {
this.about = about;
}
public String[] getCommunity() {
return community;
}
public void setCommunity(String[] community) {
this.community = community;
}
public boolean isMailingList() {
return mailingList;
}
public void setMailingList(boolean mailingList) {
this.mailingList = mailingList;
}
public List<Country> getCountryList() {
return countryList;
}
public void setCountryList(List<Country> countryList) {
this.countryList = countryList;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Country.java
public class Country {
private String countryName;
private int countryId;
public Country(){}
public Country(int countryId,String countryName){
this.countryId=countryId;
this.countryName=countryName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public int getCountryId() {
return countryId;
}
public void setCountryId(int countryId) {
this.countryId = countryId;
}
}
You can get the value from the request object.
HttpServletRequest request = (HttpServletRequest)(ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST));
Country value = (Country)request.getParameter("country");
use listKey="countryName" listValue="countryName" in this way you will receive value

Categories

Resources