我有一个 JSP 页面,它加载另一个 jsp 页面的内容,它工作得很好。问题是我想通过 request.getParameter("cfgname")
到这个内容页面,这样当它被加载到主 JSP 中时,内容就完成了。 (当前代码显示 null
代替 request.getParameter
这是我的主要 JSP 页面,它获取 cfgid=41&cfgname=Abhishek&cfgdesc=test1&cfgtype=Development&filepath=files%2Fcsmclientbuckeye.xml
<title>Testing</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.get("content.jsp", addContent);
function addContent(data)
{
$("#content").html(data);
}
});
</script>
</head>
<body>
<div id="content"></div>
</body>
Content.jsp页面
<table class="csm-table" width="100%" cellspacing="0" cellpadding="4" border="0">
<tbody>
<tr>
<th width="25%">Configuration Name</th>
<td width="25%"><span id="cname"><%= request.getParameter("cfgname") %></span></td>
<th width="25%">Host Name</th>
<td width="25%"><span id="chostname">{hostname}</span></td>
</tr>
<tr> </tr>
<tr>
<th>Configuration Description</th>
<td><span id="cdesc"><%= request.getParameter("cfgdesc") %></span></td>
<th width="25%">Os Name</th>
<td width="25%"><span id="cosname">{osname}</span></td>
</tr>
<tr>
<th>Configuration Type</th>
<td><span id="ctype"><%= request.getParameter("cfgtype") %></span></td>
<th>Product Name</th>
<td><span id="cproductname"></span></td>
</tr>
<tr>
<th>Last Updated Time</th>
<td><span id="clastupdatetime"></span></td>
<th>Configuration File Name</th>
<td><span id="cfilename"><%= request.getParameter("filepath") %></span></td>
</tr>
</tbody>
</table>
你们(Kees 和 Mick)的回答都对我有帮助
我做了$.get("content.jsp?cfgname=<%= request.getParameter("cfgname") %>", addContent);
这解决了我的问题。谢谢大家
最佳答案
从您的代码示例看来,您正在调用不带参数的 content.jsp:
$.get("content.jsp", addContent);
你需要这样调用它:
$.get("content.jsp?cfgid=41&cfgname=Abhishek&cfgdesc=test1&cfgtype=Development&filepath=files%2Fcsmclientbuckeye.xml", addContent);
本质上,AJAX 调用不会立即访问您首先加载的页面的 URL 参数,因此如果您需要将它们传递给另一个 HTTP 请求,也就是 AJAX 调用,那么您需要遵循另一张海报,用于构建您的查询字符串。
关于javascript - 如何使用jquery创建jsp页面内容并使用request.getParameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6357145/