aspose file tools*
The moose likes Struts and the fly likes Need help in populating and updating values using HashMapin Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Need help in populating and updating values using HashMapin Struts" Watch "Need help in populating and updating values using HashMapin Struts" New topic
Author

Need help in populating and updating values using HashMapin Struts

Santosh Bobade
Greenhorn

Joined: May 22, 2014
Posts: 8
Hi Fellas,

I'm kinda new to struts and stuck in something. I need your help in solving the following problem:

The requiremtn is to update course name of selectd student ID.
On load of JSP Page: I'm fecthing student records and storing them in the studentList HasMap(key=studentId, value=courseName)
On JSP Page: I'm populating a dropdown with all the student IDs, use can select one from the dropdown and one disbaled text field shows that student's current course name. Now user can update this cousre name
by entering a new course name in the other text field on the same page.

Problem: On the click of update button, in Action class, i've getting selected student's course name(old one), newly entered course name. But selected studentId is coming as an "" (Empty String).
I need this id so that i can update the DB.

Please help me in solving this. Your efforts will be appreciated

Thanks
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Can you share the code piece where student drop down is defined in JSP and where it is retrieved in the Action class. And asociated configuration where the form fields are mapped to Action form (if you are using Struts 1).
In Struts 2, the field names can be directly mapped to Action class instance variables.


Regards,
Prajakta
Santosh Bobade
Greenhorn

Joined: May 22, 2014
Posts: 8
Hi Prajakta,
Thanks for the reply.

JSP Page:

<html:select property = "currCourse" onchange="populateField(this)">
<html: option value="0">- Select -</html: option>
<html: optionsCollection name="studentList" label = "studentId" value = "courseName" />
</html:select>
<br />
<br />

<label for="selectedData">Current Course Name:</label>
<html:text property="selectedData" styleId="currCourseName" maxlength="10" readonly="true" disabled="true" />
<br />
<br />


<label for="newCourseName">New Course Name:</label>
<html:text property="newCourseName" styleId="newCourseName" maxlength="15" />
<br />
<br />
=========================
Javascript:
function populateField(obj)
{
document.getElementById("currCourseName").value = obj.value;
}
=================================
Values passed in Action class to update values: Assuming user enters Science in the text field.
newCourseName = Science
selectedData = null
currCourse = Commerce (i.e. whatever use select in the dropdown)
studentId =""
===================================
In Action class to retrieve records from DB on JSP page load i'm doing this:

List<StudentRecord> studentIds= studentDao.fetchStudents();
if(studentIds != null){
studentForm.populateSelect(studentIds);
sess.setAttribute("studentList", studentIds);
}
i'm using session scope's HashMap and using it in JSP page to show studnetIds
=========================================
Here's config file:

<action path="/StudentUpdate" forward="/StudentUpdateCouseName.do" />

<action
path="/StudentUpdate"
type="com.myproject.server.struts.action.StudentUpdateBuildAction"
name="studentUpdateForm"
scope="request"
validate="false"
method = "populateSelect"
input="/pages/mn/StudentUpdateProfile.jsp">
</action>

<action
path="/StudentUpdateSubmit"
type="com.myproject.server.struts.action.StudentUpdateSubmitAction"
name="studentUpdateForm"
scope="request"
validate="true"
input="/pages/mn/StudentUpdateProfile.jsp">
<set-property property="cancellable" value="true"/>
<forward name="cancel" path="/HomePath.do" redirect="true"/>
===============================
Also have a look at populateSelect method of StudentUpdateForm, called from build action to populate hashmap.

public void populateSelect(List<Student> studentIdList){
studentIdList= new HashMap<Integer, String>();
for (Iterator it = studentIdList.iterator(); it.hasNext();) {
Student student= (Student)it.next();
studentIdList.put(student.getStudentId(),student.getCourseName());
}
}

========================
What am i doing wrong? If you need more inputs from my side i'm ready to give.

Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi,

What I can see from the code is, you have not explicitly mapped studentId anywhere.
The Javascript function sets value of course name to currCourse which gets mapped in the action form as expected.

You should have an additional hidden field as studentId in the form, set it in the java script function and map it to the action form.

Hope this helps.

Note: Next time when you put code in your post, please make sure to add code tags to make it readable.
Santosh Bobade
Greenhorn

Joined: May 22, 2014
Posts: 8
Thank you so much for the help. It's working now. I would have never done this without your help.

Thanks a BILLION!!
Santosh Bobade
Greenhorn

Joined: May 22, 2014
Posts: 8
And in my next post i will surely use proper tags to maintain readability. This was my first ever post and got it solved with your help. Thanks again
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Need help in populating and updating values using HashMapin Struts