[英]JSP page loading with error
我是Spring MVC和Restful Servlet的新手;当我通过此URL请求jsp页面时http://localhost:8080/ES/index
它返回404,但是当我请求http://localhost:8080/ES/api/foo/
我得到了结果。
我的JSP页面很简单。 这是Controller,web.xml和dispatcher-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>
据我所知,这是因为您没有索引页面的请求映射。 您只显示了一个控制器,该控制器映射包含“ / api / foo”的URL。
@RequestMapping("/api/foo")
public class Controller {
您需要另一个@RequestMapping作为“ / index”,可以像已经完成的那样将其范围限定为类:
@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";
}
}
您到底想要什么还不清楚。 您没有像对/ api / foo那样将索引映射到任何请求映射。 如果您希望使用/ api / foo作为索引页,则无需在网址末尾添加/ index 。 为此,只需将/ api / foo添加到web.xml中的欢迎文件列表中
例:
<welcome-file-list>
<welcome-file>api/foo</welcome-file>
</welcome-file-list>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.