aspose file tools*
The moose likes Struts and the fly likes I cant set any values in my Action Form for Contact object: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "I cant set any values in my Action Form for Contact object:" Watch "I cant set any values in my Action Form for Contact object:" New topic
Author

I cant set any values in my Action Form for Contact object:

Osgar ali
Greenhorn

Joined: Jan 24, 2009
Posts: 22
I am having some issue setting values for Contact object in my form bean. Please see code below:
I have 2 jsps, a.jsp and b.jsp. In a.jsp I am able to see the contact information, when I click on edit, I cant see contact information in b.jsp page
Thanks

public class StudentsForm extends ActionForm{
private String studentId;
private String firstName;
private String lastName;
private ContactVO contact;
public ContactVO getContact() {
return contact;
}
public void setContact(ContactVO contact) {
this.contact = contact;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}

public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
contact = new ContactVO();
}

}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<link href="<c:url value='main.css'/>" rel="stylesheet" type="text/css"/>
<title><fmt:message key="label.students"/></title>
</head>
<body>
<div class="titleDiv"><fmt:message key="application.title"/></div>
<h1><fmt:message key="label.students"/></h1>
<c:url var="url" scope="page" value="/studentSetUp.do">
<c:param name="dispatch" value="setUpForInsertOrUpdate"/>
</c:url>
<a href="${url}">Add New Student Contact</a>
<br/><br/>
<table class="borderAll">
<tr>
<th><fmt:message key="label.firstName"/></th>
<th><fmt:message key="label.lastName"/></th>
<th><fmt:message key="label.address"/></th>
<th><fmt:message key="label.phonenum"/></th>
<th><fmt:message key="label.email"/></th>
<th> </th>
</tr>
<c:forEach var="std" items="${students}" varStatus="status">
<tr class="${status.index%2==0?'even':'odd'}">
<td class="nowrap"><cut value="${std.firstName}"/></td>
<td class="nowrap"><cut value="${std.lastName}"/></td>
<td class="nowrap"><cut value="${std.contactVO.address}"/></td>
<td class="nowrap"><cut value="${std.contactVO.phoneNumber}"/></td>
<td class="nowrap"><cut value="${std.contactVO.email}"/></td>
<td class="nowrap">
<c:url var="url" scope="page" value="/studentSetUp.do">
<c:param name="studentId" value="${std.studentID}"/>
<c:param name="dispatch" value="setUpForInsertOrUpdate"/>
</c:url>
<a href="${url}">Edit</a>

<c:url var="url" scope="page" value="/studentProcess.do">
<c:param name="studentId" value="${std.studentID}"/>
<c:param name="dispatch" value="delete"/>
</c:url>
<a href="${url}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<c:set var="insertUpdateTitle" value="${!empty studentForm.studentId && studentForm.studentId != 0 ?'Update Student':'Add Student'}"/>
<html>
<head>
<link href="<c:url value='main.css'/>" rel="stylesheet" type="text/css"/>
<style>td { white-space:nowrap; }</style>
<title><cut value="${insertUpdateTitle}"/></title>
</head>
<body>
<div class="titleDiv"><fmt:message key="application.title"/></div>
<h1><cut value="${insertUpdateTitle}"/></h1>
<html:form action="/studentProcess">
<table>
<tr>
<td class="tdLabel"><fmt:message key="label.firstName"/>:</td>
<td><html:text property="firstName" size="40"/> <html:errors property="firstName"/></td>
</tr>
<tr>
<td class="tdLabel"><fmt:message key="label.lastName"/>:</td>
<td><html:text property="lastName" size="40"/> <html:errors property="lastName"/></td>
</tr>
<tr>
<td class="tdLabel"><fmt:message key="label.address"/>:</td>
<td><html:text property="contact.address" size="75"/> <html:errors property="contact.address"/></td>
</tr>
<tr>
<td class="tdLabel"><fmt:message key="label.phonenum"/>:</td>
<td><html:text property="contact.phoneNumber" size="15"/> <html:errors property="contact.phoneNumber"/></td>
</tr>
<tr>
<td class="tdLabel"><fmt:message key="label.email"/>:</td>
<td><html:text property="contact.email" size="15"/> <html:errors property="contact.email"/></td>
</tr>

<tr>
<td colspan="2">
<html:hidden property="studentId"/>
<input type="hidden" name="dispatch" value="insertOrUpdate"/>
<br/>
<input type="submit" value="<fmt:message key="button.label.submit"/>" class="butStnd"/>

<input type="submit" value="<fmt:message key="button.label.cancel"/>" class="butStnd" onclick="document.studentForm.dispatch.value='getAllStudents'"/>
</td>
</tr>
</table>
</html:form>
</body>
</html>
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please disable smilies and put your code in code tags--makes things much, much easier to read.

Without seeing the config it'll be hard to help--the action configuration is (generally) what determines what form is being used, how it's being initialized, etc.
Osgar ali
Greenhorn

Joined: Jan 24, 2009
Posts: 22
Thanks David,
Here is the config file:


<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

<form-beans>
<form-bean name="studentForm" type="com.uop.form.StudentsForm"/>
</form-beans>

<action-mappings>

<action
path="/studentSetUp"
name="studentForm"
type="com.uop.action.StudentContactAction"
scope="request"
parameter="dispatch">
<forward name="success" path="/studentsForm.jsp"/>
</action>

<action
path="/studentProcess"
name="studentForm"
type="com.uop.action.StudentContactAction"
scope="request"
parameter="dispatch">
<forward name="failure" path="/studentsForm.jsp"/>
<forward name="success" path="/students.jsp"/>
</action>

</action-mappings>

<message-resources parameter="MessageResources" null="false"/>

</struts-config>

My Contact VO

import java.io.Serializable;

public class ContactVO implements Serializable {
Integer contactID;
String address;
String phoneNumber;
String email;
public ContactVO(Integer contactID, String address, String phoneNumber, String email) {
super();
this.contactID = contactID;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
}
public ContactVO(){
this.contactID=0;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getContactID() {
return contactID;
}
public void setContactID(Integer contactID) {
this.contactID = contactID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

}



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please disable smilies and put your code in code tags--makes things much, much easier to read.

You still don't provide the action code that initializes the action form. Do you understand why it's important to include that? Without that code we have no way of knowing if the action is initializing the action form from the ID provided in the edit link.

In the missing action code do you retrieve the appropriate entity from the database? Do you then get the entity object values into the action form?
Osgar ali
Greenhorn

Joined: Jan 24, 2009
Posts: 22
package com.uop.action;
import java.util.List;

public class StudentContactAction extends DispatchAction {
private static StudentService stdService = new StudentsDAOService();
private static ContactDAOService contactService = new ContactDAOServiceImp();

public ActionForward getAllStudents(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside getAllStudents");
populateStudents(request);
System.out.println("Forwarding Request getAllStudents");
return mapping.findForward(Constants.SUCCESS);
}

public ActionForward setUpForInsertOrUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("setUpForInsertOrUpdate");
StudentsForm studentForm = (StudentsForm)form;
if (isUpdate(request, studentForm)) {
Integer id = Integer.valueOf(studentForm.getStudentId());
StudentsVO student = stdService.getStudent(id);
BeanUtils.copyProperties(studentForm, student);
}
// prep(request);
return mapping.findForward(Constants.SUCCESS);
}

//private void prep(HttpServletRequest request) {
//request.setAttribute(Constants.CONTACTS, contactService.getAllContacts());
//}

private boolean isUpdate(HttpServletRequest request, StudentsForm stdForm) {
boolean updateFlag = true;
//if ID is null or 0 we know we are doing an insert.
String id = stdForm.getStudentId();
if (id == null || id.trim().length() == 0 || Integer.parseInt(id) == 0) {
updateFlag = false;
}
request.setAttribute("updateFlag", Boolean.valueOf(updateFlag));
return updateFlag;
}

public ActionForward insertOrUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

StudentsForm studentForm = (StudentsForm)form;
if (validationSuccessful(request, studentForm)) {
StudentsVO student = new StudentsVO();
BeanUtils.copyProperties(student, studentForm);
if (isUpdate(request, studentForm)) {
System.out.println("Update");
stdService.updateStudents(student);
} else {
System.out.println("Insert");
stdService.insertStudent(student);
}
populateStudents(request);
return mapping.findForward(Constants.SUCCESS);
} else {
// prep(request);
return mapping.findForward(Constants.FAILURE);
}
}

public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("delete");
StudentsForm studentsForm = (StudentsForm)form;
Integer id = Integer.valueOf(studentsForm.getStudentId());
stdService.deleteStudents(id);
populateStudents(request);
return mapping.findForward(Constants.SUCCESS);
}

private void populateStudents(HttpServletRequest request) {
List students = stdService.getAllStudents();
System.out.println("List Size is "+students.size());
request.setAttribute(Constants.STUDENTS, students);
// prep(request);
}

private boolean validationSuccessful(HttpServletRequest request, StudentsForm form) {
boolean isOk = true;
ActionMessages errors = new ActionMessages();
if (form.getFirstName() == null || form.getFirstName().trim().length() == 0) {
errors.add("firstName", new ActionMessage("errors.required", "First Name"));
}
if (form.getLastName() == null || form.getLastName().trim().length() == 0) {
errors.add("lastName", new ActionMessage("errors.required", "Last Name"));
}
//More Vlidation here for other fields:
if (!errors.isEmpty()) {
saveErrors(request, errors);
isOk = false;
}
return isOk;
}

}
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I find that very difficult to read without proper indentation.

Is there any particular reason you refuse to use the formatting options available to you?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(In addition, IIRC copyProperties does only a shallow copy, but I could be mis-remembering that.)

Have you verified that the student is being retrieved correctly? Have you verified that the action form is being correctly set as a result of the copyProperties call?
Osgar ali
Greenhorn

Joined: Jan 24, 2009
Posts: 22
Sorry David, I am new to java world and forum. Will make sure in future I will disable simile button.
Yes I am retrieving the values from backend. the first page gets it. It is only when I click on edit I dont see address phone and email but I do see First Name and Last Name
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay, see, I actually already answered that part. From the documentation of BeanUtilsBean.copyProperties:

Note that this method is intended to perform a "shallow copy" of the properties and so complex properties (for example, nested ones) will not be copied.


The information that the shallow attributes (first and last name) were visible was important and pertinent information and should have been included the first time--it would have saved a lot of trouble: Tell The Details. It's still not a Struts issue, though.
Osgar ali
Greenhorn

Joined: Jan 24, 2009
Posts: 22
Thanks David, yes you are right. I ran it in debug when, I copy student into studentForm it copies only first and latname ,contact object is not copied. Learned something new about shallow and deep copying.
It there any way in struts which allows me to do the deep copy.

BeanUtils.copyProperties(studentForm, student);
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In Struts? No, Struts is a web application framework. It doesn't provide "language-level" functionality.

Searching the web for java +deep copy may help, or asking in a more appropriate forum. Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I cant set any values in my Action Form for Contact object: