0
    @RequestMapping(value = "/pages/{pageNumber}", method = RequestMethod.GET)
public String getStories(@PathVariable Integer pageNumber, Model model) {

    Page<Article> page = articleService.getStories(pageNumber);

    int current = page.getNumber() + 1;
    int begin = Math.max(1, current - 5);
    int end = Math.min(begin + 10, page.getTotalPages());

    model.addAttribute("storiesLog", page);
    model.addAttribute("beginIndex", begin);
    model.addAttribute("endIndex", end);
    model.addAttribute("currentIndex", current);

    return "atriev-stories";
}

我使用这个网址**http://localhost:8080/Atriev/pages/1** ,但它给了我一个错误

请求的资源不可用。

我是新手,对不起。。

web.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>atriev</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>atriev</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>atriev</servlet-name>
    <url-pattern>/pdfs/**</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>atriev</servlet-name>
    <url-pattern>/images/**</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>atriev</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>

<display-name>Archetype Created Web Application</display-name>

配置文件

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.2.xsd
        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-3.2.xsd">

    <mvc:annotation-driven/>

    <mvc:resources location="assets" mapping="/assets/**"/>
    <mvc:resources location="pdfs" mapping="/pdfs/**"/>
    <mvc:resources location="uploads" mapping="/uploads/**"/>

    <context:component-scan base-package="com.jwlayug.atriev.controller"/>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="contentNegotiationManager">
            <bean class="org.springframework.web.accept.ContentNegotiationManager">
                <constructor-arg>
                    <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                        <constructor-arg>
                            <map>
                                <entry key="json" value="application/json" />
                                <entry key="xml" value="application/xml" />
                            </map>
                        </constructor-arg>
                    </bean>
                </constructor-arg>
            </bean>
        </property>

        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <constructor-arg>
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                            <property name="autodetectAnnotations" value="true" />
                        </bean>
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>

    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="language"/>
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en"/>

    <bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource" 
    p:basename="messages"></bean>

    <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/> -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="2"/>





     <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/>

</beans>
4

2 回答 2

2

您的调度程序 servlet 映射到 url 模式

  1. *.html
  2. /pdfs/**
  3. /图片/**
  4. *.json

但是您的网址http://localhost:8080/Atriev/pages/1与任何映射都不匹配最好将您的 deispltcher servlet 映射到 <url-pattern>/</url-pattern>

于 2013-11-06T05:32:16.443 回答
0

同样作为最佳实践,最好在方法定义中提及路径变量名称。

public String getStories(@PathVariable("pageNumber") Integer pageNumber, Model model) {}

当然,如果多个路径变量是强制性的。

于 2013-11-06T05:41:53.280 回答