2

嗨,我很难使用以下网址:

<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 中删除 {quesId} 并从方法签名中删除 @PathParam ,这一切都很好(也从 anchor 中删除 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}现在它获得了控制权,但我无法理解以下方法的行为。请在 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
4

1 回答 1

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 前的斜线。它会起作用。

于 2013-07-18T05:25:49.450 回答