JSP页面加载有错误

时间:2015-07-05 07:31:48

标签: java spring jsp spring-mvc servlets

我是Spring MVC和restful servlet的新手;当我通过此url http://localhost:8080/ES/index请求jsp页面时它返回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>

2 个答案:

答案 0 :(得分:1)

据我所知,因为你的索引页面没有请求映射。您只显示了一个控制器,它映射了包含&#34; / api / foo&#34;的网址。

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

你需要另一个@RequestMapping for&#34; / index&#34;,它可以是你已经完成的类范围:

@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";
    }
}

答案 1 :(得分:0)

你想要什么并不清楚。您尚未将索引映射到任何请求映射,就像您对 / api / foo 所做的那样。如果您希望将 / api / foo 用作索引页面,则无需在网址末尾添加 / index 。为此,只需将 / api / foo 添加到 web.xml

中的欢迎文件列表中

示例:

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