MattTheHack
MattTheHack

Reputation: 1394

Redirecting from a JSP to a controller Spring

I have a JSP that I want to use to call a controller (that's linked to another JSP page) when a URL is clicked, my code is as follows:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
    <h1>Spring MVC Hello World Example</h1>

    <h2>${msg}</h2>

     <a href="/FileMonitor/ResultPage/">click</a>

</body>
</html>

The class I want to call at /FileMonitor/ResultPage/ is here:

@Controller
@RequestMapping(value="/Result")
public class ResultController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("ResultPage");
        model.addObject("msg", "result");

        return model;
    }

}

But I'm getting a 404, can anyone see what I'm doing wrong? How can I access the controller from the JSP page?

Upvotes: 2

Views: 9505

Answers (3)

merours
merours

Reputation: 4106

What's happening is that <a href="/something /> link will redirect you to the server root (because of the "/"). To prevent that, you have two options :

  • setting the absolute path :

"/myProject/something"

  • using the JSTL tag <c:url value="/something"/> wich will add the "/myProject" on the url.

Upvotes: 1

Ilya
Ilya

Reputation: 2167

Try url tag from JSTL:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
    <h1>Spring MVC Hello World Example</h1>

    <h2>${msg}</h2>

    <a href="<c:url value="/FileMonitor/ResultPage/"/>">click</a>

</body>
</html>

Upvotes: 2

Alex
Alex

Reputation: 11579

@Controller
@RequestMapping(value="/FileMonitor/ResultPage/")
public class ResultController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("ResultPage");
        model.addObject("msg", "result");

        return model;
    }

}

Upvotes: 2

Related Questions