有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么Spring找不到资源文件?

我正在用Spring框架运行NetBeans。网页会显示,但未格式化。它找不到css和js文件。我做错了什么

NetBeans

enter image description here

网络。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>
        <url-pattern>/</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>

索引。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">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<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 type="text/javascript" src="resources/js/jquery.dataTables.js"></script>        
    <link href="resources/css/jquery-ui.css" rel="stylesheet" type="text/css" />
    <link href="resources/css/medai.css" rel="stylesheet"  type="text/css" />
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <div>
            <h1>MedAI</h1>
            <span class=version>V 1.0.0</span>            
    </div>

    <div id="tabs">
    <ul>
            <li><a href="<c:url value='/jsp/homeTab' />">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()
                //.addClass('ui-tabs-vertical ui-helper-clearfix');
            });    
        </script>    
    </body>
</html>

调度器servlet。xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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>

重定向。jsp

<%--
Views should be stored under the WEB-INF folder so that
they are not accessible except through controller process.

This JSP is here to provide a redirect to the dispatcher
servlet but should be the only JSP outside of WEB-INF.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("index.htm"); %>

共 (4) 个答案

  1. # 1 楼答案

    我从零开始,终于找到了答案。唯一的问题是这句话不应该出现在网络上。xml

    <url-pattern>/</url-pattern>
    
  2. # 2 楼答案

    我也遇到了同样的问题,我已经解决了这个问题,为静态内容创建了另一个servlet,如下所示:

    将这些行添加到您的web。您的调度器后面的xml如下所示:

        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet>
            <servlet-name>resources</servlet-name>
            <servlet-class>com.eproducts.servlets.DefaultServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>resources</servlet-name>
            <url-pattern>/resources/*</url-pattern>     
        </servlet-mapping>
    

    声明类处理程序:

    package com.eproducts.servlets;
    
    import java.io.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class DefaultServlet extends HttpServlet
    {   
        private static final long serialVersionUID = 1L;
    
        // Tomcat, Jetty, JBoss, and GlassFish 
        private static final String COMMON_DEFAULT_SERVLET_NAME = "default";
    
        // Resin 
        private static final String RESIN_DEFAULT_SERVLET_NAME = "resin-file";
    
        // WebLogic 
        private static final String WEBLOGIC_DEFAULT_SERVLET_NAME = "FileServlet";
    
        // WebSphere 
        private static final String WEBSPHERE_DEFAULT_SERVLET_NAME = "SimpleFileServlet";
    
    
        public String scanDefaultServlet(){
            if(this.getServletContext().getNamedDispatcher(COMMON_DEFAULT_SERVLET_NAME) != null) {
                return COMMON_DEFAULT_SERVLET_NAME;
            } else if(this.getServletContext().getNamedDispatcher(RESIN_DEFAULT_SERVLET_NAME) != null) {
                return RESIN_DEFAULT_SERVLET_NAME;
            } else if(this.getServletContext().getNamedDispatcher(WEBLOGIC_DEFAULT_SERVLET_NAME) != null){
                return WEBLOGIC_DEFAULT_SERVLET_NAME;
            } else if(this.getServletContext().getNamedDispatcher(WEBSPHERE_DEFAULT_SERVLET_NAME) != null){
                return WEBSPHERE_DEFAULT_SERVLET_NAME;
            } else {
                throw new IllegalStateException("Cannot determine what Server you currently use");
            }       
        }
    
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {
            RequestDispatcher rd = getServletContext().getNamedDispatcher(this.scanDefaultServlet());
            HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
                    public String getServletPath() {return "";}
            };
            rd.forward(wrapped, resp);
        }
    }
    

    完成后,您可以这样添加资源:

    <script type="text/javascript" src="<c:url value="/resources/public/js/jquery-2.1.1.min.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/resources/public/js/bootstrap.min.js"/>"></script>
    

    [编辑]:

    在资源之前添加以下库,使标签<c:url/>

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    
  3. # 3 楼答案

    在你的索引中。换衣服

    <script type="text/javascript" src="resources/js/jquery.js"></script>     
        <script type="text/javascript" src="resources/js/jquery-ui.js"></script>  
        <script type="text/javascript" src="resources/js/jquery.dataTables.js"></script>        
        <link href="resources/css/jquery-ui.css" rel="stylesheet" type="text/css" />
        <link href="resources/css/medai.css" rel="stylesheet"  type="text/css" />
    

    <script type="text/javascript" src="<c:url value="resources/js/jquery.js" />" ></script>     
        <script type="text/javascript" src="<c:url value="resources/js/jquery-ui.js" />" ></script>  
        <script type="text/javascript" src="<c:url value="resources/js/jquery.dataTables.js" />" ></script>        
        <link href="<c:url value="resources/css/jquery-ui.css" />" rel="stylesheet" type="text/css" />
        <link href="<c:url value="resources/css/medai.css" />" rel="stylesheet"  type="text/css" />
    

    然后在你的调度器servlet中。xml,添加

    <mvc:resources mapping="/resources/**" location="/resources/"/>
    
  4. # 4 楼答案

    您应该在dispatcher-servlet.xml中启用default servlet handler,例如:

    <?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:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:default-servlet-handler/>
    
    </beans>
    

    改用resources mapping,例如:

    <?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:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:resources mapping="/resources/**" location="/resources/"/>
    </beans>
    

    注册默认的servlet请求处理程序,例如:

    <?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:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
            <property name="defaultHandler">
                <bean class="org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler"/>
            </property>
        </bean>
    </beans>
    

    如果你仍然不知道怎么做,我建议你先阅读整个Spring WebMvc Documentation