@PathVariable location SPRING-MVC

2019-09-03 15:41发布

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

I use this url **http://localhost:8080/Atriev/pages/1** but it gives me an error

The requested resource is not available.

Im a newbie, sorry..

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>

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>

标签: spring-mvc
2条回答
霸刀☆藐视天下
2楼-- · 2019-09-03 16:02

your dispatcher servlet is mapped to url patterns

  1. *.html
  2. /pdfs/**
  3. /images/**
  4. *.json

But your url http://localhost:8080/Atriev/pages/1 doesnt match any of the mapping Better map your deispltcher servlet to <url-pattern>/</url-pattern>

查看更多
ら.Afraid
3楼-- · 2019-09-03 16:08

Also as a best practice it is better to mention the path variable name in the method definition.

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

Of course it is mandatory if multiple path variables.

查看更多
登录 后发表回答