gpt4 book ai didi

javascript - 将JSP文件加载到Javascript中实现Fragments

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:18:55 25 4
gpt4 key购买 nike

我正在使用 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>

searchSites.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>

编辑:我走近了。我必须从表单中触发类似这样的东西,而不是我到现在为止得到的 Action ,然后它才会起作用:建议?

function searchSites(searchStr) {
$.ajax({
type: "POST",
url: "sites?searchStr=",
success: function(data) {
$("#contentbox").html(data);
}
});
}

最佳答案

你应该删除 JSP 标签

var html =  'searchSites.jsp';
$('#contentbox').load(html);

关于javascript - 将JSP文件加载到Javascript中实现Fragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com