一个jsp标签中的java变量? [英] java variable in a jsp tag?

查看:269
本文介绍了一个jsp标签中的java变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做这样的事情:

I'm trying to do something like this:

<%
    String headerDateFormat = "EEE, d MMM yyyy h:mm:ss aa"; 
%>

<fmt:formatDate pattern="<% out.print( headerDateFormat ); %>" value="${now}" />

我也尝试过:

<fmt:formatDate pattern="${headerDateFormat}" value="${now}" />

并且:

<fmt:formatDate pattern="headerDateFormat" value="${now}" />

我显然是JSP的新手-这可能吗?理想情况下,我希望能够通过Rhino在javascript中重用headerDateFormat-我认为它可以使用它,但不能在JSP标记中使用.

I'm obviously very new to JSP - is this possible? Ideally I'd like to be able to reuse the headerDateFormat in javascript via Rhino - I think as is it will work with it, but not in the JSP tags.

推荐答案

如果要使用

<fmt:formatDate pattern="${headerDateFormat}" value="${now}" />

(实际上是正确方式)

然后您应该将其作为属性,以该名称为键在页面,请求,会话或应用程序作用域之一中.假设您要将其放入servlet的请求范围中:

then you should be putting it as an attribute in one of the page, request, session or application scopes with that name as key. Assuming that you want to put it in the request scope in a servlet:

String headerDateFormat = "EEE, d MMM yyyy h:mm:ss aa";
request.setAttribute("headerDateFormat", headerDateFormat);

您也可以为此使用JSTL <c:set>.

You can also use JSTL <c:set> for this.

<c:set var="headerDateFormat" value="EEE, d MMM yyyy h:mm:ss aa" />

默认情况下,它将在页面范围内设置.

it will by default be set in the page scope.

  • Our JSP wiki page - a little introduction to JSP

这篇关于一个jsp标签中的java变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆