JSP与Spring MVC - $ {}变量没有显示

Oni*_*udo 3 java jsp tomcat spring-mvc

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

<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>
Run Code Online (Sandbox Code Playgroud)

事情是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>
Run Code Online (Sandbox Code Playgroud)

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

Nome:     Joao
CPF:      98765482312
E-mail:   joao@joao.com
Run Code Online (Sandbox Code Playgroud)

它只是显示如下:

Nome    ${aluno.nome}
CPF         ${aluno.cpf}
E-Mail  ${aluno.email}
Run Code Online (Sandbox Code Playgroud)

可能是什么问题所以我得到修复?问题可能是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);
}
Run Code Online (Sandbox Code Playgroud)

Ruj*_*uju 6

您必须提供以下指令

  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是maven,请添加以下依赖项

  <dependency> 
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId> 
       <version>1.2</version> 
 </dependency>
Run Code Online (Sandbox Code Playgroud)

否则在项目中添加jstl.jar


Dmi*_*try 5

尝试以启动您的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">
Run Code Online (Sandbox Code Playgroud)