Servlet属性名称可以包含连字符-吗? [英] Can servlet attribute names contain a hyphen -?

查看:78
本文介绍了Servlet属性名称可以包含连字符-吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

servlet属性名称可以包含连字符-吗?

Can servlet attribute names contain a hyphen -?

因为,我尝试从servlet的doPost中的请求集中检索属性,但结果不是我想要的.

Because, I tried to retrieve the attributes from the request set in the doPost in my servlet, but the result is not what I'm looking for.

在我的servlet中,我有这个:

In my servlet I have this :

String X_USER = request.getParameter("X-User");
request.setAttribute("X-User", X_USER);

String YYYY_YYYY = request.getParameter("Y_CODE");
request.setAttribute("Y-Code", YYYY_YYYY);

在我要显示这些属性的JSP中,我这样做:

In my JSP where I want to show these attributes I do this:

<li>${X-User}</li>
<li>${Y-Code}</li>

问题是我得到0而不是参数值.

The problem is that I'm getting 0 instead of the parameter value.

推荐答案

您的具体问题不是HTTP请求参数,而是EL变量名称. EL变量名称中的所有Java关键字和标识符都是非法的.

Your concrete problem is not the HTTP request parameter, but the EL variable name. All Java keywords and identifiers are illegal in EL variable names.

-是Java(以及EL)中的减法运算符. EL处理器应该如何知道您是要使用文字名称X-User的属性还是从${X}减去${User}的整数的结果?按规范解释为后者,它也解释了0的数值结果.

The - is a subtraction operator in Java (and also in EL). How should the EL processor know if you meant to use an attribute with the literal name X-User or the result of an integer subtraction of ${User} from ${X}? It's by specification interpreted as latter which also explains the numeric result of 0.

就像在普通Java中一样,只需使用下划线或驼峰字母即可.

Just use underscore or camelcase instead, like as in normal Java.

request.setAttribute("Y_User", X_USER);
request.setAttribute("Y_Code", Y_CODE);

${X_User}
${Y_Code}

如果您绝对需要访问EL中包含连字符的请求属性,请在请求范围映射上使用大括号符号:

If you absolutely need to access a request attribute containing a hyphen in EL, then use the brace notationon the request scope map:

${requestScope['X-User']}
${requestScope['Y-User']}

顺便说一句,这同样适用于请求参数,您不必将其复制到servlet的请求范围中:

The same applies to request parameters, by the way, which you don't necessarily need to copy over into the request scope in the servlet:

${param['X-User']}
${param['Y-Code']}

另请参见:

  • EL 3.0规范
  • 我们的EL Wiki页面
  • See also:

    • EL 3.0 specification
    • Our EL wiki page
    • 这篇关于Servlet属性名称可以包含连字符-吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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