2015-07-05 3 views
0

Я новичок в Spring MVC и спокойном сервлете; когда я запрашиваю страницу jsp этим URL-адресом http://localhost:8080/ES/index Он возвращает 404, но когда я запрашиваю http://localhost:8080/ES/api/foo/, я получил результат.Загрузка JSP с ошибкой

MY JSP Страница проста. Вот контроллер, web.xml и диспетчеру-servlet.xml

Controller 
    `@RestController 
    @RequestMapping("/api/foo") 
    public class Controller { 

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
     public ResponseEntity<FindAllEstateQueryJsonObject> findAll(@RequestParam(value = "start", required = false, defaultValue = "0") Integer start 
       , @RequestParam(value = "size", required = false, defaultValue = "10") Integer size 
       , @RequestParam(value = "type", required = false) String type) { 
      // ommited 
      return new ResponseEntity<JsonObject>(jsonObject, HttpStatus.OK); 
     }` 

mvc-dispatcherservlet.xml 
`<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" 
     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/> 
    <context:component-scan base-package="org.geopart.estate.web"/> 


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/pages/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:org.geopart.estate.resources.messages"/> 
     <property name="defaultEncoding" value="UTF-8"/> 
    </bean> 

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
     <property name="paramName" value="lang"/> 
    </bean> 

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
     <property name="defaultLocale" value="en"> 
     </property> 
    </bean> 

    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
     <property name="interceptors"> 
      <ref bean="localeChangeInterceptor"/> 
     </property> 
    </bean> 
</beans>` 

web.xml 

    <web-app 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 MVC Application</display-name> 

    <!-- Spring Context Param--> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/application-context.xml 
      ,/WEB-INF/spring-data.xml 
      ,/WEB-INF/spring-security.xml 
     </param-value> 
    </context-param> 
    <!--Spring Listener --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <filter> 
     <filter-name>openSessionInViewFilter</filter-name> 
     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>openSessionInViewFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <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> 
</web-app> 
+0

Почему вы получите что-то в '/ ES/index'. Если это JSP, это должно быть '/ ES/index.jsp'. Если это контроллер, то как определяется контроллер? –

+0

Потому что это: НО, НО он не имеет никакого другого /ES/index.jsp или любого другого типа! :) Нужен ли контроллер? – Mammadreza

ответ

1

Насколько я могу судить, его потому, что вы не имеете отображение запроса для страницы индекса. Вы указали только один контроллер, который отображает URL-адрес, содержащий «/ api/foo».

@RequestMapping("/api/foo") 
public class Controller { 

Вам нужен другой @RequestMapping для "/ индекса", который может быть либо класс контекстным, как вы уже сделали:

@RequestMapping("/index") 
public class IndexController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String homePage() { 
     return "myHomePage"; 
    }` 
} 

Или метод контекстным:

public class IndexController { 

    @RequestMapping(value="/index", method = RequestMethod.GET) 
    public String homePage() { 
     return "myHomePage"; 
    } 

    @RequestMapping(value="/otherPage", method = RequestMethod.GET) 
    public String otherPage() { 
     return "aDifferentPage"; 
    } 
} 
+0

Спасибо Но что мой метод возвращает, должен быть ModelAndView или String? Не могли бы вы объяснить или рассказать мне ссылку? – Mammadreza

+0

Есть куча разных типов возвращаемых данных, которые вы можете использовать, но я думаю, что важно быть последовательными. Ответ на этот вопрос дает вам возможность сбрасывать все типы возвращаемых типов, которые вы можете использовать: http://stackoverflow.com/questions/18607290/which-return-type-use-in-spring-mvc-in-requestmapping- метод – ConMan

+0

Спасибо, это так полезно. – Mammadreza

0

Что вы хочу точно не понятно. Вы не указали индекс на любой запрос, как вы делали для /api/foo. Если вы хотите использовать /api/foo как индекс страница, то не нужно добавлять /index в конце вашего URL. Для этого просто добавьте /API/Foo в список ваших желанных файлов в web.xml

Пример:

<welcome-file-list> 
    <welcome-file>api/foo</welcome-file> 
</welcome-file-list>