Quando i miei file jsp sono all'interno della cartella WEB-INF (come /WEB-INF/file.jsp), posso accedervi da localhost: 8080/ProjectCtxtRoot /, ma non posso accedervi, se sono inseriti in/WEB-INF/jsp/file.jsp?Perché i file jsp all'interno della cartella WEB-INF funzionano, ma non collocati in una cartella sotto WEB-INF?
Ho cambiato il percorso nel tag welcome-list in web.xml come segue
<welcome-file-list>
<welcome-file>/JSP/fileName.jsp</welcome-file>
</welcome-file-list>
Ho cambiato anche il dispatcher-servlet.xml come segue
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
Ancora non funziona . URL utilizzato per il caso di cui sopra sono
localhost:8080/ContextRoot/jsp/
localhost:8080/ContextRoot/jsp/fileName.jsp
localhost:8080/ContextRoot/jsp/fileName
e non funziona per qualsiasi URL sopra.
Ma funzionava quando
<welcome-file-list>
<welcome-file>/fileName.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.xml come segue
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
URL utilizzato per caso sopra è localhost: 8080/ContextRoot/e funziona.
Uso il server tomcat v 7.0. Rinnovo il mio progetto in Eclipse IDE, poi lo pulisco, lo costruisco, costruisco guerra usando l'installazione di mvn clean, quindi scelgo la guerra dalla home page di Tomcat Manager e la distribuisco. Lo faccio ogni volta.
Ecco come diapatcher-servlet.xml analizza tutto il processo. Mi limito a cambiare sezione particolare come sopra detto
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.projectName.www" />
<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
</bean>
<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="tableName" />
</bean>
<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!-- <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
<bean class=
"org.springframework.web.servlet.view.tiles2.TilesConfigurer"> -->
<!-- <property name="definitions">
<list>
<value>/WEB-INF/views/views.xml</value>
</list>
</property>
</bean> -->
</beans>
Ecco come il mio web.xml assomiglia
<web-app>
<!-- <display-name>Archetype Created Web Application</display-name> -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/src/main/webapp/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<welcome-file-list>
<welcome-file>/fileName.jsp</welcome-file>
</welcome-file-list>
</web-app>
Ok. Funziona quando ho spostato l'intera cartella jsp da /webapp/WEB-INF/jsp/fileName.jsp a /webapp/jsp/fileName.jsp. Mi piacerebbe sapere 1. perché funziona ora? 2. È questo il modo corretto di fare le cose? 3. Funziona quando l'url è localhost: 8080/CtxtRoot/jsp/o localhost: 8080/CtxtRoot/jsp/search.jsp, ma non funziona con localhost: 8080/AnnaUnivResults/jsp/search. Perché è così?
Penso che la capitalizzazione possa effettivamente fare la differenza qui, dato che la primavera sta interpretando le cose in un percorso file. – msknapp
si prega di lasciare un commento che dice se il primo esempio funziona quando il file di benvenuto ha un jsp in minuscolo. – msknapp
Un '' è ** completamente ** non correlato a Spring 'ViewResolver'. La tua domanda è attualmente dappertutto. Dovrai chiarire il tuo percorso di contesto e la richiesta HTTP esatta che invii in ogni scenario. Se la richiesta è gestita da 'DispatcherServlet' di Spring, sarà necessario mostrare anche il gestore. –