【问题标题】:JSTL: Why my data are not being printed in JSP?JSTL:为什么我的数据没有在 JSP 中打印?
【发布时间】:2017-04-17 07:23:13
【问题描述】:

请检查下面的代码,我将数据从我的Servlet 附加到RequestDispatcher

request.setAttribute("userInfo", userInfo);
            request.setAttribute("userProfession", userProfession);
            request.setAttribute("userQualification", userQualification);
            
            RequestDispatcher dispatch = request.getRequestDispatcher("/WEB-INF/jsp/profile.jsp");
            dispatch.forward(request, response);

所有附加属性均为beans

在我的JSP 中,我得到如下数据。

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

<body>
<h5>Name</h5>
<p class="xpanel_txt1"><c out="${userInfo.firstName}"/> <c out=" "/> <c out="${userInfo.lastName}"/></p>
</body>

现在有趣的部分是,当显示网站 JSP 时,我只看到一个空白字段,应该输入上述数据。但是,如果我在谷歌浏览器中打开inspector,我可以看到数据已经加载。

为什么会这样?

【问题讨论】:

    标签: java html jsp servlets requestdispatcher


    【解决方案1】:

    发现问题。 JSTl 中有错误。正确的打印方式是

    <c:out value="${userInfo.email}"/>
    

    我错过了代码中的value 部分和其他问题。

    【讨论】:

    • 我在回答中提到了一些额外的内容
    【解决方案2】:

    你应该在jstl标签中使用c:outvalue属性

    <head>
              <%@taglib prefix="c" uri= "http://java.sun.com/jsp/jstl/core" %>
        </head>
    
        <body>
        <h5>Name</h5>
        <p class="xpanel_txt1"><c:out value="${userInfo.firstName}"/> <c:out value=" "/> <c:out value="${userInfo.lastName}"/></p>
        </body>
    

    【讨论】:

      【解决方案3】:

      仅供参考,而不是

      <p class="xpanel_txt1"><c:out value="${userInfo.firstName}"/> <c:out value=" "/> <c:out value="${userInfo.lastName}"/></p>
      

      你可以直接使用EL显示输出

      <p class="xpanel_txt1">${userInfo.firstName} ${userInfo.lastName}</p>
      

      这两种方式的区别,请参考这个答案-https://stackoverflow.com/a/6574812/7873361

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 2021-11-23
        • 2021-01-05
        • 2017-08-12
        • 2012-08-16
        • 1970-01-01
        相关资源
        最近更新 更多