Announcement Announcement Module
Collapse
No announcement yet.
How to map dynamic url /prj/noticeOpen/2 in Spring MVC controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to map dynamic url /prj/noticeOpen/2 in Spring MVC controller

    Hi I am having hard time with following url:

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

    to map with following controller method:

    Code:
    @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...noticeOpen.jsp


    Moreover I have following mapping in applicationContext.xml

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

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


    Code:
     @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

    HTML Code:
    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
    I have also posted this on http://stackoverflow.com/questions/1...mvc-controller

    Thanks
    Navdeep Singh Kanwal

  • #2
    If the first method finds the JSP page the second one should too. I suspect you have a spelling mistake that you haven't reproduced in the forum post?

    Comment

    Working...
    X