How to map dynamic url /prj/noticeOpen/2 in Spring

2019-01-08 01:57发布

问题:

Hi I am having hard time with following url:

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

to map with following controller method:

@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);

}

Problem starts when i click on the anchor dynamicLink and it does not transfers control to my controller, instead it shows following in browser's address bar:

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

Moreover I have following mapping in 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>

This all works fine if I remove {quesId} from controller's @RequestMapping and @PathParam from method signature(also remove questionId from anchor )

http://127.0.0.1:8080/prj/noticeOpen 

But that does not sound dynamic and fulfill my requirement.

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>

Update

I created new Controller for /noticeOpen/{quesId} and its now getting the control, But I am not able to understand the behavior of the following methods. Please take a look below on NoticeController and then result under that:

  @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") it redirects me to correct noticeOpen.jsp @RequestMapping(value="/noticeOpen/{quesId}") it redirects me to following error Page

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:

Change your prefix value in applicationContext.xml as follows

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

A slash before WEB-INF.It will work.