【问题标题】:Can't load page in Spring MVC无法在 Spring MVC 中加载页面
【发布时间】:2017-07-07 05:14:23
【问题描述】:

我想在我的 Spring MVC 中添加一个页面。我可以调用索引文件,但我无法单击index.jsp 中的链接以显示页面product.jsp。它的错误消息是“请求的资源不可用”。

下面是我的控制器代码:

@RequestMapping({"/", "/index"})
public String index(ModelMap model, HttpSession session, Principal principal) throws Exception {
    return "index";
}

@RequestMapping({"/product"})
public String product(ModelMap model, HttpSession session, Principal principal) throws Exception {
    return "product";
}

这个 index.jsp

<div class="row">
   <div class="col-sm-4 menu-img-pad">
       <ul class="multi-column-dropdown">
          <li><a href="../product.jsp">Joggers</a></li>
          <li><a href="../product.jsp">Foot Ball</a></li>
          <li><a href="../product.jsp">Cricket</a></li>
       </ul>
   </div>
</div>

【问题讨论】:

  • 试试这个:&lt;a href="/product"&gt;Joggers&lt;/a&gt;
  • 提供 product.jsp 的绝对路径或将文件(index.jsp 和 product.jsp )保存在同一个文件夹中。
  • @Aamir >Folder>index.jsp 和 product 和 Folder 一样在同一个位置。
  • @Càphêđen 它不起作用。

标签: java spring jsp spring-mvc


【解决方案1】:

您需要定义一个 ViewResolver 以便让 spring 如何找到您的视图。

看看here。在您的情况下,InternalResourceViewResolver 可以工作。

然后——为了调用你的处理程序——你的链接应该指向/product,因为你在@RequestMapping方法中定义了路径

【讨论】:

    【解决方案2】:

    将以下配置添加到 dispatcher-servlet.xml

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

    在上述配置中,views 是容器将在其中查找您的 product.jsp 文件的文件夹名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 2012-10-24
      • 2018-02-25
      • 1970-01-01
      • 2012-06-03
      • 2011-06-01
      • 1970-01-01
      相关资源
      最近更新 更多