Announcement Announcement Module
Collapse
No announcement yet.
Accessing a controller method from a link in a JSP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing a controller method from a link in a JSP

    I am getting a 404 error from the following link.

    Code:
    <a href="<c:url value="/getSingleBulletin/${bulletin.id}" />">${bulletin.name}	-- ${bulletin.subject}</a>
    This is the method I'm trying to access.

    Code:
    	@RequestMapping(value = "/getSingleBulletin", method = RequestMethod.GET)
    	public ModelAndView getSingleBulletin(@ModelAttribute("id") int id,
    			Model model) {
    		ModelAndView mav = new ModelAndView();
    
    		try {
    			Bulletin bulletin = bulletinDAO.getSingleBulletin(id);
    			mav.setViewName("WEB-INF/jsp/BulletinApproval");
    			if (bulletin != null) {
    				mav.addObject("bulletin", bulletin);
    			}
    		} catch (Exception e) {
    			System.out.println(e.getMessage());
    			mav.setViewName("WEB-INF/jsp/FailurePage");
    		}
    
    		return mav;
    	}

  • #2
    i suggest a read of the reference guide... It isn't a @ModelAttribute nor a @RequestParam ... Use @PathVariable to get access and modify you URL accordingly.

    Code:
    @RequestMapping(value = "/getSingleBulletin/{id}", method = RequestMethod.GET)
    public ModelAndView getSingleBulletin(@PathVariable("id") int id, Model model) {
    I also would modify the url getSingleBulleting sounds like you are doing RPC instead of REST

    Code:
    @RequestMapping(value = "/bulletin/{id}", method = RequestMethod.GET)
    public ModelAndView getSingleBulletin(@PathVariable("id") int id, Model model) {
    Ofcourse you would have to modify the href to...

    Comment


    • #3
      I have modified my code accordingly, but it didn't work. I'm still getting the same error. Here is what I have now. This is my link.

      Code:
      <a href="/bulletin/${bulletin.id}" >${bulletin.name} -- ${bulletin.subject}</a>
      This is my controller code.

      Code:
      	@RequestMapping(value = "/bulletin/{id}", method = RequestMethod.GET)
      	public ModelAndView getSingleBulletin(@PathVariable("id") int id,
      			Model model) {
      		ModelAndView mav = new ModelAndView();
      
      		try {
      			Bulletin bulletin = bulletinDAO.getSingleBulletin(id);
      			mav.setViewName("WEB-INF/jsp/BulletinApproval");
      			if (bulletin != null) {
      				ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
      						.currentRequestAttributes();
      				HttpSession session = attributes.getRequest().getSession(true);
      				session.setAttribute("bulletin", bulletin);
      			}
      		} catch (Exception e) {
      			System.out.println(e.getMessage());
      			mav.setViewName("WEB-INF/jsp/FailurePage");
      		}
      
      		return mav;
      	}

      Comment


      • #4
        As mentioned before I suggest a read of the reference guide, judging by the code of your controller you are missing some basics of Spring MVC.

        Currently your link is broken as it will resolve to /bulleting/{id} whereas you probably want /application/bulletin/{id}, so why did you remove the c:url part?! YOu also might want to check Springs url tag which takes into account additional properties.

        Code:
        <a href="<c:url value="/bulletin/${bulletin.id}" />">${bulletin.name}	-- ${bulletin.subject}</a>
        Your controllers code, why on earth are you using the RequestContextHolder to get the request and session and next put the bulletin in the session which should be only in the model. Also judging from the code you are lacking a properly configurer ViewResolver.

        I would expect the controller to be more like this

        Code:
        @RequestMapping(value = "/bulletin/{id}", method = RequestMethod.GET)
        public String getSingleBulletin(@PathVariable("id") int id, Model model) {
        	try {
        		Bulletin bulletin = bulletinDAO.getSingleBulletin(id);
        		model.addAttribute("bulletin", bulletin);		
        		return "BulletinApproval";
        	} catch (Exception e) {
        		System.out.println(e.getMessage());
        		return "FailurePage";
        	}
        }
        This requires a properly configured ViewResolver which would prefix the viewname with /WEB-INF/jsp.

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

        Comment


        • #5
          Problem solved. Here is my URL.

          Code:
          <a href="<c:url value="/bulletin/${bulletin.id}" />" >${bulletin.name} -- ${bulletin.subject}</a>
          Here is my controller method.

          Code:
          @RequestMapping(value = "/bulletin/{id}", method = RequestMethod.GET)
          	public String getSingleBulletin(@PathVariable("id") int id,	Model model) {
          		try {
          			Bulletin bulletin = bulletinDAO.getSingleBulletin(id);
          			model.addAttribute("bulletin", bulletin);
          			return "ShowBulletin";
          		} catch (Exception e) {
          			System.out.println(e.getMessage());
          			return "FailurePage";
          		}
          	}

          Comment

          Working...
          X