Précédent   Forum des professionnels en informatique > Java > Général Java > Spring
Spring Forum d'entraide pour le framework Spring. Avant de poster -> FAQ Spring
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 08/02/2012, 11h33   #1
Invité régulier
 
Homme Romain
Etudiant Java
Inscription : octobre 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme Romain
Localisation : France

Informations professionnelles :
Activité : Etudiant Java

Informations forums :
Inscription : octobre 2011
Messages : 15
Points : 6
Points : 6
Par défaut Spring MVC : imports statiques

Bonjour tout le monde,

Je rencontre un problème concernant l'import statique de ressources (css, javascript...) dans une jsp, dans le cadre d'une application Web utilisant Spring. chaque que je tente d'importer un css ou javascript, je me retrouve face à une erreur 400.
J'ai cherché un peu partout sur Internet les solutions, mais toujours en vain

Est-ce que quelqu'un aurait une solution qui marche à m'apporter ? Merci beaucoup !


Voici mes sources, si jamais ça peut aider

web.xml
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
<web-app version="2.5" 
		 xmlns="http://java.sun.com/xml/ns/javaee" 
		 xmlns:c="http://java.sun.com/jsp/jstl/core" 
		 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		 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_2_5.xsd">
 
	<!-- servlets -->
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
 
	<!-- mappings -->
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>
spring-mvc-servlet.xml
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans" 
	   xmlns:beans="http://www.springframework.org/schema/beans" 
	   xmlns:context="http://www.springframework.org/schema/context" 
	   xmlns:mvc="http://www.springframework.org/schema/mvc" 
	   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 
						   http://www.springframework.org/schema/context 
						   http://www.springframework.org/schema/context/spring-context-3.0.xsd 
						   http://www.springframework.org/schema/mvc 
						   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
	<context:annotation-config />
	<context:component-scan base-package="fr.perso.spring_mvc" />
 
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 
	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
 
	<bean id="viewsResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>
HomeController.java
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
package fr.perso.spring_mvc.controller;
 
import org.springframework.stereotype.Controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import org.springframework.ui.ModelMap;
 
@Controller
@RequestMapping("/")
public class HomeController {
 
	@RequestMapping(method = RequestMethod.GET)
	public String httpGet(ModelMap model) {
 
		model.addAttribute("title", "Home");
 
		return "home";
	}
}
home.jsp
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
 
<!DOCTYPE html>
 
<html>
	<head>
		<title>${title}</title>
 
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
		<!-- css -->
		<link rel="stylesheet" type="text/css" href="/static/css/styles.css" />
	</head>
 
	<body>
		<p>ce texte doit être en rouge</p>
	</body>
</html>
styles.css
Code :
1
2
3
4
5
6
 
@CHARSET "utf-8";
 
p {
	color: red;
}
bloub est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2012, 12h33   #2
Expert Confirmé
 
Homme
Inscription : septembre 2006
Messages : 2 291
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : septembre 2006
Messages : 2 291
Points : 2 738
Points : 2 738
ne manquerait-il pas des <mvc:resources …> dans votre servlet.xml ?
JeitEmgie est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2012, 13h51   #3
Invité régulier
 
Homme Romain
Etudiant Java
Inscription : octobre 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme Romain
Localisation : France

Informations professionnelles :
Activité : Etudiant Java

Informations forums :
Inscription : octobre 2011
Messages : 15
Points : 6
Points : 6
C'est justement une des solutions que j'ai essayé, j'avais mis ça :

Code :
<mvc:resources mapping="/static/**" location="/static/" />
Mais le résultat est la même, une erreur 400

L'autre solution consistait à bidouiller un peu le fichier web.xml, mais j'ai pas réussi à reproduire tout ça.
bloub est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2012, 13h59   #4
Expert Confirmé
 
Homme
Inscription : septembre 2006
Messages : 2 291
Détails du profil
Informations personnelles :
Sexe : Homme

Informations forums :
Inscription : septembre 2006
Messages : 2 291
Points : 2 738
Points : 2 738
Et

Code :
<mvc:resources mapping="/static/**" location="classpath:/static/" />
?
JeitEmgie est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2012, 15h09   #5
Invité régulier
 
Homme Romain
Etudiant Java
Inscription : octobre 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme Romain
Localisation : France

Informations professionnelles :
Activité : Etudiant Java

Informations forums :
Inscription : octobre 2011
Messages : 15
Points : 6
Points : 6
mmh pas essayé de le mettre dans le classpath, je teste ça ce soir et je vous informerai.

encore merci !

EDIT : ça ne fonctionne pas non plus
bloub est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/02/2012, 21h47   #6
Invité régulier
 
Homme Romain
Etudiant Java
Inscription : octobre 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme Romain
Localisation : France

Informations professionnelles :
Activité : Etudiant Java

Informations forums :
Inscription : octobre 2011
Messages : 15
Points : 6
Points : 6
Bon... J'ai trouvé une solution qui marche mais elle est très moche, je suis quand même preneur de solutions plus conventionnelles !

J'ai utilisé cette forme :

Code :
1
2
3
<style type="text/css">
    <%@ include file="../static/css/styles.css" %>
</style>
bloub est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 21h01.


 
 
 
 
Partenaires

Hébergement Web