使用@RequestMapping时,Spring MVC中没有请求的资源

时间:2013-12-07 13:31:34

标签: spring spring-mvc

在Spring MVC中,使用.jsp时遇到加载@RequstMapping页面的问题。处理程序方法响应,但.jsp页面无法加载,因为我不知道为什么ModelAndView在字符串中添加了表示URL路径的内容。以下是@Controller类中的代码:

@RequestMapping(value="/sent/{phoneNumber}", method=RequestMethod.GET)
public ModelAndView showSentMessagesToSelectedPhoneNumber(@PathVariable int phoneNumber,HttpSession session)
{
    ModelAndView modelAndView = new ModelAndView("sentMessagesToPhoneNumber");
            ...
            return modelAndView;
     }

以下是.jsp中的内容:

<a href="${pageContext.request.contextPath}/sent/${reciever.getPhoneNumber()}.html">${reciever.getContactName()}</a>

然后当我点击Ling时,浏览器会告诉我: HTTP Status 404 - /HomeWork/sent/WEB-INF/pages/sentMessagesToPhoneNumber.jsp。 我可以得出结论,/sent中的@RequestMapping会添加到路径中。 有人能告诉我这是什么问题吗?

2 个答案:

答案 0 :(得分:0)

看起来您已在配置文件中定义了view resolver。像

这样的东西
<bean id="jspViewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
              value="someViewClass"/>
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

当您在控制器中使用名称构造ModelAndView时,您将被定向到一个jsp页面,其中的bean和名称中定义了前缀和后缀。您可能因为sentMessagesToPhoneNumber.jsp目录中没有/WEB-INF/pages/而得到404。

答案 1 :(得分:0)

以下是我在app-context.xml文件中的内容:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="mk.ukim.finki.mp.homework"></context:component-scan>

    <mvc:resources mapping="/js/**" location="/js/" />

    <import resource="security-context.xml"/>