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

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.

Related

How to bind nested List object to JSP form?

Hello fellow programmers,
I am writing a Spring MVC application for students to do an online assessment with multiple choice questions. An admin should be able to create assessments, so I created this object structure:
#Entity
#Table(name = "assessment")
public class Assessment {
private List<Question> questions;
// getter and setter
}
#Entity
#Table(name = "question")
public class Question {
private String questionText;
private List<Answer> answers;
// getters and setters
}
#Entity
#Table(name = "answer")
public class Answer {
private String answerText;
private boolean isCorrect;
// getters and setters
}
Now, I am using a JSP form on the admin page:
Controller
#RequestMapping(value = "/add/assessment", method = RequestMethod.GET)
public String addAssessments(Model model) {
model.addAttribute("assessmentModel", new Assessment());
return "admin-assessments-create";
}
JSP form
<form:form method="POST" modelAttribute="assessmentModel">
<form:input path="questions[0].questionText" type="text"/> <!-- this is working-->
<form:radiobutton path="questions[0].answers[0].isCorrect"/> <!-- not working-->
<form:input path="questions[0].answers[0].answerText"/>
<button class="btn" type="submit">Submit</button>
</form:form>
When I go to this page I receive the following error:
org.springframework.beans.NotReadablePropertyException:
Invalid property 'questions[0].answers[0].isCorrect' of bean class [com.johndoe.model.Question]:
Bean property 'questions[0].answers[0].isCorrect' is not readable or has an invalid getter method:
Does the return type of the getter match the parameter type of the setter?
I checked all the getters and setters but those are perfectly fine.
Question:
how do I avoid the NotReadablePropertyException and thus bind the nested Answer List to my form?
Use
<form:radiobutton path="questions[0].answers[0].correct"/>
and it will be working.
Why? For boolean fields you have to adapt the get/set paradigm to "is"XYZ(). For the EL expression, you have to drop the "is" in front of the method that accesses the current value of the field, pretty much the same way, you would do with "get" / "set".

How to access an object via a method `get()` in jsp?

I have an container object that contains a google/Guava Optional and I want to access the content of this Optinal in jsp.
import com.google.common.base.Optional;
public class Container {
private Optional<User> user;
public Optional<User> getUser(){return this.user;}
}
public class User{
private String name;
public String getName() {return this.name;}
}
A Optional has a method get() to obtain the inner object. http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html#get%28%29
I have already tried (${container} ins an instance of Container):
<c:out value="${container.user.name}" />
<c:out value="${container.user.get.name}" />
<c:out value="${container.user..name}" />
none of them work (Tomcat 7.42). Does anybody has an idea how to solve this, without adding a new property to the container (getUser2(){return this.user.get();})?
Thanks to Sotirios Delimanolis
since Servlet 3.0 / JSP 2.2 one can use
<c:out value="${container.user.get().name}" />

Accessing bean properties in JSF 2.0

I have the following model code, which I am supposed to use.
public class Client extends User {
private String userName;
public Client(String firstName, String lastName, String userName){
super(firstName, lastName);
this.userName = userName;
}
//getters and setters
}
public abstract class User {
String firstName;
String lastName;
//getters and setters
}
Now I have created the following bean:
#ManagedBean(name = "client")
#SessionScoped
public class ClientBean implements Serializable {
private final long serialVersionUID = 1L;
private Client client;
public Client getClient(){
return client;
}
public void setClient(Client client){
this.client = client;
}
}
Now I want to set the clients' firstName using this bean in an xhtml page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Register as a client</title>
</head>
<body>
<h:form>
First Name:<h:inputText value="#{???}"></h:inputText>
<br/>
<h:commandButton value="Register" action="registered?faces-redirect=true"/>
</h:form>
</body>
</html>
Now my question is: How do I access the clients' firstName? Am I supposed to create a new bean that represents user as well and extend it in ClientBean? (If so, what's the use of having model code at all? I would have double code everywhere?) Or is there any other simpler way to implement this in JSF 2.0?
You will need the following for the page to display the last name correctly.
-- The class User must have a constructor as below along with getters and setters for firstname and lastname.
public User (String firstName, String lastName)
-- Public getter and setter method for username in Client class.
-- In the ClientBean class, I would suggest you change the name to clientBean. Also, change the getter and setter method to public instead of private. You would need to create an object of type client and initialize the client object to some value if you need to display it on the screen. In the code provided, you are not creating an object or giving any value to any of the name properties.
-- In the JSF page, you can access the values using "#{clientBean.client.firstName}"
Make getter and setter public
public Client getClient(){
return client;
}
public void setClient(Client client){
this.client = client;
}
If your Client is null, simply instantiate it.
private Client client = new Client();
You would take this approach with a managed bean and a pojo if you want to for instance persist the values in a database, or do some other magic stuff, like calling a webservice.
To access the first name you write #{client.client.firstName} Indeed it looks a bit awesome, so I suggest give the managed bean another name.
You could create an instance of the pojo in the managed bean:
public class ClientBean implements Serializable {
private Client client = new Client();
...
}
You could also include first name and last name in the managed bean directly, this would make sense in case you are creating the pojo during some action to save the values.
JSF doesn't press you in a corset, instead you can choose the way that fits you.

No getter method available for property for bean with name

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.

Java/Java EE : to invoke getter method from JSP

I want to invoke a getter method (returns String value) of a Java class from JSP by using "jsp:usebean", but it returns a null value. What I don't understand is why it can't return the updated value.
Can someone shed some light on this?
Should I use a Cookie to get the value from JSP?
I'm not sure what you're using (Struts, plain Servlets, etc.) but essentially you need to add an attribute to the ServletRequest like:
class Person {
private String firstName;
// other fields, getters, setters
}
public void method(HttpServletRequest httpServletRequest) {
Person p = new Person();
p.setFirstName("Obama");
httpServletRequest.setAttribute("person", p);
}
and in your JSP:
<jsp:getProperty object="person" property="firstName" />
or if you use JSTL:
<c:out value="${person.firstName}"/>
It is simple.
In java file:
package loga;
class bean{
String name;
public void setName(String Uname)
{
this.name=Uname;
}
public void getName()
{
return name;
}
In jsp file, call this method as:
<jsp:useBean id="object" class="loga.bean">
<jsp:setproperty name="object" property="Name" Value="XXXX"/>
<jsp:getProperty name="object" property="Name"/>
</jsp:usebean>
Here, the property indicates the method name of the getName() in the java class.
To pass value from other controls use param property and give name of the control.

Categories

Resources