The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Struts - Struts2 (modeldriven) update method setting fields which do not exist on form to null
Discuss Struts2 (modeldriven) update method setting fields which do not exist on form to null in the Java Help forum on Dev Shed. Struts2 (modeldriven) update method setting fields which do not exist on form to null Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 30th, 2009, 08:47 AM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 12
Time spent in forums: 4 h 15 m
Reputation Power: 0
|
|
|
Struts - Struts2 (modeldriven) update method setting fields which do not exist on form to null
I have a modeldriven action class into which I am trying to implement crud methods. All is fine appart from update method.
The problem I have is that I am finding that if my prepopulated jsp contains all the fields corresponding to my object properties then all is good, however if I remove a field from my jsp, the update sets my object property to null. I want it to keep its value, not be set to null just because I do not have the field on the form.
here is my code:
Code:
package com.sthelens.vehicles.actions;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.sthelens.dao.api.HibernateDAOFactory;
import com.sthelens.dao.api.VehicleDAO;
import com.sthelens.dao.api.VehicleHireTypeDAO;
import com.sthelens.dao.api.VehicleLicenseTypeDAO;
import com.sthelens.vehicles.beans.Vehicle;
import com.sthelens.vehicles.beans.VehicleHireType;
import com.sthelens.vehicles.beans.VehicleLicenseType;
public class VehicleAction extends ActionSupport implements ServletRequestAware, Preparable, ModelDriven<Vehicle>{
/**
*
*/
private static final long serialVersionUID = -5789642385122502628L;
private HttpSession mSession;
private HttpServletRequest mRequest;
private HibernateDAOFactory daoFactory = new HibernateDAOFactory();
private VehicleDAO vehicleDAO = daoFactory.getVehicleDAO();
private Integer vehicleId;
private Vehicle vehicle;
private Collection<VehicleHireType> vehicleHireTypes;
private Collection<VehicleLicenseType> vehicleLicenseTypes;
private Boolean readOnly;
private Boolean editAuthorised;
public void setServletRequest(HttpServletRequest request){
mRequest = request;
mSession = mRequest.getSession();
}
public void setVehicleId(String vehicleId) {
System.out.println("PARAM ID: " + vehicleId);
this.vehicleId = Integer.parseInt(vehicleId);
}
public Integer getVehicleId() {
System.out.println("PARAM ID: " + vehicleId);
return vehicleId;
}
public void prepare() throws Exception{
System.out.println("PREPARE");
if (vehicleId == null || "".equals(vehicleId)){
vehicle = new Vehicle();
} else {
vehicle = (Vehicle) vehicleDAO.findById(vehicleId, false);
mSession.setAttribute("vehicle", vehicle);
}
VehicleHireTypeDAO hireTypeDAO = daoFactory.getVehicleHireTypeDAO();
VehicleLicenseTypeDAO licenseTypeDAO = daoFactory.getVehicleLicenseTypeDAO();
setReadOnly(false);
setEditAuthorised(false);
setVehicleHireTypes(hireTypeDAO.findAll());
setVehicleLicenseTypes(licenseTypeDAO.findAll());
mRequest.setAttribute("bannerTitle", mRequest.getServletPath());
}
public String find() {
return INPUT;
}
public Vehicle getModel(){
System.out.println("GET MODEL");
return vehicle;
}
public String execute() {
mRequest.setAttribute("vehicle",vehicle);
return SUCCESS;
}
public String save(){
System.out.println("SAVE : " + vehicle.getFleet_number());
System.out.println("SAVE ID: " + vehicleId);
vehicleDAO.makePersistent(vehicle);
mRequest.setAttribute("vehicle",vehicle);
return SUCCESS;
}
public String remove(){
vehicleDAO.makeTransient(vehicle);
return SUCCESS;
}
public Boolean getReadOnly() {
return readOnly;
}
public void setReadOnly(Boolean readOnly) {
this.readOnly = readOnly;
}
public Boolean getEditAuthorised() {
return editAuthorised;
}
public void setEditAuthorised(Boolean editAuthorised) {
this.editAuthorised = editAuthorised;
}
public Collection<VehicleHireType> getVehicleHireTypes() {
return vehicleHireTypes;
}
public void setVehicleHireTypes(Collection<VehicleHireType> vehicleHireTypes) {
this.vehicleHireTypes = vehicleHireTypes;
}
public Collection<VehicleLicenseType> getVehicleLicenseTypes() {
return vehicleLicenseTypes;
}
public void setVehicleLicenseTypes(
Collection<VehicleLicenseType> vehicleLicenseTypes) {
this.vehicleLicenseTypes = vehicleLicenseTypes;
}
}
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts.xml"/>
<package name="vehicles" namespace="/vehicles" extends="struts-default">
<interceptors>
<interceptor name="login" class="com.sthelens.interceptors.LoginInterceptor" />
<interceptor-stack name="defaultSecureStack">
<interceptor-ref name="paramsPrepareParamsStack" />
<interceptor-ref name="login" />
</interceptor-stack>
</interceptors>
<!-- Make the defaultLoginStack the default one used
for all actions unless otherwise configured. -->
<default-interceptor-ref name="defaultSecureStack" />
<global-results>
<result name="LOGIN-FAILURE" type="redirectAction">
<param name="actionName">OpenLogin</param>
<param name="namespace">/security</param>
</result>
</global-results>
<action name="OpenInventory" class="com.sthelens.vehicles.actions.impl.GenerateHtmlImpl">
<result>/WEB-INF/pages/Inventory.jsp</result>
</action>
<action name="GetHtmlInventory" class="com.sthelens.vehicles.actions.impl.GenerateHtmlImpl" method="inventory">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
<action name="findVehicle" class="com.sthelens.vehicles.actions.VehicleAction" method="find">
<result name="input">/WEB-INF/pages/Vehicle.jsp</result>
</action>
<action name="AddInventoryItem" class="com.sthelens.vehicles.actions.VehicleAction" method="add">
<result name="input">/WEB-INF/pages/Vehicle.jsp</result>
</action>
<action name="DiscardChanges">
<result type="redirectAction">
<param name="actionName">OpenInventory</param>
</result>
</action>
<action name="saveVehicle" class="com.sthelens.vehicles.actions.VehicleAction" method="save">
<result type="redirectAction">
<param name="actionName">OpenInventory</param>
</result>
<interceptor-ref name="defaultStack"/>
</action>
<action name="*" class="com.sthelens.vehicles.actions.VehicleActionSupport">
<result>/WEB-INF/pages/{1}.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../scripts/css/general.css" />
<link rel="stylesheet" type="text/css" href="../scripts/css/form.css" />
<link rel="stylesheet" type="text/css" href="../scripts/css/view.css" />
<link rel="stylesheet" type="text/css" href="../jquery/themes/redmond/ui.all.css" />
<title>
Approved Transport - <fmt:message key="title.${bannerTitle}"/>
</title>
</head>
<body>
<jsp:include page="./includes/BannerWithoutAppNav.jsp" />
<jsp:include page="./includes/NavVehicle.jsp"/>
<div id="vehicle_container" class="form_container">
<s:form theme="simple" action="saveVehicle.do">
<s:textfield name="id"/>
<s:div id="tabs">
<ul>
<li><a href="#page1"><span>Basic Details <c:out value="${tabTitleSuffix}"/></span></a></li>
<li><a href="#page2"><span>Licensing <c:out value="${tabTitleSuffix}"/></span></a></li>
<li><a href="#page3"><span>Safety & Accessibility <c:out value="${tabTitleSuffix}"/></span></a></li>
</ul>
<s:div id="page1">
<s:div cssClass="inline_block tab_page_section_50">
<br/><label>Registration Number:</label><br/>
<br/><label>Fleet Number:</label><br/>
<s:textfield id="fleet_number" name="fleet_number" cssClass="field_5" disabled="%{readOnly}"/><br/>
<br/><label>Manufacturer:</label><br/>
<s:textfield id="manufacturer_name" name="manufacturer_name" cssClass="field_20" disabled="%{readOnly}"/><br/>
<br/><label>Model:</label><br/>
<s:textfield id="model_name" name="model_name" cssClass="field_20" disabled="%{readOnly}"/><br/>
</s:div>
<s:div cssClass="inline_block tab_page_section_50">
<br/><label>Engine Size:</label><br/>
<s:textfield id="engine_size" name="engine_size" cssClass="field_10" disabled="%{readOnly}"/><br/>
<br/><label>Body Colour:</label><br/>
<s:textfield id="colour" name="colour" cssClass="field_10" disabled="%{readOnly}"/><br/>
<br/><label>Chassis Number:</label><br/>
<s:textfield id="chassis_number" name="chassis_number" cssClass="field_10" disabled="%{readOnly}"/><br/>
<br/><label>Vehicle Category:</label><br/>
<s:textfield id="category" name="category" cssClass="field_20" disabled="%{readOnly}"/><br/>
</s:div>
</s:div>
<s:div id="page2">
<s:div cssClass="inline tab_page_section_50">
<br/><label>Hire Type:</label><br/>
<s:select id="selectHireTypeElement" name="hire_type.id" list="vehicleHireTypes" listValue="name" listKey="id" emptyOption="true" cssClass="field_10" disabled="%{readOnly}"/><br/>
<br/><label>License Type:</label><br/>
<s:select id="selectLicenseTypeElement" name="license_type.id" list="vehicleLicenseTypes" listValue="name" listKey="id" emptyOption="true" cssClass="field_15" disabled="%{readOnly}"/><br/>
<br/><label>PSV Plate Number:</label><br/>
<s:textfield id="psv_plate_number" name="psv_plate_number" cssClass="field_10" disabled="%{readOnly}"/><br/>
</s:div>
<s:div cssClass="inline tab_page_section_50">
<br/><label>Date License Issued:</label><br/>
<s:textfield id="date_license_issue" name="date_license_issue" onchange="setAnnualTestDueDate(this.value, 'date_annual_test_due', 3);" cssClass="field_5" disabled="%{readOnly}"/><br/>
<br/><label>Date License Expires:</label><br/>
<s:textfield id="date_license_expiry" name="date_license_expiry" onchange="setAnnualTestDueDate(this.value, 'date_annual_test_due', 3);" cssClass="field_5" disabled="%{readOnly}"/><br/>
</s:div>
</s:div>
<s:div id="page3">
<s:div cssClass="inline tab_page_section_50">
<br/><label>Number of Seats:</label><br/>
<s:textfield id="seat_count" name="seat_count" cssClass="field_5" disabled="%{readOnly}"/><br/>
<br/><label>Number of Seat Belts:</label><br/>
<s:textfield id="seat_belt_count" name="seat_belt_count" cssClass="field_5" disabled="%{readOnly}"/><br/>
<br/><label>Seat Belt Info:</label><br/>
<s:textarea id="seat_belt_info" name="seat_belt_info" cssClass="field_max" disabled="%{readOnly}"/><br/>
</s:div>
<s:div cssClass="inline tab_page_section_50">
<br/><label>Wheelchair Capability:</label><br/>
<s:radio id="wheelchair_capability" list="#{'true':'Yes', 'false':'No'}" name="wheelchair_capability" value="wheelchair_capability" disabled="%{readOnly}"/><br/>
<br/><label>Wheelchair Capability Details:</label><br/>
<s:textarea id="wheelchair_capability_info" name="wheelchair_capability_info" cssClass="field_max" disabled="%{readOnly}"/><br/>
</s:div>
</s:div>
<br/>
</s:div>
<s:submit key="save"></s:submit>
</s:form>
</div>
</body>
<script type="text/javascript" src="../scripts/javascript/general.js"></script>
<script type="text/javascript" src="../scripts/javascript/form.js"></script>
<script type="text/javascript" src="../jquery/jquery-1.3.2.js"></script>
<script type="text/javascript" src="../jquery/ui/jquery-ui-1.7.1.custom.js"></script>
<script type="text/javascript" src="../jquery/ui/ui.tabs.js"></script>
<script type="text/javascript" src="../jquery/jquery.form.js"></script>
<script type="text/javascript" src="../jquery/jquery.validate.js"></script>
<script type="text/javascript">
$(function() {
$("#tabs").tabs();
$("#menu").accordion();
$(window).scroll(function(){
$('#menu').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});
});
});
function GetInventory(){
$.get('htmlInventory.do' + '?noCacheFix=' + new Date().getTime(),
function(data){
$("#InventorySpan").html( data );
}
)
}
function DeleteVehicle(delButton, vehicleId){
//utils.makeWindow('./NotifyRemoveWPlanAssociations.do?openInLightWindow=true&NoOfRelatedTPlans=' + noOfRelatedTPlans , 'Unable To Delete', 100, 630);
userComfirmed = confirm('The Inventory Item is about to be Deleted. Are You Sure You Want to Delete This Vehicle?')
if(userComfirmed){
var elemToDelete = delButton.parentNode.parentNode;
$.get('DeleteVehicle.do?vehicleId=' + vehicleId + '&noCacheFix=' + new Date().getTime());
DeleteElement(elemToDelete);
}
}
</script>
</html>
Code:
package com.sthelens.vehicles.beans;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "Vehicles")
public class Vehicle implements Serializable {
public final static String NAME = "vehicleX";
public Vehicle() {
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
@Column(name = "registration_number")
private String registration_number;
@Column(name = "manufacturer_name")
private String manufacturer_name;
@Column(name = "model_name")
private String model_name;
@Column(name = "engine_size")
private String engine_size;
@Column(name = "colour")
private String colour;
@Column(name = "chassis_number")
private String chassis_number;
@Column(name = "category")
private String category;
@Column(name = "seat_count")
private Integer seat_count;
@Column(name = "wheelchair_capability")
private Boolean wheelchair_capability;
@Column(name = "wheelchair_capability_info")
private String wheelchair_capability_info;
@Column(name = "hire_type_id")
private Long hire_type_id;
@ManyToOne
@JoinColumn(name="hire_type_id", insertable=false, updatable=false)
private VehicleHireType hire_type;
@Column(name = "psv_plate_number")
private String psv_plate_number;
@Column(name = "fleet_number")
private String fleet_number;
@Column(name = "seat_belt_count")
private Integer seat_belt_count;
@Column(name = "seat_belt_info")
private String seat_belt_info;
@ManyToOne
@JoinColumn(name="license_type_id")
private VehicleLicenseType license_type;
@Column(name = "date_license_issue")
private Date date_license_issue;
@Column(name = "date_license_expiry")
private Date date_license_expiry;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRegistration_number() {
return registration_number;
}
public void setRegistration_number(String registration_number) {
this.registration_number = registration_number;
}
public String getManufacturer_name() {
return manufacturer_name;
}
public void setManufacturer_name(String manufacturer_name) {
this.manufacturer_name = manufacturer_name;
}
public String getModel_name() {
return model_name;
}
public void setModel_name(String model_name) {
this.model_name = model_name;
}
public String getEngine_size() {
return engine_size;
}
public void setEngine_size(String engine_size) {
this.engine_size = engine_size;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public String getChassis_number() {
return chassis_number;
}
public void setChassis_number(String chassis_number) {
this.chassis_number = chassis_number;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Integer getSeat_count() {
return seat_count;
}
public void setSeat_count(Integer seat_count) {
this.seat_count = seat_count;
}
public Boolean getWheelchair_capability() {
return wheelchair_capability;
}
public void setWheelchair_capability(Boolean wheelchair_capability) {
this.wheelchair_capability = wheelchair_capability;
}
public String getWheelchair_capability_info() {
return wheelchair_capability_info;
}
public void setWheelchair_capability_info(String wheelchair_capability_info) {
this.wheelchair_capability_info = wheelchair_capability_info;
}
public Long getHire_type_id() {
return hire_type_id;
}
public void setHire_type_id(Long hire_type_id) {
this.hire_type_id = hire_type_id;
}
public VehicleHireType getHire_type() {
return hire_type;
}
public void setHire_type(VehicleHireType hire_type) {
this.hire_type = hire_type;
}
public String getPsv_plate_number() {
return psv_plate_number;
}
public void setPsv_plate_number(String psv_plate_number) {
this.psv_plate_number = psv_plate_number;
}
public String getFleet_number() {
return fleet_number;
}
public void setFleet_number(String fleet_number) {
this.fleet_number = fleet_number;
}
public Integer getSeat_belt_count() {
return seat_belt_count;
}
public void setSeat_belt_count(Integer seat_belt_count) {
this.seat_belt_count = seat_belt_count;
}
public String getSeat_belt_info() {
return seat_belt_info;
}
public void setSeat_belt_info(String seat_belt_info) {
this.seat_belt_info = seat_belt_info;
}
public VehicleLicenseType getLicense_type() {
return license_type;
}
public void setLicense_type(VehicleLicenseType license_type) {
this.license_type = license_type;
}
public Date getDate_license_issue() {
return date_license_issue;
}
public void setDate_license_issue(Date date_license_issue) {
this.date_license_issue = date_license_issue;
}
public Date getDate_license_expiry() {
return date_license_expiry;
}
public void setDate_license_expiry(Date date_license_expiry) {
this.date_license_expiry = date_license_expiry;
}
}
|

February 23rd, 2011, 11:50 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Time spent in forums: 2 m 31 sec
Reputation Power: 0
|
|
|
Did you find an answer for this? I am thinking about changing the set method on my object to not allow nulls or to use a DTO.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|