【问题标题】:jquery tab page not found找不到jquery标签页
【发布时间】:2017-07-24 05:32:27
【问题描述】:

我有 3 个 jquery 选项卡。当我单击主页选项卡时,Firefox 控制台显示“GET http://localhost:8080/MedAI/jsp/homeTab.jsp [404 Not Found]”。
为什么?这是 index.jsp 中的代码 sn-p

<div id="tabs">
<ul>
    <li><a href="jsp/homeTab.jsp">Home</a></li>
    <li><a href="<c:url value='jsp/geneNetworkTab' />">Gene Network</a></li>
    <li><a href="<c:url value='jsp/diseaseNetworkTab' />">Disease Network</a></li>
</ul>  
</div>
    <script type="text/javascript">
        $(document).ready(function() {
        $('#tabs')
            .tabs()
        });    
    </script>  

我的 homeTab.jsp 存在于 MedAI/WebPages/Web-INF/jsp/homeTab.jsp 的 NetBeans 项目中。它只显示“hello world”。

我尝试使用 jstl 方法和直接 url 方法,无论有无 .jsp 扩展名。没有任何效果。为什么找不到呢?

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

【问题讨论】:

    标签: javascript java jquery-ui-tabs


    【解决方案1】:

    jsp/homeTab.jsp WebPages 文件夹而不是Web-INF

    您的 jsp 应位于 WebContent 的根文件夹中,而不是位于 Web-INF 中。 Web-INF 是编译 Java 类的位置,将包含您的 lib 和您的项目描述符 web.xml

    【讨论】:

    • 是的。您可以将 JSP 放在 web-inf 中,但直接请求无法访问它们,只能通过 servlet 和转发。它可能很有用。
    • 我添加了我的 web.xml 和 dispather-servlet.xml。请注意,index.jsp 发现没问题。 redirect.jsp 位于 Web Pages 文件夹下(NetBeans 相当于 Eclipse 的 WebContent),并且只执行 "response.sendRedirect("index.htm");
    • 显然 index.htm 通过 dispather-servlet.xml 中的 indexController 和 viewResolver 映射到 /WEB-INF/jsp/index.jsp。多么糟糕的系统。为什么春天这么受欢迎?
    【解决方案2】:

    我花了几天的时间研究和反复试验,但我终于明白了。我讨厌春天!这是我用过的最令人困惑的非直观框架。

    这是我的 NetBeans 项目结构:

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    

    我用 url 模式胡闹,试图让简单的“/”工作,但做不到。不断收到 404 not found 错误。

    请注意,index.jsp 是 WEB-INF 之外的唯一 jsp。那就是你在浏览器中输入“http://localhost:8084/MedAI/”时的着陆点。

    它降落在那里是因为

    <welcome-file>index.jsp</welcome-file>
    

    dispatcher-servlet.xml

    <?xml version='1.0' encoding='UTF-8' ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
        <context:annotation-config />
        <context:component-scan base-package="com.tekknow.medai" />
        <mvc:annotation-driven />
    
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
    </beans>
    

    由于 web.xml url-pattern,任何以 *.htm 结尾的 url 都会被发送到 dispatcher-servlet。 viewResolver bean 然后说去 WEB-INF/jsp 文件夹中查找名称(减去 .htm 扩展名并加上 .jsp 扩展名)。

    index.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!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=UTF-8">
            <title>MedAI - Medical Artifical Intelligence</title>
            <script type="text/javascript" src="resources/js/jquery.js"></script>     
            <script type="text/javascript" src="resources/js/jquery-ui.js"></script>  
    </script>        
            <link href="resources/css/jquery-ui.css" rel="stylesheet" type="text/css" />
            <link href="resources/css/medai.css" rel="stylesheet"  type="text/css" />
        </head>
        <body>
        <div class="header">
            <h1>MedAI</h1>
            <span class="version">V 1.0.0</span>            
        </div>
    
        <div id="tabs">
        <ul>
            <li><a href="home.htm">Home</a></li>
            <li><a href="GeneNetwork.htm">Gene Network</a></li>
            <li><a href="DiseaseNetwork.htm">Disease Network</a></li>
        </ul>  
        </div>
            <script type="text/javascript">
                $(document).ready(function() {
                $('#tabs')
                    .tabs()
                });    
            </script>    
        </body>
    </html>
    

    注意 url 以 .htm 结尾。正如我之前所说,我尝试不使用 .htm 并在 web.xml url-pattern 中使用“/”,但它不起作用。

    home.htm、GeneNetwork.htm 和 DiseaseNetwork.htm 都有自己的控制器,类似于:

    HomeController.java

    package com.tekknow.medai.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/home.htm")
    public class HomeController {
    
        @RequestMapping(method = RequestMethod.GET)
        public String handleRequest(ModelMap modelMap) {
            System.out.println("HomeController.handleRequest");
            return "home";
        }
    }
    

    这会将它发送到 home.jsp 视图。

    home.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Home</title>
        </head>
        <body>
            <h1>MedAI Home</h1>
        </body>
    </html>
    

    然后终于在几十个小时后......

    希望这将节省你们所有人浪费的时间!

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 2020-06-04
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多