无法从 JSP (JSTL) 中的 Servlet 打印属性值

问题描述

我是 Java Servlets 和 JSP 的初学者。我正在尝试打印一个从 servlet 传递的简单字符串。但是,当我尝试使用

打印属性
<h2><c:out value="${name}" /></h2>

我得到的输出为 ${name}。我尝试搜索相关帖子,并执行了我阅读的解决方案,但我无法弄清楚这个基本问题。请帮帮我。

这是我的 Servlet 代码

package com.crudproject.CrudControllers;
import java.io.IOException;
import javax.servlet.Requestdispatcher;
import javax.servlet.servletexception;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CrudServlet
 */
@WebServlet("/")
public class CrudServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException {
        doGet(request,response);
    }
    
    protected void doGet(HttpServletRequest request,IOException {

        String name = "Afras";
        Requestdispatcher rd = request.getRequestdispatcher("WEB-INF/greetings.jsp");
        request.setAttribute("name",name);
        rd.forward(request,response);
    }
}

这是我的 JSP 页面

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<html>
<body>
<h2>Hello World!</h2>
<h2><c:out value="${name}" /></h2>
</body>
</html>

POM.XML 中的 JSTL 依赖

<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)