External CSS does not load in web page - css

I am developing a web application using Spring and Tomcat 7.0. When I test the page in a browser it does not apply the CSS because it is unable to load the external stylesheet.
Here are my various files
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyProject</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ResourceServlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResourceServlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
dispatcher-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="userService" class="com.abdus.service.UserServiceImpl" />
<context:component-scan base-package="com.abdus.web" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>
and this is the head from jsp page
welcomePage.jsp
<meta charset="utf-8"/>
<title>Welcome</title>
<link rel="stylesheet" href="/resources/dream.css" type="text/css" />

You're saying the css url is not accessible (as opposed to it not being applied to your jsp) right? What's the response when you browse to /resources/dream.css?
I have had issues before with tomcat not serving static content with a /* servlet mapping. The way I solved it was to explicitly map each content type in web.xml, including .css files, like so:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
(where the "default" servlet is predefined by tomcat in conf/web.xml as org.apache.catalina.servlets.DefaultServlet)

It was actually a path issue. This was my directory structure
MyProject/WEB-INF/resources/dream.css
and in jsp link i had given it as href="/resources/dream.css" and i even tried href="MyProject/resources/dream.css".
But the wasy i resolved it is I made directory structure as
MyProject/resources/dream.css and
href="MyProject/resources/dream.css"

You could use jstl tags:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link href="<c:url value="/resources/style.css"/>" rel="stylesheet" type="text/css" />
Or you could use full resources path:
<base href="http://localhost:8080/myapp/" />
<link href="resources/style.css" rel="stylesheet" type="text/css" />

Related

Localhost : requested URL not found

I am trying to deploy a web app developped with spring mvc, hibernate and MySQL, I am trying to deploy it to tomcat 8. I copied the war file (bureau.war) of the app in the folder webapps when the tomcat started, the folder "bureau" is created, but, once i try to connect to the app URL : http://localhost/bureau/index, I get this error : The requested URL /bureau/index was not found on this server.
It's been 3 days, I am looking for a solution to this
This is web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/applicationContext.xml</param-value>
</context-param>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/relogin.jsp</location>
</error-page>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
and this is servlet-context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/webjars/**" location="/webjars/"/>
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="org.commune.bureau" />
</beans:beans>
When Tomcat is starting I get the following errors :
enter image description here

My css files don't get loaded

I'm going crazy why my css file isnĀ“t getting loaded. I'm using spring mvc 4 + apache tomcat 8
I already use the same code for other projects and it works normally.
Here's my project structure
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>AO</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>hibernate.xml</param-value>
</context-param>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
my servlet context :
<context:component-scan base-package="controller"/>
<mvc:annotation-driven />
<mvc:resources mapping="/RESOURCES/**" location="/RESOURCES/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
when he try to load :
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/RESOURCES/css/welcome.css" >
or
<link rel="stylesheet" type="text/css" href="/RESOURCES/css/welcome.css">
or
<link rel="stylesheet" type="text/css" href="/AO/RESOURCES/css/welcome.css">
i got this error:
GET http://localhost:8080/AO/RESOURCES/css/welcome.css 404 (Introuvable)
Try adding <mvc:default-servlet-handler /> to your mvc configuration file.
change the mapping for RESOURCES to LOWERCASE resources
Except the name of your Application,which can contain Uppercase elements, the path for resources must contain only lowercase
<mvc:resources mapping="/resources/**" location="/RESOURCES/" />
Then ref :
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/welcome.css" >
Use c:url tag of JSP Standard Tag Library to make the URL relative.
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel=stylesheet href="<c:url value='/RESOURCES/css/welcome.css'/>"
type="text/css" />
I Delete the Apache tomcat server and I add it again and finally my CSS has been loaded :)
Can somebody explain me why ?

can't apply external CSS in Struts2

I have a Java web application with struts2 framework. in my index.jsp file, I link the CSS file, but it doesn't show in my page and Error 404: not found display in Firebug. I tested one application without Struts and everything is fine. but when i add struts2 library and config struts.xml and web.xml file, CSS doesn't display :(
here is my index.jsp:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="styles/main.css" rel="stylesheet" type="text/css"/>
</head>
Of course, I add <%# taglib prefix="s" uri="/struts-tags" %> and tried <s:url> in href, but don't see any effect.
here is web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>HotelSystem</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
and here is part of my struts.xml:
<package name="default" namespace="/" extends="struts-default">
<action name="index.jsp" class="Actions.Test">
<result name="success">/index.jsp</result>
</action>
</package>
Note: Action.Test is a test action and always return SUCCESS.
You should map the .css type in the web.xml file as following!
<filter-mapping>
<filter-name>expires-filter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>

Spring MVC not showing the desired result

I am new to spring MVC and maven also. I created a maven web project in eclipse. Add dependencies for spring and run the project, but i am not getting the desired result. Here is my project structure
When i run the project then i get the result Hello World which is my index.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
But when i change to url http://localhost:8080/Spring_Maven/jsp/hello i get HTTP Status 500 error. And when i change to url http://localhost:8080/Spring_Maven/jsp/hello.jsp then i get the output ${message}
Here is my hello.jsp page
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring_Maven</display-name>
<servlet>
<servlet-name>springMaven-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMaven-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springMaven-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
here is my springMaven-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="pk.training.basitMahmood.springMaven.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Here are the list of dependencies that i added through maven
What i am doing wrong?
Finally i made it work :). But i want to share so people with new to maven and eclipse could save their time.
First i installed the m2e eclipse WTP plugin and then create the maven project as i described in my question. The thing that you need to do is to add the compiler plugin and JDK version in your pom.xml else each time you do right click on project --> Maven --> Update project you get an error in the marker tab about JRE and java EE configuration problem. You also ned to change in project facets by doing right click on project --> properties --> Project facets --> Change java version. Here is the snippet of the pom.xml.
<build>
<finalName>SpringMavenHelloWorld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Then in the web.xml i updated the servelt schema, and i found that i need to define my servet-dispatcer.xml file in both dispatcher-servlet and in the servlet context. Here is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>Spring_Maven</display-name>
<servlet>
<servlet-name>springMaven-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMaven-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Here is the structure of my project. I changed it a bit. Made a spring folder in WEB-INF and move the dispatcher servlet in it.
Although there is no lib folder in WEB-INF but everything is working fine. The thing that took my time so much is to define both servletcontext param and servelet init-param. If i only define servlet init param like
<servlet>
<servlet-name>springMaven-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMaven-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value>
</context-param>
-->
then i got the error that
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from ServletContext resource
[/WEB-INF/applicationContext.xml]; nested exception is
java.io.FileNotFoundException: Could not open ServletContext
resource [/WEB-INF/applicationContext.xml]
and if i define only the context param like
<servlet>
<servlet-name>springMaven-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value>
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMaven-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value>
</context-param>
then i got the error that
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from ServletContext resource
[/WEB-INF/springMaven-dispatcher-servlet.xml]; nested exception is
java.io.FileNotFoundException: Could not open ServletContext resource
[/WEB-INF/springMaven-dispatcher-servlet.xml]
But defining both solve the issue. Now when i do right click on my project --> run on server then i get the page Hello World with url http://localhost:8080/SpringMavenHelloWorld/ and when i change it to http://localhost:8080/SpringMavenHelloWorld/hello then i get my desired output that is
Hope this will help others too. Thank you :)

Spring MVC: CannotLoadBeanClassException for bean in classpath resource

My set up is as follows:
Tomcat 7
Spring MVC 3.0.6
Eclipse Indigo
File structur looks like this
src
- main
- java
- resources
- mdp-servlet.xml
- mdp-service.xml
- webapp
- WEB-INF
- web.xml
- test
mdp-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="ch.hszt.mdp.web" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
mdp-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd">
<bean id="userService" class="ch.hszt.mdp.service.UserServiceImpl" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- Needed to see file mdp-service.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mdp-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>testing</display-name>
<servlet>
<servlet-name>mdp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mdp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Sourcefolder:
src/main/java
src/main/resources
src/test/java
This is the error message I get when I try to start the project with Tomcat within eclipse
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mdp-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mdp-servlet.xml]
Has anyone an idea to fix this?
Thx a lot!
found the solution
<servlet>
<servlet-name>mdp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mdp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mdp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Resources