如何在Spring MVC控制器中映射动态url / prj / noticeOpen / 2

时间:2013-07-17 10:54:51

标签: spring spring-mvc

嗨,我很难跟进网址:

<a href="/noticeOpen/2">dynamicLink</a>

使用以下控制器方法进行映射:

@RequestMapping(value="/noticeOpen/{quesId}") 
public ModelAndView noticeOpen(@ModelAttribute("NoticeForm") NoticeForm noticeForm,
                               ModelMap model,
                               @PathVariable("quesId") Integer quesId){
    System.out.println(quesId);
    return new ModelAndView("/noticeOpen","noticeForm",noticeForm);

}

当我点击锚 dynamicLink 并且它没有将控制权转移到我的控制器时问题就开始了,而是在浏览器的地址栏中显示以下内容:

http://127.0.0.1:8080/prj/noticeOpen/2/WEB-INF/pages/noticeOpen.jsp

此外,我在applicationContext.xml

中有以下映射
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
</bean>

如果我从方法签名中删除控制器的@RequestMapping和@PathParam中的{quesId}(也从锚中删除questionId),这一切都可以正常工作

http://127.0.0.1:8080/prj/noticeOpen 

但这听起来不是动态的,符合我的要求。

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

   <display-name>Spring Web MVC Application</display-name>

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

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>   
</web-app>

更新

我为/noticeOpen/{quesId}创建了新的Controller,它现在获得了控件,但我无法理解以下方法的行为。 请看下面的NoticeController,然后在下面得到结果:

  @Controller
public class NoticeController {


    @RequestMapping(value="/noticeOpen/{quesId}") 
    public ModelAndView noticeOpen(@ModelAttribute("NoticeForm") NoticeForm noticeForm,ModelMap model,@PathVariable("quesId") Integer quesId){

        return new ModelAndView("noticeOpen","noticeForm",noticeForm);

    }

    @RequestMapping(value="/noticeOpen") 
    public ModelAndView noticeOpen(@ModelAttribute("NoticeForm") NoticeForm noticeForm,ModelMap model){

        return new ModelAndView("noticeOpen","noticeForm",noticeForm);

    }

@RequestMapping(value="/noticeOpen")它将我重定向到正确的noticeOpen.jsp @RequestMapping(value="/noticeOpen/{quesId}")它会将我重定向到以下错误页面

HTTP Status 404 - /prj/noticeOpen/WEB-INF/pages/noticeOpen.jsp
type Status report
message /prj/noticeOpen/WEB-INF/pages/noticeOpen.jsp
description The requested resource is not available.
Apache Tomcat/6.0.36

1 个答案:

答案 0 :(得分:3)

在applicationContext.xml中更改前缀值,如下所示

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
</bean>

WEB-INF之前的斜线。它会起作用。