html - CSS样式表在tomcat中不可用

标签 html css tomcat

我在 eclipse 中创建了一个 maven-webapp 元素,想在我的 .jsp 站点上放置一个 css 样式表,但是我的 tomcat 一直说“请求的源不可用”。

这是我的 .jsp 文件(也尝试将绝对路径添加到文件...):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/src/main/webapp/WEB-INF/pages/index1.css">
<link rel="stylesheet" type="text/css" href="index1.css">
<link rel="stylesheet" type="text/css" href="/WEB-INF/pages/index1.css"><title>Simple Form Demo</title>
</head>

<body>
	<h1>test</h1>
<h2>Simple Form Demosdds</h2>
	<div id="ololo" class="container">
		<div class="dropdown">
			<button class="btn btn-primary dropdown-toggle" type="button"
				data-toggle="dropdown">
				Dropdown Example <span class="caret"></span>
			</button>
			<ul class="dropdown-menu">
				<li><a href="#">HTML</a></li>
				<li><a href="#">CSS</a></li>
				<li><a href="#">JavaScript</a></li>
			</ul>
		</div>
	</div>

	<form name="myForm" action="result.jsp" method="post">
		<table>
			<tbody>
				<tr>
					<td>First Name:</td>
					<td><input type="text" name="first" value="" size="50"></td>
				</tr>
			<tbody>
				<tr>
					<td>Last Name:</td>
					<td><input type="text" name="last" value="" size="50"></td>
				</tr>
			<tbody>
				<tr>
					<td>Email:</td>
					<td><input type="text" name="email" value="" size="50"></td>
				</tr>
			<tbody>
				<tr>
					<td>Gender:</td>
					<td><input type="radio" name="gender" value="Male" />Male <input
						type="radio" name="gender" value="Female" />Female</td>
				</tr>
				<tr>
					<td><select name="state">
							<option value="">Choose a state...</option>
							<option value="IA">Ioaw</option>
							<option value="IL">Illinois</option>
							<option value="MO">Missouri</option>
							<option value="Other">Other</option>
					</select></td>
				</tr>
			</tbody>
		</table>

		<input type="reset" value="Clear" name="clear"> <input
			type="submit" value="Submit" name="submit">

	</form>
</body>
</html>

这是我的 .css 文件:

@CHARSET "ISO-8859-1";

body {
    background-color: red;
}

h1 {
    color: navy;
    margin-left: 60px;
}

h2 {
    size: 220;
    margin-left: 60px;
}

#ololo {
	color: red;
}

当我启动 tomcat 8 时,它看起来像这样: click

当我将 .css 文件直接导入 firefox 时,它起作用了。

我的结构是这样的: click

€: web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

	<display-name>Application</display-name>

	<servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>
                        org.springframework.web.servlet.DispatcherServlet
                </servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
                   org.springframework.web.context.ContextLoaderListener
                </listener-class>
	</listener>
</web-app>

mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="com.XXX.XXX.controller" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/pages/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

</beans>

最佳答案

好吧,终于找到问题所在了。我必须将 mvc:resources 映射添加到我的 mvc-dispatcher-servlet.xml。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<mvc:annotation-driven />
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<context:component-scan base-package="com.xxx.xxx.controller" />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/pages/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

</beans>

关于html - CSS样式表在tomcat中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502058/

相关文章:

javascript - 用JS创建带有onMouseOver效果的DIV

html - 某些 css 在 IE 中不起作用

css - 如何正确对齐页眉、导航、部分和页脚//HTML5 和 CSS

jquery - 从下拉菜单中获取选定元素的列表到 Django View

java - Tomcat:无法解析运行时描述符

html - 使图片行扩展以适应可用空间

html - 在范围 slider 上显示工具提示的 React 方式是什么?

CSS 页面位置问题

java - 如何将我在 tomcat 8080 端口上运行的应用程序 URL 重定向到域 www.xyz.com

java - 为什么我从 hello world SpringBoot 收到 WhiteLabel 错误?