jsp表达式标签之间的差异<%和<%= [英] Difference between jsp expression tags <% and <%=

查看:136
本文介绍了jsp表达式标签之间的差异<%和<%=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我或多或少知道<%之间的差异!和<%,但我似乎找不到<%=和<%之间的区别.我试图通过将一些逻辑引入当前使用<%= ...%>的表达式中来避免空值错误.除非将标签替换为<%...%>,否则我会收到错误消息.但是,在构建之后,我得到一个jsp错误而不是servlet错误.我不能真正在其中粘贴原始代码,但<%= ...%>中的代码本质上是检索作为Struts 1项目中的servlet参数传递的嵌套数组对象(更像另一个数组对象中的数组对象). .我只想添加一个try ... catch语句,以防对象的属性尚未实例化.

I more or less know the difference between <%! and <%, but I can't seem to find the difference between <%= and <%. I'm trying to avoid a null value error by introducing some logic into my expression that currently uses <%= ... %>. I get an error unless I replace the tags with <%...%>. However after my build I get a jsp error instead of the servlet error. I can't really paste my original code in here but the code inside <%= ... %> essentially retrieves a nested array object (more like array object within another array object) passed as a servlet argument in a Struts 1 project. I just want to add a try...catch statement in case the object's property isn't instantiated yet.

<%=((package.package.package.ClassName)session.getAttribute("attrName")).getObjectList()[0].getSecondObject.length%>; 

这是jsp问题,还是Struts 1问题?同样,这两个标签有什么区别?

Is this a jsp issue, or is it a Struts 1 issue? And again, what is the difference between the 2 tags?

推荐答案

<%...%>之间,您可以在Java中编写所需的任何逻辑.

Between <%...%>you can write any logic that you want in java.

使用<%=...%>会将括号之间的表达式结果输出到屏幕.因此,不用编写示例

Using <%=...%> will output the result of the expression between the brackets to the screen. So instead of writing for example

<% System.out.println("Hello World") %> 

您可以简单地写

<%= "Hello world" %> 

基本上,<%=%>的作用是调用要求值的表达式的toString()方法.

Basically, what <%= %> does is to call the toString() method of the expression that is being evaluated.

如果您需要按要求添加null检查逻辑,则可以使用

If you need to add null check logic as you said you need you can use

 <%..%>

以下是您可以参考的链接:

Here's a link you can refer to:

http://www.easywayserver.com/jsp/JSP-example.htm http://www.tutorialspoint.com/jsp/jsp_syntax.htm

这篇关于jsp表达式标签之间的差异&lt;%和&lt;%=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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