从JSP重定向到控制器Spring

时间:2013-08-28 12:58:17

标签: jsp spring-mvc

当我点击一个URL时,我想用一个JSP调用一个控制器(链接到另一个JSP页面),我的代码如下:

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

我想在/FileMonitor/ResultPage/致电的课程在这里:

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

}

但是我得到了404,谁能看到我做错了什么?如何从JSP页面访问控制器?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

}

答案 2 :(得分:1)

正在发生的事情是<a href="/something />链接会将您重定向到服务器根目录(因为“/”)。为防止这种情况,您有两种选择:

  • 设置绝对路径:
  

“/ myProject的/东西”

  • 使用JSTL标记<c:url value="/something"/>将在网址上添加"/myProject"