我有一个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参数,因此,如果您需要将它们传递给AJAX调用的另一个HTTP请求,那么您需要遵循其他发布者的建议,以构建您的查询字符串。

关于javascript - 如何使用jquery创建JSP页面内容并使用request.getParameter?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6357145/

10-12 13:05