我正在使用 SringMVC,并且正在寻找一种简单的解决方案来将 JSP 加载到另一个 JSP 文件的 div 框中。我听说过使用 Tiles,但我更喜欢使用 ajax/jquery。任何人都可以帮助我吗?我现在正试图让这个工作两天......我目前的方法是这样的:
$(document).ready(function() {
var html = '<jsp:include page="searchSites.jsp"/>';
$('#contentbox').load(html);
});
但这会在第二行引发“Uncaught SyntaxError: Unexpected token ILLEGAL”错误。我也试过 c:import 但这也不起作用。非常感谢您的帮助!
编辑:
@Controller
@RequestMapping("/search")
public class SearchController {
@Autowired private SiteService siteService;
@Autowired private SystemService systemService;
@RequestMapping(value = "")
public String displaySearch(Model model) {
return "displaySearch";
}
@RequestMapping(value = "sites", method = RequestMethod.POST )
public String displaySites(Model model, @RequestParam String searchStr) {
List<RSCustomerSiteViewDTO> sites = siteService.getSitesByName(searchStr);
model.addAttribute("sites", sites);
return "searchSites";
}
@RequestMapping(value = "systems", method = RequestMethod.POST)
public String displaySystems(Model model, @RequestParam String searchStr) {
List<RSServicedSystemViewDTO> systems = systemService.getSystemsByName(searchStr);
model.addAttribute("systems", systems);
return "searchSystems";
}
}
显示搜索.jsp
<html>
<head>
<title>Site</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="<c:url value="resources/css/style.css" />" />
<script>
$(document).ready(function() {
var html = '/crsp/search/sites';
$('#contentbox').load(html);
});
</script>
</head>
<body>
<div id="content">
<div id="searchdiv">
<form method="POST" action="search/sites">
<input type=text name=searchStr placeholder="Search Site..."
id="searchSite" class="search" />
</form>
<form method="POST" action="search/systems">
<input type=text name=searchStr placeholder="Search System..."
id="searchSystem" class="search" />
</form>
</div>
<div id="contentbox">
</div>
</div>
</body>
</html>
搜索站点.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<table>
<tr id="header">
<td>Name</td>
<td>Customer</td>
<td>City</td>
<td>Region</td>
</tr>
<c:forEach var="site" items='${sites}' varStatus="loopStatus">
<tr class="${loopStatus.index % 2 == 0 ? 'even' : 'odd'}">
<td>${site.siteName}</td>
<td>${site.customerName}</td>
<td>${site.siteCity}</td>
<td>${site.regionName}</td>
</tr>
</c:forEach>
</table>
编辑:我走近了。我必须从表单中触发这样的东西,而不是到目前为止我得到的动作,然后它会起作用:建议?
function searchSites(searchStr) {
$.ajax({
type: "POST",
url: "sites?searchStr=",
success: function(data) {
$("#contentbox").html(data);
}
});
}