@PathVariable位置SPRING-MVC

时间:2013-11-06 04:35:31

标签: spring-mvc

    @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“&GT;

<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>

config.xml中

<?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>

2 个答案:

答案 0 :(得分:2)

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

  1. * .html
  2. / PDF文件/ **
  3. /图像/ **
  4. *。JSON
  5. 但您的网址http://localhost:8080/Atriev/pages/1与任何地图都不匹配 更好地将deispltcher servlet映射到<url-pattern>/</url-pattern>

答案 1 :(得分:0)

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

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

当然,如果有多个路径变量,则必须使用。