简体   繁体   中英

CSS stylesheet not available in tomcat

I created a maven-webapp project in eclipse and want to put a css stylesheet on my .jsp site, but my tomcat keeps saying that "The requested source is not available".

This is my .jsp file (also tried to add the absolute path to the file...):

 <%@ 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> 

And this is my .css file:

 @CHARSET "ISO-8859-1"; body { background-color: red; } h1 { color: navy; margin-left: 60px; } h2 { size: 220; margin-left: 60px; } #ololo { color: red; } 

When I start my tomcat 8 it looks like this: click

When I import the .css file directy into firefox, it works.

My structure is like this: 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> 

Okay, finally I found the problem. I had to add the mvc:resources mapping to my 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM