How to display data in struts form if object has another object - struts

I'm working on struts 1.3 framework application.
I have one object which I'm setting in request attribute.
request.setAttribute("school",school);
And I'm trying to display that object through <bean:define> tag.
E.g.
School is Value Object
School school;
in school VO object I have another two object
Student student;
Teacher teacher;
And I'm trying to display value of student as well as teacher object
<bean:define id="summary" name="school" />
<bean:define id="StudentSummary" name="summary" property="student"/>
<bean:define id="TeacherSummary" name="summary" property="teacher"/>
And writing this element through tag
<bean:write name="StudentSummary" property="name" />
<bean:write name="StudentSummary" property="class" />
<bean:write name="TeacherSummary" property="name" />
But it is giving
javax.servlet.jsp.JspException: Cannot find message resources under key org.apache.struts.action.MESSAGE
what would be wrong in the code.

I have never done it using bean tag but you can do it using Expression language(EL). EL, I believe is a more standard way to do things.
Take a look at this previous post. I think it helps Link
I think in your case you can do something along the line of
<c:out value="${school.student.name}"/>
The above statement will print the value of "name", if you have a "name" property in your student object.

Related

Accessing field of object given by name JSTL

How do I access field of object which name is given by attribute in jstl?
Let's say in normal case, when it's not given by attribute I'd do it like so:
<c:forEach var='varName' items='${realFormName.listFieldName}'>
...
However since in this case I have to access field of object given by name setup code looks more like this:
<c:set var="formName" value="realFormName" />
Now i want to iterate over listFieldItem of object given by formName variable. How do i do that? These, for example don't work:
<c:forEach var='varName' items='${formName.listFieldName}'>
<c:forEach var='varName' items='${${formName}".listFieldName"}'>
So is there a simple way to do it?
Ok, i solved it. For me this worked:
<c:set var="form" value="${sessionScope[formName]}"></c:set>
It got object represented by formName from session scope and i was able to use it normally.

Checkboxes using struts

Hi I am willing to update the value of the checkbox , fetched from the Database, loaded on the JSP. I am creating a Employee Profile. The Jsp has fields Employee Name, Employee Address, Employee technical Skills.
The Employee Skills has following Checkboxes to select the following values
Checkbox1: Java Checkbox2: Dot net
For a Employee X, Does not have both the computational skills Java and Dot net in the database. I am able to fetch the record from the database to the JSP.
In the action class I am fetching the values from the database and setting them into the EmployeeForm which has the two getters and setters. I am setting these values in request also with setattribute.
In the EmployeeUpdate.jsp I am doing the following
String skillsValue1=(String)request.getAttribute("C/C++");
String skillsValue2=(String)request.getAttribute("Java");
<%if(skillsValue1!=null){ %>
<html:checkbox property="ComputationalSkill1" value="C/C++"/>
<%}else{ %>
<html:checkbox property="ComputationalSkill1" value=" "/>
<%}%>
</td>
<%if(skillsValue2!=null){ %>
<html:checkbox property="ComputationalSkill2" value="Java"/>
<%}else{ %>
<html:checkbox property="ComputationalSkill2" value=" "/>
<%}%>
</td>
The following code displays the checkboxes perfectly for the employees with any of the above skills and without skills.
I am getting the following problem here
When I uncheck the value on any field, which was checked earlier , the value is setting to null in the bean perfectly.
But when When I Check the value on any field, which was not checked earlier(to update any of the fields), There is no value set in the bean for the particular property, neither null also.
Could anyone let me know how to get the changed values of a checkbox, i,e when I change the state from uncheck to check state, the value should be set in the property.
If an employee has the skill Java, you're displaying the following checkbox:
<html:checkbox property="ComputationalSkill2" value="Java"/>
This means that on submit, if the checkbox is checked, the following parameter will be sent:
ComputationalSkill2=Java
If an employee doesn't have the skill Java, you're displaying the following checkbox:
<html:checkbox property="ComputationalSkill2" value=" "/>
This means that on submit, if the checkbox is checked, the following parameter will be sent:
ComputationalSkill2=<blank space>
What you want is generate always the same checkbox (the first one), but preselect it if the employee has the skill Java, and not preselect it if the employee doesn't have the skill Java.
This is not possible as is using the <html:checkbox> tag, because it's supposed to be bound to a boolean property. So you could instead have a property isJavaSkilled()/setJavaSkilled() in your form bean, and use
<html:checkbox property="javaSkilled"/>
Struts will pre-select the checkbox if the form's javaSkilled property is true, and leave it unchecked if it's false.
Note however, that since you have a list of skills that can be or not present, you should instead have a property getSkills()/setSkills() of type String[], and use a <html:multibox> tag.

in struts where can we find the property_name of <bean:write name="name" property="property_name" />

I am new to struts and i am viewing an example of struts project and it has a table in which it has various number of data and to obtain that data from the data base it uses the code in the place of field to gain the data from database but i didn't see the name main_actvt_name which is the property anywhere.where can i find that in the action or in the form or in the jsp file.Guys help me out

Struts 1.x tags usage question

I have a jsp page with the tags:
<logic:iterate id="var" ...
....
<bean:write name="var" property="p1" ...
etc.
And I need, on each iteration, to generate a href composed from the various bean's properties. I even need to URLEncode some of them so the link works.
Something like
<logic:iterate id="var" ...
....
<html:link action="otheraction.do?_X_
<bean:write name="var" property="p1" ...
etc
where X is generated by collecting the bean's properties; something like
String X="p1="+URLEncode(p1)+"&p2="+SimpleDateFormatof(p2)+"&p3="+p3;
How can I do that ?
Thanks in advance.
Better to make one POJO class.
1. Assign all your values to the object in Action which is being called before your jsp page comes in the picture.
2. Keep the object of POJO to request attribute.
3. Get the value from request attribtue on JSP using <bean:write> tag.

Map a property to the latest entry in NHibernate

Let's say my domain looks like this:
I have an object, Vehicle, that has an OdometerReading property.
An OdometerReading has the Miles & Date (when it was read).
I need to keep a history of all OdometerReadings for the Vehicle in the database, but don't want the entire odometer history to belong to the Vehicle object. What I would like is for the OdometerReading property map to the most recent OdometerReading entry out of the database.
I thought about mapping the whole collection of OdometerReadings to the Vehicle, and having a dynamic property called CurrentOdometerReading that would order them and return the latest one, but I don't need the whole collection under the Vehicle in my domain, and it seems like I would be getting more data out of the database than I need.
Is that possible with NHibernate? How would I map such a thing?
There are a few ways of doing this depending on what you want your domain model to look like. My preferred choice is to use a custom collection type, for example IOdometerReadingCollection, which you can add extra methods to. In this case it might be something like:
public interface IOdometerReadingCollection : IList<OdometerReading>
{
OdometerReading Latest { get; }
}
This way you can do:
OdometerReading reading = vehicle.OdometerReadings.Latest;
which I prefer to:
OdometerReading reading = vehicle.LatestOdometerReading;
There's lots of documentation about custom collections, which you can find with a simple google search.
If this approach isn't for you there are other options. You may be able to use a property with a formula (I'm not sure if that works with complex types?), or a regular NHibernate association where you'd have the key of the latest OdometerReading on your Vehicle mapping. As you also mentioned you could just load all the OdometerReadings, which depending on your use case might actually be fine.
I hope this helps, or at least points you in the right direction.
There is a "where" clause that you can put in your collection mapping. Check the reference documentation.
I would map the OdometerReading property as a component, then use a named query to ensure it's populated with the latest reading out of the database. (In this example, you'd have a sql-query with a name of "vehicle" that does the SQL to load the Vehicle columns along with the latest Odometer reading)
<class name="Vehicle">
<property name="Type" not-null="true"/>
<component name="OdometerReading">
<property name="Miles" />
<property name="Date" />
</component>
<loader query-ref="vehicle"/>
</class>