Java Spring MVC控制器一次单击即可工作2次
我正在尝试使用Spring MVC进行分页。我的控制器通过ajax接收2个参数: -每页显示多少个元素(行) -选择了哪个页面Java Spring MVC控制器一次单击即可工作2次,java,spring-mvc,pagination,Java,Spring Mvc,Pagination,我正在尝试使用Spring MVC进行分页。我的控制器通过ajax接收2个参数: -每页显示多少个元素(行) -选择了哪个页面 @Controller public class SymptomController { @Autowired private SymptomService symptomService; @RequestMapping(value = "/symptoms", method = RequestMethod.GET) public String symptomPag
@Controller
public class SymptomController {
@Autowired
private SymptomService symptomService;
@RequestMapping(value = "/symptoms", method = RequestMethod.GET)
public String symptomPage(
@RequestParam(required = false, value = "rows") Integer rows,
@RequestParam(required = false, value = "page") Integer tabNumber,
Model model) {
Page<Symptom> page = null;
if ((rows == null) && (tabNumber == null)) {
page = symptomService.findSymptomsByPage(0, 10);
System.out.println(1);
} else {
page = symptomService.findSymptomsByPage(tabNumber - 1, rows);
System.out.println(rows + " " + tabNumber);
}
int currentPage = page.getNumber() + 1;
int first = Math.max(1, currentPage - 3);
int last = Math.min(currentPage + 2, page.getTotalPages());
model.addAttribute("symptom", new Symptom());
model.addAttribute("symptomPage", page);
model.addAttribute("currentPage", currentPage);
model.addAttribute("first", first);
model.addAttribute("last", last);
return "symptoms";
}
这就解释了控制器在一次点击时工作两次。请告诉我如何使控制器正常工作。我认为浏览器发送了两次相同的请求:
- 一旦使用您编写的javascript代码
- 使用
上的本机单击事件
我通过在parrent标记上使用.on函数解决了这个问题,将arrguments'click'、'li>span',function(){}作为arrguments。现在很好用。但我感谢你的回答。我也会尝试这种方法。$(文档).ready(函数(事件){ event.preventDefault(); var pads=$('ul.pagination').children(); pads.click(函数(){ $.ajax({ 键入:“GET”, url:“/hospital/Symps”, 数据:{rows:parseInt($('#select').text()),page:parseInt($(this.text())}, }); }); });
<table id="table-methods-table" class="table table-hover table-bordered"> <thead> <tr style="background-color: #F5F5F5"> <th data-field="state" data-checkbox="false" data-align="center"></th> <th data-field="editBtn" data-align="center"><spring:message code="label.editsymptom" /></th> <th data-field="name" data-sortable="true" data-align="left"><spring:message code="label.symptoms" /></th> <th data-field="description" data-sortable="true" data-align="left"><spring:message code="label.descpiption" /></th> <!--<th data-field="operate" data-formatter="operateFormatter" data-events="operateEvents">Delete</th>--> </tr> </thead> <tbody id="tbody"> <c:forEach items="${symptomPage.content}" var="symptom"> <tr> <td class="bs-checkbox"><input name="SelectItem" type="checkbox"></td> <td><a href="#edit" data-toggle="modal"> <i class="fa fa-pencil-square-o"></i></a></td> <td>${symptom.name}</td> <td><i>${symptom.description}</i></td> </tr> </c:forEach> </tbody> </table> </div> <ul class="pagination no-margin pull-right"> <c:choose> <c:when test="${currentPage == 1}"> <li class="inactive"><a href="#">«</a></li> <li class="inactive"><a href="#"><</a></li> </c:when> <c:otherwise> <li><a href="${firstURL}">«</a></li> <li><a href="${prevURL}"><</a></li> </c:otherwise> </c:choose> <c:forEach var="i" begin="${first}" end="${last}"> <c:url var="pageUrl" value="/symptoms" /> <c:choose> <c:when test="${i == currentPage}"> <li class="active"><a href="${pageUrl}"><c:out value="${i}"></c:out></a></li> </c:when> <c:otherwise> <li><a href="${pageUrl}"><c:out value="${i}"></c:out></a></li> </c:otherwise> </c:choose> </c:forEach> <c:choose> <c:when test="${currentPage != last}"> <li><a href="${nextURL}">></a></li> <li><a href="${lastURL}">»</a></li> </c:when> <c:otherwise> <li class="inactive"><a href="#">></a></li> <li class="inactive"><a href="#">»</a></li> </c:otherwise> </c:choose> </ul> <select id="select" class="form-control" style="width: 80px; margin-top: 18px;"> <option selected="selected">10</option> <option>25</option> <option>50</option> <option>100</option> </select> <script> $(document).ready(function() { var pads = $('ul.pagination').children(); pads.click(function() { $.ajax({ type: 'GET', url : "/hospital/symptoms", data: {rows: parseInt($('#select').text()), page: parseInt($(this).text())}, }); }); }); </script>
Hibernate: select count(symptom0_.id) as col_0_0_ from symptoms symptom0_ Hibernate: select symptom0_.id as id16_, symptom0_.symptom_description as symptom2_16_, symptom0_.symptom_name as symptom3_16_ from symptoms symptom0_ limit ?, ? 10 3 Hibernate: select count(symptom0_.id) as col_0_0_ from symptoms symptom0_ Hibernate: select symptom0_.id as id16_, symptom0_.symptom_description as symptom2_16_, symptom0_.symptom_name as symptom3_16_ from symptoms symptom0_ limit ? 1 Hibernate: select count(symptom0_.id) as col_0_0_ from symptoms symptom0_ Hibernate: select symptom0_.id as id16_, symptom0_.symptom_description as symptom2_16_, symptom0_.symptom_name as symptom3_16_ from symptoms symptom0_ limit ?, ? 10 2 Hibernate: select count(symptom0_.id) as col_0_0_ from symptoms symptom0_ Hibernate: select symptom0_.id as id16_, symptom0_.symptom_description as symptom2_16_, symptom0_.symptom_name as symptom3_16_ from symptoms symptom0_ limit ? 1
<script> $(document).ready(function(event) { event.preventDefault(); var pads = $('ul.pagination').children(); pads.click(function() { $.ajax({ type: 'GET', url : "/hospital/symptoms", data: {rows: parseInt($('#select').text()), page: parseInt($(this).text())}, }); }); }); </script>