java为什么Spring找不到资源文件?
我正在用Spring框架运行NetBeans。网页会显示,但未格式化。它找不到css和js文件。我做错了什么
NetBeans
网络。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"); %>
# 1 楼答案
我从零开始,终于找到了答案。唯一的问题是这句话不应该出现在网络上。xml
# 2 楼答案
我也遇到了同样的问题,我已经解决了这个问题,为静态内容创建了另一个servlet,如下所示:
将这些行添加到您的web。您的调度器后面的xml如下所示:
声明类处理程序:
完成后,您可以这样添加资源:
[编辑]:
在资源之前添加以下库,使标签
<c:url/>
# 3 楼答案
在你的索引中。换衣服
到
然后在你的调度器servlet中。xml,添加
# 4 楼答案
您应该在
dispatcher-servlet.xml
中启用default servlet handler,例如:或改用resources mapping,例如:
或注册默认的servlet请求处理程序,例如:
如果你仍然不知道怎么做,我建议你先阅读整个Spring WebMvc Documentation