Когда я обновляю определенную запись, она обновляет первую запись в базе данных, я не могу обновить конкретную запись. Я использовал ту же техникум для добавления, она работает должным образом. Я использую jquery для вызова контроллера из jsp.jquery для вызова обновления, пожалуйста, помогите мне
Это мой бэкэнд-код
@RequestMapping(value = "/campaign", method = RequestMethod.GET)
public ModelAndView campaign(CampaignModel cmp, HttpServletRequest request)
{
UserDatabaseHelper udh=new UserDatabaseHelper();
DealerUser dUser = udh.Getuser(request.getUserPrincipal().getName());
int DealerID = dUser.getDealerId();
//model.addAttribute("isUserClickedLogin", "true");
DatabaseHelper dh = new DatabaseHelper();
List<CampaignModel> cml = dh.GetDealerCampaignModels(DealerID);
ModelMap mm = new ModelMap();
mm.addAttribute("campaingModel", cml);
VehicleDatabaseHelper vdh=new VehicleDatabaseHelper();
List<String> vms=vdh.GetVehicleMake();
mm.addAttribute("vehicleMake",vms);
List<VehicleModel> vm = vdh.GetVehiclMakeModel();
mm.addAttribute("vehicleMakeModel", vm);
ModelAndView mv = new ModelAndView("campaign", mm);
return mv;
}
@ RequestMapping (value = "/ updatecampaign", method = RequestMethod.POST)
public String UpdateCampaign(CampaignModel cmp,HttpServletRequest request)
{
int result1=0;
UserDatabaseHelper udh=new UserDatabaseHelper();
DealerUser dUser = udh.Getuser(request.getUserPrincipal().getName());
int DealerID = dUser.getDealerId();
//model.addAttribute("isUserClickedLogin", "true");
DatabaseHelper dh = new DatabaseHelper();
result1=dh.UpdateCampaignModel(DealerID,cmp.getCampaignModelID(),cmp.getCampaignName(),cmp.getType(),cmp.getDealerLocation(),cmp.getYearFrom(),cmp.getYearTo(),cmp.getMake(),
cmp.getModel().split("\\,")[0],cmp.getMiles(),cmp.getPriceFrom(),cmp.getPriceTo(),cmp.getPopularityIndex(),cmp.getAgeOfStock(),cmp.getRanking(),cmp.isEnabled());
String strResult = "New Campaign Model could not be added. Please check values and try again";
if(result1 != 0)
{
strResult = "New Campaign Model updated successfully";
}
List<CampaignModel> cml = dh.GetDealerCampaignModels(DealerID);
ModelMap mm = new ModelMap();
mm.addAttribute("campaingModel", cml);
mm.addAttribute("AddCampaignResult", strResult);
VehicleDatabaseHelper vdh=new VehicleDatabaseHelper();
List<String> vms=vdh.GetVehicleMake();
mm.addAttribute("vehicleMake",vms);
List<VehicleModel> vm = vdh.GetVehiclMakeModel();
mm.addAttribute("vehicleMakeModel", vm);
ModelAndView mv = new ModelAndView("campaign", mm);
return "redirect:/campaign";
}
`
this **This is my Jquery**
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
$('button[type=submit]').click(
function(e) {
//alert("Button Clicked:"+e.currentTarget.id);
//Prevent default submission of form
e.preventDefault();
//Remove all errors
if (e.currentTarget.id == "btnaddcampaign") {
$.post({
url: 'addcampaign',
data: $('form[name=addcmp]').serialize(),
success: function(res) {
if (res.validated) {
//Set respons
$('#resultContainer pre code').text(
$(AddCampaignResult));
$('#resultContainer').show();
} else {
//Set error messages
$.each(res.errorMessages, function(key, value) {
$('input[name=' + key + ']').after(
'<span class="error">' + value +
'</span>');
});
}
}
});
}
if (e.currentTarget.id == "btnupdatecampaign") {
$.post({
url: 'updatecampaign',
data: $('form[name=updatecmp]').serialize(),
success: function(res) {
if (res.validated) {
//Set respons
$('#resultContainer pre code').text(
$(UpdateCampaignResult));
$('#resultContainer').show();
} else {
//Set error messages
$.each(res.errorMessages, function(key, value) {
$('input[name=' + key + ']').after(
'<span class="error">' + value +
'</span>');
});
}
}
});
}
});
</script>
this is my getting and updating a record
<div class="container">
<c:forEach var="campaingmodel" items="${campaingModel}">
<div class="jumbotron">
<div class="container">
<!--
<div class="row"><label>Dealer ID</label> ${campaingmodel.getDealerID()}</div>
<div class="row"><label>Campaign Model ID</label>${campaingmodel.getCampaignModelID()}</div>
-->
<div class="row">
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Type:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getType()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Make:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getMake()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Model:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getModel()}</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Year From:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getYearFrom()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Year To:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getYearTo()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Miles:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getMiles()}</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Price From:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getPriceFrom()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Price To:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getPriceTo()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Popularity:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getPopularityIndex()}</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Age Of Stock:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getAgeOfStock()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Ranking:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getRanking()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Version:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getModelVersion()}</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Enabled:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.isEnabled()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Location:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getDealerLocation()}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-lg-5 col-md-1 col-xs-1">
<span class="ubh-jumbotron-label">Name:</span>
</div>
<div class="col-lg-1 col-md-2 col-xs-3">
<span class="ubh-jumbotron-value">${campaingmodel.getCampaignName()}</span>
</div>
</div>
</div>
</div>
*This is Update Button*
<div class="btn-group">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#${campaingmodel.getCampaignModelID()}">Update</button>
</div>
<div class="btn-group">
<c:url value="/campaignlist/${campaingmodel.getCampaignModelID()}" var="url1"></c:url>
<button type="button" class="btn btn-primary" onclick="location.href='${url1}'">Add Campaign</button>
</div>
<div class="btn-group">
<c:url value="/setlist/${campaingmodel.getCampaignModelID()}" var="url"></c:url>
<button type="button" class="btn btn-info" onclick="location.href ='${url}'">Add Adset</button>
</div>
<div class="btn-group">
<c:url value="/gettarget/${campaingmodel.getCampaignModelID()}" var="url2"></c:url>
<button type="button" class="btn btn-default" onclick="location.href='${url2}'">Add Target</button>
</div>
<div class="btn-group">
<c:url value="/geteditform/${campaingmodel.getDealerID()}" var="url9"></c:url>
<button type="button" class="btn btn-default" onclick="location.href='${url9}'">Edit Campaign</button>
</div>
</div>
</div>
<!-- FOR UPDATE -->
*Update Popup*
<div class="modal fade" id="${campaingmodel.getCampaignModelID()}">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Update Campaign</h4>
</div>
<div class="modal-body">
<form action="updatecampaign" method="POST" name="updatecmp" enctype="multipart/form-data">
<div class="form-group row">
<label class="control-label col-md-12" for="dealerID">Dealer
ID:</label>
<div class="col-md-12">
<input type="text" class="form-control" name="DealerID" title="Dealer ID should not be empty" value="${campaingmodel.getDealerID() }" readonly>
</div>
</div>
<div class="form-group row">
<label class="control-label col-md-12" for="CampaignModelID">Campaign Model
ID:</label>
<div class="col-md-12">
<input type="text" class="form-control" name="CampaignModelID" title="Dealer ID should not be empty" value="${campaingmodel.getCampaignModelID()}" readonly>
</div>
</div>
<div class="form-group row">
<div class="col-md-4">
<label class="control-label" for="yearFrom">Year
From:</label>
<div class="col-xs-3">
<select class="target" name="YearFrom" value="${campaingmodel.getYearFrom()}">
<option value="2019" ${campaingmodel.getYearFrom()=='2019' ? 'selected="true"' : ''}>2019</option>
<option value="2018" ${campaingmodel.getYearFrom()=='2018' ? 'selected="true"' : ''}>2018</option>
<option value="2017" ${campaingmodel.getYearFrom()=='2017' ? 'selected="true"' : ''}>2017</option>
<option value="2016" ${campaingmodel.getYearFrom()=='2016' ? 'selected="true"' : ''}>2016</option>
<option value="2015" ${campaingmodel.getYearFrom()=='2015' ? 'selected="true"' : ''}>2015</option>
<option value="2014" ${campaingmodel.getYearFrom()=='2014' ? 'selected="true"' : ''}>2014</option>
<option value="2013" ${campaingmodel.getYearFrom()=='2013' ? 'selected="true"' : ''}>2013</option>
<option value="2012" ${campaingmodel.getYearFrom()=='2012' ? 'selected="true"' : ''}>2012</option>
<option value="2011" ${campaingmodel.getYearFrom()=='2011' ? 'selected="true"' : ''}>2011</option>
<option value="2010" ${campaingmodel.getYearFrom()=='2010' ? 'selected="true"' : ''}>2010</option>
<option value="2009" ${campaingmodel.getYearFrom()=='2009' ? 'selected="true"' : ''}>2009</option>
<option value="2008" ${campaingmodel.getYearFrom()=='2008' ? 'selected="true"' : ''}>2008</option>
<option value="2007" ${campaingmodel.getYearFrom()=='2007' ? 'selected="true"' : ''}>2007</option>
<option value="2006" ${campaingmodel.getYearFrom()=='2006' ? 'selected="true"' : ''}>2006</option>
<option value="2005" ${campaingmodel.getYearFrom()=='2005' ? 'selected="true"' : ''}>2005</option>
</select>
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="yearTo">YearTo:</label>
<div class="col-xs-3">
<select class="dropdown-toggle" name="YearTo" value="${campaingmodel.getYearTo()}">
<option value="2019" ${campaingmodel.getYearTo()=='2019' ? 'selected="true"' : ''}>2019</option>
<option value="2018" ${campaingmodel.getYearTo()=='2018' ? 'selected="true"' : ''}>2018</option>
<option value="2017" ${campaingmodel.getYearTo()=='2017' ? 'selected="true"' : ''}>2017</option>
<option value="2016" ${campaingmodel.getYearTo()=='2016' ? 'selected="true"' : ''}>2016</option>
<option value="2015" ${campaingmodel.getYearTo()=='2015' ? 'selected="true"' : ''}>2015</option>
<option value="2014" ${campaingmodel.getYearTo()=='2014' ? 'selected="true"' : ''}>2014</option>
<option value="2013" ${campaingmodel.getYearTo()=='2013' ? 'selected="true"' : ''}>2013</option>
<option value="2012" ${campaingmodel.getYearTo()=='2012' ? 'selected="true"' : ''}>2012</option>
<option value="2011" ${campaingmodel.getYearTo()=='2011' ? 'selected="true"' : ''}>2011</option>
<option value="2010" ${campaingmodel.getYearTo()=='2010' ? 'selected="true"' : ''}>2010</option>
<option value="2009" ${campaingmodel.getYearTo()=='2009' ? 'selected="true"' : ''}>2009</option>
<option value="2008" ${campaingmodel.getYearTo()=='2008' ? 'selected="true"' : ''}>2008</option>
<option value="2007" ${campaingmodel.getYearTo()=='2007' ? 'selected="true"' : ''}>2007</option>
<option value="2006" ${campaingmodel.getYearTo()=='2006' ? 'selected="true"' : ''}>2006</option>
<option value="2005" ${campaingmodel.getYearTo()=='2005' ? 'selected="true"' : ''}>2005</option>
</select>
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="miles">Miles:</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="Miles" value="${campaingmodel.getMiles()}">
</div>
</div>
</div>
<div class="form-group row">
<div class="col-md-4">
<label class="control-label" for="priceFrom">Price From:</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="PriceFrom" value="${campaingmodel.getPriceFrom()}">
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="priceTo">Price To:</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="PriceTo" value="${campaingmodel.getPriceTo()}">
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="popularityIndex">Popularity:</label>
<div class="col-xs-3">
<select class="target" name="PopularityIndex" value="${campaingmodel.getPopularityIndex()}">
<option value="1" ${campaingmodel.getRanking()=='1' ? 'selected="true"' : ''}>1</option>
<option value="2" ${campaingmodel.getRanking()=='2' ? 'selected="true"' : ''}>2</option>
<option value="3" ${campaingmodel.getRanking()=='3' ? 'selected="true"' : ''}>3</option>
<option value="4" ${campaingmodel.getRanking()=='4' ? 'selected="true"' : ''}>4</option>
<option value="5" ${campaingmodel.getRanking()=='5' ? 'selected="true"' : ''}>5</option>
<option value="6" ${campaingmodel.getRanking()=='6' ? 'selected="true"' : ''}>6</option>
<option value="7" ${campaingmodel.getRanking()=='7' ? 'selected="true"' : ''}>7</option>
<option value="8" ${campaingmodel.getRanking()=='8' ? 'selected="true"' : ''}>8</option>
<option value="9" ${campaingmodel.getRanking()=='9' ? 'selected="true"' : ''}>9</option>
<option value="10" ${campaingmodel.getRanking()=='10' ? 'selected="true"' : ''}>10</option>
</select>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-md-4">
<label class="control-label" for="ageOfStock">Age Of Stock:</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="AgeOfStock" title="Age Of Stock should not be empty" value="${campaingmodel.getAgeOfStock()}">
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="ranking">Ranking:</label>
<div class="col-xs-3">
<select class="target" name="Ranking" value="${campaingmodel.getRanking()}">
<option value="1" ${campaingmodel.getPopularityIndex()=='1' ? 'selected="true"' : ''}>1</option>
<option value="2" ${campaingmodel.getPopularityIndex()=='2' ? 'selected="true"' : ''}>2</option>
<option value="3" ${campaingmodel.getPopularityIndex()=='3' ? 'selected="true"' : ''}>3</option>
<option value="4" ${campaingmodel.getPopularityIndex()=='4' ? 'selected="true"' : ''}>4</option>
<option value="5" ${campaingmodel.getPopularityIndex()=='5' ? 'selected="true"' : ''}>5</option>
<option value="6" ${campaingmodel.getPopularityIndex()=='6' ? 'selected="true"' : ''}>6</option>
<option value="7" ${campaingmodel.getPopularityIndex()=='7' ? 'selected="true"' : ''}>7</option>
<option value="8" ${campaingmodel.getPopularityIndex()=='8' ? 'selected="true"' : ''}>8</option>
<option value="9" ${campaingmodel.getPopularityIndex()=='9' ? 'selected="true"' : ''}>9</option>
<option value="10" ${campaingmodel.getPopularityIndex()=='10' ? 'selected="true"' : ''}>10</option>
</select>
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="enabled">Enabled:</label>
<div class="col-xs-3">
<label class="switch">
<c:choose>
<c:when test="${campaingmodel.isEnabled()==true}">
<input type="checkbox" id="ans" class="form-control" name="Enabled"
title="Enabled should not be empty" value="${campaingmodel.isEnabled()}" checked="checked"/>
</c:when>
<c:otherwise>
<input type="checkbox" id="ans" class="form-control" name="Enabled"
title="Enabled should not be empty" value="${campaingmodel.isEnabled()}"/>
</c:otherwise>
</c:choose>
<span class="slider round"></span>
</label>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label class="control-label" for="Type">Type:</label>
<div class="col-xs-3">
<select class="target" name="Type" value="${campaingmodel.getType()}" id="updtype">
<option value="ALL" ${campaingmodel.getType()=='ALL' ? 'selected="true"' : ''}>ALL</option>
<option value="NEW" ${campaingmodel.getType()=='NEW' ? 'selected="true"' : ''}>NEW</option>
<option value="OLD" ${campaingmodel.getType()=='OLD' ? 'selected="true"' : ''}>OLD</option>
</select>
</div>
</div>
<div class="col-md-4">
<label class="control-label" for="make">Make:</label>
<div class="col-xs-2">
<select id="upmakedropdown" name="Make">
<option value="ALL" selected="selected">ALL</option>
<c:forEach var="vehm" items="${vehicleMake}">
<option value="${vehm}" ${campaingmodel.getMake()==vehm? 'selected="true"' : ''}>${vehm}</option>
</c:forEach>
</select>
</div>
</div>
<div class="col-md-5">
<label class="control-label" for="model">Model:</label>
<div class="col-xs-4">
<select id="upmodeldropdown" name="Model">
<option value="ALL" selected="selected">ALL</option>
<c:forEach var="vehmm" items="${vehicleMakeModel}">
<option value="${vehmm.getMake()}.${vehmm.getModel()}" ${campaingmodel.getModel()==vehmm.getModel()? 'selected="true"' : ''}>${vehmm.getModel()}</option>
</c:forEach>
</select>
</div>
</div>
</div>
<div class="form-group row">
<label class="control-label col-md-12" for="dealerLocation">Dealer
Location:</label>
<div class="col-md-12">
<input type="text" class="form-control" name="DealerLocation" title="Dealer Location should not be empty" value="${campaingmodel.getDealerLocation()}">
</div>
</div>
<div class="form-group row">
<label class="control-label col-md-12" for="dealerLocation">Name:</label>
<div class="col-md-12">
<input type="text" class="form-control" name="CampaignName" title="Dealer Location should not be empty" value="${campaingmodel.getCampaignName()}">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<div class="form-group">
<button type="submit" class="btn btn-success" id="btnupdatecampaign" data-dismiss="modal">Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</c:forEach>
</div>