我怎样才能让Tomcat输出空字符串而不是null? [英] How can I get Tomcat to output empty string instead of null?

查看:134
本文介绍了我怎样才能让Tomcat输出空字符串而不是null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从其他Servlet/JSP引擎切换到Tomcat 6.0.26.以前,空值将打印为空字符串.但是,Tomcat将空值打印为"null".

I just switched over to Tomcat 6.0.26 from a different Servlet/JSP engine. Previously, null values would print out as an empty String. However, Tomcat is printing out nulls as "null".

<% String str = null; %>
The value of str is <%= str %>.

此输出

The value of str is null.

如何让Tomcat用空/空白字符串替换空值?

How can I get Tomcat to replace nulls with empty/blank strings?

推荐答案

这是默认行为和预期行为.显然,先前的服务器为此具有一些专有的配置设置.没想到Tomcat.

This is default and expected behaviour. Apparently the previous server has some proprietary configuration setting for that. None in Tomcat comes to mind.

无论如何, scriptlets 是一种老式技术,十年来一直不鼓励使用它.它已被taglibs(如JSTL)和表达语言( EL),并结合Servlet作为控制器.

Regardless, scriptlets is a vintage technique and its use is discouraged over a decade. It has been succeeded by taglibs (like JSTL) and Expression Language (EL) in combination with a Servlet as controller.

例如在Servlet的doGet():

E.g. in Servlet's doGet():

String str = null;
request.setAttribute("str", str);
request.getRequestDispatcher("page.jsp").forward(request, response);

page.jsp中的

The value of str is ${str}

如果值等于null,则

EL默认情况下不打印任何内容,而 scriptlet 表达式(<%= %>)默认情况下将打印

EL will by default print nothing if the value equals null while scriptlet expression (<%= %>) by default prints whatever String#valueOf() returns.

另请参见:

这篇关于我怎样才能让Tomcat输出空字符串而不是null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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