从servlet调用时jQuery不显示在JSP页面上 [英] jQuery not displaying on JSP page when called from servlet

查看:102
本文介绍了从servlet调用时jQuery不显示在JSP页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery的新手,正在尝试向现有jsp页面(从servlet调用)中添加一个简单的jQuery选项卡组件. 如果我直接访问该页面,则会显示jQuery组件,但是当从servlet调用该页面时,将显示该页面,但jQuery效果不起作用.看来jQuery永远不会被调用.

Hi I'm fairly new to jQuery and am trying to add a simple jQuery tab component to an existing jsp page which is called from a servlet. If I access the page directly, the jQuery components appear but when the page is called from the servlet the page displays but the jQuery effects dont work. It appears jQuery never gets called.

下面的示例代码:

我只是得到以下内容,而不是制表符效果

I am just getting the following instead of the tabs effect

*Tabs

    * First
    * Second
    * Third

This is tab one
This is tab two.
This is tab three*

任何人都可以帮忙,我已经在网上搜索了几天以寻找答案,但是还没有运气.任何帮助将不胜感激

Can anybody help, I have searched the web for days looking for answer but havent had any luck. Any help would be very much appreciated

我添加了一个弹出窗口,该弹出窗口在直接从浏览器直接调用页面时会出现,但是在从servlet调用页面时不会被调用

I have added an popup which appears when page called directly from browser but doesnt get called when page called from servlet

public class SBServlet extends HttpServlet {

    public SBServlet()
    {
        super();
    }  


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        // redirecting to test.jsp
        getServletContext().getRequestDispatcher("/JSP/test.jsp").forward(request,response);        

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
          doPost(request, response);    
    }


}

test.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" href="../css/hot-sneaks/jquery-ui-1.8.10.custom.css" rel="stylesheet" />  
<script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.10.custom.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    alert('jQuery is accessible');
    $("#tabs").tabs();
  });


</script>


<title>Jquery test</title>
</head>
<body>

<h2 class="demoHeaders">Tabs</h2>
  <div id="tabs">

    <ul>
      <li><a href="#tabs-1">First</a></li>
      <li><a href="#tabs-2">Second</a></li>
      <li><a href="#tabs-3">Third</a></li>
    </ul>

      <div id="tabs-1">This is tab one</div>
      <div id="tabs-2">This is tab two.</div>
      <div id="tabs-3">This is tab three</div>
  </div>
</body>
</html>

推荐答案

好吧,我正在使用tomcat服务器,因此它的localhost:8080/Test/JSP/test.jsp与jQuery一起显示可以.从servlet中,我正在调用初始jsp文件,该文件将调用servlet,该servlet会转发到test.jsp. URL是localhost:8080/Test/SBServlet?handler = login

Well I'm using tomcat server so its localhost:8080/Test/JSP/test.jsp which jQuery displays ok with. From the servlet I'm calling initial jsp file which calls servlet which forwards to test.jsp. THe URL is localhost:8080/Test/SBServlet?handler=login

您已声明相对于当前请求URL的脚本URL.

You've declared the script URLs relative to the current request URL.

<script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.10.custom.min.js"></script>

如果您使用 Firebug 调试了HTTP流量,您将注意到在servlet URL上脚本是从http://localhost:8080/js/jquery-1.4.4.min.jshttp://localhost:8080/js/jquery-ui-1.8.10.custom.min.js加载,它们显然返回未找到的404页.

If you have debugged the HTTP traffic with for example Firebug, you would have noticed that on the servlet URL the scripts are been loaded from http://localhost:8080/js/jquery-1.4.4.min.js and http://localhost:8080/js/jquery-ui-1.8.10.custom.min.js which each obviously returns a 404 page not found.

您要从上下文路径/Test加载它们.您需要修复脚本URL,以便它们相对于域根而不是当前请求URL.

You want to load them from context path /Test. You need to fix the script URLs so that they are relative to the domain root instead of the current request URL.

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-ui-1.8.10.custom.min.js"></script>

这将最终以生成的HTML格式出现

This would then end up in the generated HTML as

<script type="text/javascript" src="/Test/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="/Test/js/jquery-ui-1.8.10.custom.min.js"></script>

这篇关于从servlet调用时jQuery不显示在JSP页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆