【问题标题】:JSP with Spring MVC - ${} variables not showing带有 Spring MVC 的 JSP - ${} 变量未显示
【发布时间】:2013-05-11 18:48:37
【问题描述】:

我正在为这个应用程序使用 Spring MVC,它是一个非常简单的应用程序,仅用于测试目的,只需保存和查看数据库中的数据。这是第一个页面,其中包含指向另一个页面的简单链接,使用 ${context.root} 设置基本 url。这是页面中的代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Mah oeeee!</title>
</head>
<body>
    <h1>Alunos!</h1>

    <a href="${context.root}/SGE/aluno/cadastro">Novo aluno</a>
</body>

问题是,链接上没有显示例如:http://localhost:8080/SGE/aluno/cadastro,而是显示http://localhost:8080/SGE/${context.root}/SGE/aluno/cadastro。同样的事情发生在视图页面上,它显示了来自数据库的数据:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>CONSULTA</h1>
    <table>
        <tr>
            <td>Nome</td>
            <td>${aluno.nome}</td>
        </tr>
        <tr>
            <td>CPF</td>
            <td>${aluno.cpf}</td>
        </tr>
        <tr>
            <td>E-Mail</td>
            <td>${aluno.email}</td>
        </tr>
    </table>
</body>

而不是显示变量的值,例如:

Nome:     Joao
CPF:      98765482312
E-mail:   joao@joao.com

它只是这样显示:

Nome    ${aluno.nome}
CPF         ${aluno.cpf}
E-Mail  ${aluno.email}

可能是什么问题,所以我得到了解决?问题可能是tomcat上的一些配置吗?

编辑:根据要求,这是代码:

@RequestMapping(method = RequestMethod.GET)
public ModelAndView index() {
    return new ModelAndView("aluno/index");
}

@RequestMapping(value = "/consulta/{id}")
public ModelAndView consultar(@PathVariable int id) {
    Aluno aluno = getAlunoService().buscarPorId(id);
    return new ModelAndView("aluno/consulta", "aluno", aluno);
}

【问题讨论】:

  • 试试${pageContext.request.contextPath}。你可以粘贴控制器代码吗?很难像现在这样诊断问题。
  • 我为这两个视图添加了代码。问题是,我如何发送数据、它是什么页面、它是什么变量并不重要,如果我使用 ${} 它只是将变量视为文本 o.O
  • @onlido 也许你缺少 jstl EL 罐子?
  • alunoconsultar() 中真的不为空吗?另外,Aluno 类是否具有正确的 getter 方法(如 getNome()getCpf() 等)?
  • 您使用的是什么版本的 Servlet?在 2.3 之前,JSP EL 默认是禁用的。

标签: java jsp tomcat spring-mvc


【解决方案1】:

您必须提供以下指令

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

如果你使用的是maven,那么添加以下依赖

  <dependency> 
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId> 
       <version>1.2</version> 
 </dependency>

在你的项目中添加 jstl.jar

【讨论】:

    【解决方案2】:

    尝试将您的 web.xml 启动为

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
    

    【讨论】:

      【解决方案3】:

      您的网页上似乎缺少一些指令..

      <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
      

      你可能不需要所有这些(取决于你想要什么),但你肯定需要一些。

      【讨论】:

        【解决方案4】:

        因为我刚刚遇到了这个问题,这是我在堆栈溢出中发现的第一个问题,其中包含我的确切问题。我以为我会超越对我有用的解决方案。请参阅此页面。

        https://www.mkyong.com/spring-mvc/modelandviews-model-value-is-not-displayed-in-jsp-via-el/

        如果您在 web.xml 中使用旧式 JSP 声明,则基本上 EL 标记会通过 JSTL 被忽略。你可以留下它并添加

        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
        <html>
        <head>
        <%@ page isELIgnored="false" %>
        

        到您使用 EL 标记的每个页面,或者您可以更新到默认使用 EL 标记的新样式 web.xml 声明。

        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
        //...
        </web-app>
        

        我希望这对其他人有所帮助。非常感谢 mkyong.com 页面提供了我需要的解决方案。

        【讨论】:

        • 我提供的解决方案的编辑是使用上面 Dmitry 的答案更新我的 web-app 标签,因为它比 mkyong.com 上的更新。由于缺乏解释,我没有尝试他的答案,尽管它是正确的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-24
        • 1970-01-01
        • 2017-11-20
        • 1970-01-01
        • 2015-05-26
        • 1970-01-01
        相关资源
        最近更新 更多