No getter method available for property for bean with name - java

I'm trying to populate a select with Struts. However, I'm getting this error message:
No getter method available for property label under name com.packagename.branchImpl
There is no variable called 'label' in the class in which its looking either, so I don't know how it is looking for label
form class is a very typical entity class
any suggestions as to why this error occurs?

It could be a typo with <cain:optionsCollection should be <html:optionsCollection. The last tag uses a property attribute for the collection of beans that have label and value properties. If you have a different property names in the collection bean then it could be specified using the label and value attributes of the tag. For example if you have a collection List<MyBean> and
public class MyBean implements Serializable {
private String key;
private String name;
//getters and setters for both
}
then you should use
<html:select name="querySwiftLogForm" property="branch" >
<html:optionsCollection name="querySwiftLogForm" property="branchList" label="name" value="key"/>
</html:select>
If you don't have a bean that you could use with the collection, then you could use LabelValueBean. And you need to fill the collection with the instances of that bean. Then lable and value attributes not necessary for that bean because it will use the defaults.
Also, if you use the form that is mapped to the action then name attribute is not necessary.

No getter method available for property for bean with name
I'm not posting complete error and jsp and all so just understand
First Check properties file if you are using it.
Then check properties name -- like name, password
And check the setter and getter method where you define. Check very carefully.
Then you find that setter and getter are different then it should be
For example:
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
While it would be as:
public void setname(String name)
{
this.name = name;
}
public String getname()
{
return name;
}
Just a little mistake while using setter and getter.

It's looking for a label property because that's what optionsCollection does:
This tag operates on a collection of beans, where each bean has a label property and a value property. The actual names of these properties can be configured using the label and value attributes of this tag.

Related

Export specific fields on XML extraction in Java

I am using #XmlRootElement annotation to get XML data from the database.
Right now, if I put #XmlTransient to getters, the fields are ignored.
For example:
public class Student {
private Integer studentId;
private String studentName;
#XmlTransient // Do not get student id
public Integer getStudentId() {
return this.studentId;
}
public String getStudentName() {
return this.studentName;
}
...// Setter goes here
Then, student ids are not appear in the XML file.
However, can I do this in the opposite way? I want to specify fields that I want to have in the XML file - there are too many fields in the Student class.
My server(Spring Framework 3.2.3) also uses the Jackson library, so I wonder I could use it if that is possible.
You could annotate your class with:
#XmlAccessorType(XmlAccessType.NONE)
Now you have to explicitly map properties in order to be serialized. See the Javadoc: http://docs.oracle.com/javaee/7/api/javax/xml/bind/annotation/XmlAccessType.html

Creating REST Api bean

I was wondering it there is any good way without creating separate bean and breaking DRY principle if I want to expose different values.
For example here is the "GET" bean :
class NameBean {
#XMLAttribute
String name;
int age;
String ssn;
}
But when I "POST" I wont need ssn. Do I really have to create another bean if I don't want "ssn" in the json.
class PostNameBean {
#XMLAttribute
String name;
int age;
}
You can pass nillable = true on your ssn field and clearly document that the field is not required when the user posts the data.
http://www.w3schools.com/schema/el_element.asp

Convert java.util.Set attributes with Commons BeanUtils

I have a problem converting the following request URL:
entity.name=Test&entity.window[0].size=1&entity.windows[1].size=2
to the following JavaBean:
public class House {
private String nome;
private Set<Window> windows;
// ... getters and setters ...
}
public class Window {
private int size;
// ... getters and setters ...
}
I get this error when I use BeanUtils.populate:
Property 'windows' is not indexed on bean class 'class House'
I think this problem occurs because Sets don’t have a known order to follow. So I can’t map values with indices like [0]...[1]...[2]. For my purpose, for converting request params to java.util.Set attributes, can I continue using BeanUtils with some adjustments or do I have to pick another library (which one)?

Java EL: Access nested properties

Given a class A:
public class A {
private String foo;
//getter, setters etc...
}
can one reference the foo property if an A object itself is exposed as property in a managed bean, e.g:
#ManagedBean
public class SomeBean {
private A a;
//getter, setters etc...
}
#{someBean.a.foo}
Indeed, it will work. Both for setting and obtaining the value. An input such as:
<h:inputText value="#{someBean.a.foo}" />
will both obtain the value to display it and set the new defined value by doing either
beanInstance.getA().getFoo();
or
beanInstance.getA().setFoo("newValue");
Just take into account that for this to work and avoid getting a NullPointerException, getA() cannot return null, meaning that your a object must be instantiated.

Writing a string property of an object with <html:text />

I've got an object in my form that contains various string properties.
When I want to print it in my JSP form I could do it with
<c:out value="${form.company.address}" />
which works perfectly.
Now I want to create an HTML input field. But when I write
<html:text property="company.address" />
I get an error saying
Caused by: javax.servlet.jsp.JspException: No getter method for property company.address of bean org.apache.struts.taglib.html.BEAN
Do you know how I can create an HTML input field with my company's address?
My bean's got the necessary corresponding getters and setters.
The correct way of translating this:
<c:out value="${UFForm.company.address}" />
to Struts is,
<html:text name="UFForm" property="company.address">
It means that there's a request with name UFForm with a bean that contains a method getCompany() (which I'm assuming returns a Company object) and that in turns has a getAddress() getter (if you understand what I mean). In a nutshell, the bean from request/session UFForm, the TagLib is accessing getCompany().getAddress();
PS Hope that getAddress() doesn't return a null else <html:text /> will throw an exception.
Edit To explain what I did above:
public class Company implements Serializable {
private String address;
//Setter
public void setAddress(String address) {
this.address = address;
}
//Getter
public String getAddress() { return this.address; }
}
public class UFForm implements Serializable {
private Company company;
public void setCompany(Company company) {
this.company = company;
}
public void getCompany() {
if (this.company == null) {
setCompany(new Company());
}
return this.company;
}
}
What I did above in <html:text /> is equivalent to
UFForm ufForm = ....;
String property = ufForm.getCompany().getAddress();
Your bean should have corresponding setter and getter methods.
Html form
<html:text property="name" size="10" maxlength="10">
Corresponding bean.
public class AddressForm
{
private String name=null;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
When you are getting the value for the text box with:
<html:text property="company.address" />
You are in fact saying to Struts to do:
formObject.getCompany().getAddress();
So you must have a getter for the company (which must not return null or the next operation will fail) and a setter for the address on the company object. The setters/getters must be public. This must already be the case since you can do the following with no error:
<c:out value="${UFForm.company.address}" />
Now, the thing that bugs me is this part: ${UFForm.. When you use JSTL you are accessing the form explicitly. With the <html:text> you access a property on the form implicitly. This implicit form is provided by the enclosing <html:form> tag. Do you have the <html:text> inside a <html:form>?
The form bean is located/created/exposed based on the form bean specification for the associated ActionMapping so check your mapping also.

Categories

Resources