在我的 jsp 页面中,我必须将开始日期和到期日期传递到 Controller 类中。然后使用这些日期我在 Impl 类中过滤数据。但问题是,当将日期传递到 Controller 中时,我还需要显示该数据在引导模式中
这是我在点击那些日期转到 Controller 类后页面中的 jsp 表单
<table id="default-datatable" data-plugin="DataTable"
class="table table-striped" cellspacing="0"
width="100%">
<thead>
<tr>
<th>Season</th>
<th>Start date</th>
<th>End date</th>
<th>Activate Season</th>
</tr>
</thead>
<tbody>
<c:forEach var="element" items="${seasons}">
<tr>
<form action="season.create" method="POST">
<td>${element.seasonName}</td>
<td>${element.startDate}</td>
<td>${element.endDate}</td>
<%-- <td><a data-toggle="modal"
data-id="${element.seasonId}" title="Add this item"
class="open-AddBookDialog btn btn-info"
href="#myModal">Select Offer</a></td> --%>
<td>
<input type="hidden" name="startDate"
id="startDate" value="${element.startDate}"></input>
<input type="hidden" name="expiryDate"
id="expiryDate" value="${element.endDate}"></input><input type="submit" class="btn btn-primary"
style="width: 90%; margin-top: 10px;"
name="action" value="Add" onclick="showDiv()"></td>
我的 Controller 类方法如下所示
@RequestMapping(value = "/season.create", method = RequestMethod.POST)
public String createSeason(@ModelAttribute("Offer") Offer offer, BindingResult result,@RequestParam String action,@RequestParam("startDate") String startDate,
@RequestParam("expiryDate") String expiryDate, Map<String, Object> map, HttpServletRequest request, Model model) {
System.out.println("testing -----------");
int merchantId = 11;
switch (action.toLowerCase()) {
case "add":
offerservice.getAlloffers(merchantId,startDate,expiryDate);
System.out.println("inside season.create test strat date "+startDate+"enddate"+expiryDate);
System.out.println("exDate"+expiryDate);
//map.put("offerFilterd", offerservice.getAlloffers(merchantId));
//map.put("offers", offerservice.getAlloffers(merchantId));
map.put("offers", offerservice.getAlloffers(merchantId,startDate,expiryDate));
map.put("seasons", offerservice.getAllSeasons(merchantId));
map.put("Offer", new Offer());
map.put("SeasonTypes", new SeasonTypes());
map.put("editSingleImage", false);
map.put("imageGalary", false);
map.put("video", false);
break;
case "edit":
break;
case "delete":
break;
case "search":
break;
}
//map.put("offers", offerservice.getAlloffers(merchantId));
map.put("offers", offerservice.getAlloffers(merchantId,startDate,expiryDate));
map.put("seasons", offerservice.getAllSeasons(merchantId));
map.put("Offer", new Offer());
map.put("editSingleImage", false);
map.put("imageGalary", false);
map.put("video", false);
return "create-offer";
}
这是我的 boostrap 模态,包含在 jsp 页面中。
<div id="myModal" class="modal fade" role="dialog" style="display:none;">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Select Offer</h4>
</div>
<div class="modal-body">
<div class="table-responsive">
<table id="default-datatable" data-plugin="DataTable"
class="table table-striped" cellspacing="0" width="100%">
<p>form has submitted</p>
<thead>
<tr>
<th>Offer Heading</th>
<th>Offer Description</th>
<th>Issue Date</th>
<th>Expiration date</th>
<th>Offer Discount </th>
<th>Offer Price </th>
<th>Allocated Points </th>
<th>OfferClaim LImit</th>
</tr>
</thead>
<tbody>
<c:forEach var="element" items="${offers}">
<form:form action="offer.bind" method="POST" commandName="SeasonTypes" id="formID">
<tr>
<form:hidden path="seasonId" id="bookId" class="form-control normtxtin"/>
<%-- <td><form:input path="offerId" name="offerid" value="${element.offerid}" class="form-control normtxtin"/></td> --%>
<td>${element.offerSubheading}</td>
<td>${element.offerDescription}</td>
<td>${element.issueDateTime}</td>
<td>${element.expiryDate}</td>
<td>${element.offerDiscount}</td>
<td>${element.offerPrice}</td>
<td>${element.pointsAllocated}</td>
<td>${element.offerClaimLimit}</td>
<td><input type="hidden" id="offerId" name="offerid" type="text" value="${element.offerid}"/></td>
<td><button type="submit" class="btn btn-primary" style="width:100%;font-size:16px;padding:10px;" name="action" value="Add">Select Offer</button></td>
</tr>
</form:form>
</c:forEach>
</tbody>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
这是我的java脚本代码
<script> function showDiv() { $('#myModal').modal('show') }
</script>
这是我的 DAOIMPL 方法
public List <Offer> getAlloffers(int merchantId,String sDate,String ExDate){
System.out.println("sDate"+sDate+ "EndDate"+ExDate);
Long merchantIdLng = (long) merchantId;
List<Offer> offers= sessionfactory.openSession().createQuery("From Offer where merchant ="+merchantIdLng+"and issueDateTime BETWEEN '"+sDate+"' AND'"+ ExDate+"'" ).list();
return offers;
}
但是点击按钮后它没有显示 Bootstrap 模式。如果有人能说出为什么我的代码不起作用。我需要传递日期并过滤数据并在单击按钮时以模式显示
最佳答案
这是您使用的相同代码。除了我添加了 bootstrap 和 jquery:
function showDiv(){
$('#myModal').modal();
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<input type="submit" class="btn btn-primary" id="showDivBtn"
style="width: 90%; margin-top: 10px;" name="action" value="Add" onclick="showDiv()">
<div id="myModal" class="modal fade" role="dialog" style="display:none;">
<div class="modal-dialog modal-lg">
<!- your modal contain -!>
</div>
</div>
关于javascript - 如何从 MVC Controller 类调用引导模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46783777/