CSS样式表在tomcat中不可用

时间:2016-02-19 09:43:48

标签: html css tomcat

我在eclipse中创建了一个maven-webapp项目,并希望在我的.jsp站点上放置一个css样式表,但是我的tomcat一直在说#34;请求的源代码不可用"。

这是我的.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>
&#13;
&#13;
&#13;

这是我的.css文件:

&#13;
&#13;
@CHARSET "ISO-8859-1";

body {
    background-color: red;
}

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

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

#ololo {
	color: red;
}
&#13;
&#13;
&#13;

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

当我将.css文件直接导入firefox时,它可以工作。

我的结构是这样的: click

€:web.xml:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

MVC-调度-servlet.xml中:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

好的,最后我发现了问题。我不得不将mvc:resources映射添加到我的mvc-dispatcher-servlet.xml。

&#13;
&#13;
<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>
&#13;
&#13;
&#13;