Sono abbastanza nuovo a primavera e sto avendo problemi con l'accesso ai mie risorse nel mio primavera app MVC. Ho provato Google e utilizzando lo stack overflow per trovare una risposta (nessuno di questi mi ha aiutato possible solution 1 o possible solution, possible solution 3, spring framework) ma non ho trovato nessuno che abbia risolto il mio problema o migliorato la mia comprensione del problema (potrebbe essere semplicemente perché non ho capito bene le soluzioni citate).risorse Accesso a pagina JSP di Spring MVC app
sono riuscito a incorporato mio file css in una pagina web, ma utilizzando il codice:
<%@include file="css/style.css" %>
ma non posso accedervi utilizzando un URL in questo modo per ovvi motivi. Ho anche provato ad utilizzare il seguente nel mio file web.xml ma non ha alcun effetto, devo ammettere che non capisco davvero la mappatura in questo caso in modo che possa essere un problema
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
Ho anche provato ad utilizzare ciascuno dei seguenti separatamente:
<img src="<%=request.getContextPath()%>/images/logo.jpg"/>
<img src="<%=request.getContextPath()%>/src/main/resources/images/logo.jpg"/>
Questo è il mio layout del progetto:
Questo è il mio web.xml:
<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>MyProject Application</display-name>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Questo è il mio myServlet-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.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">
<!-- -->
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
<bean name="/index.html"
class="com.myproject.controller.AdminController" />
<bean id="viewResolver"
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>
e solo nel caso in qui è la mia pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<springVersion>3.0.4.RELEASE</springVersion>
</properties>
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- ORACLE JDBC driver, need install yourself -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springVersion}</version>
</dependency>
<!-- for compile only, your container should have this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Commons-logging & log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>admin_UI</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
sarei felice se riesco a ottenere l'accesso le cartelle di immagini e css e il contenuto di quelle cartelle all'interno della webapp/WEB-INF/pagine allo stesso modo in cui posso accedere alle mie pagine jsp (ad esempio tramite URL), ma Idealmente vorrei capire come dovrei farlo correttamente cioè cartella dovrei mappare (src \ main \ resources o r src \ main \ webapp \ WEB-INF \ some_resource_folder) e come dovrei mapparlo.
Se uno qualsiasi ulteriori informazioni è richiedo sarei felice di fornire.
Sei un gen t, che ha funzionato perfettamente, chiaramente non ho capito dove si trovava la cartella delle risorse. molto apprezzato. Ho una domanda di follow up, sto ricevendo un errore in eclipse sulla mia pagina jsp 'javax.servlet.jsp.JspException non può essere risolto con un tipo \t index.jsp 'qualche consiglio? – jonnie
Contento di poterti aiutare, puoi indicarmi la tua domanda successiva? Vorrei rispondere a più delle vostre domande. Questo è stato un bel elogio per questa mattina presto. Per rispondere alla tua domanda avrei bisogno di vedere il tuo controller. –
Ricevo ora un errore in eclissi sulla mia pagina jsp javax.servlet.jsp.JspException non può essere risolto con un tipo index.jsp qualche consiglio? – jonnie