如何在JSTL中调用java方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25198690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call java method in JSTL?
提问by Sachin J
This might be duplicate question.
这可能是重复的问题。
I just want to call method which is not getter or setter method eg. makeCall(someObj,"stringvalue") of xyz class.
我只想调用不是 getter 或 setter 方法的方法,例如。makeCall(someObj,"stringvalue") 的 xyz 类。
Java Class
Java类
Class XYZ{
public String makeCall("someValue1","stringValue2"){
//some logic here
}
}
JSTL
JSTL
<jsp:userBean id="xyz" class="com.XYZ"/>
${xyz.makeCall("hello","Friend")}
采纳答案by Sachin J
For resolve this we need create your own tag. (in .tld file)
为了解决这个问题,我们需要创建您自己的标签。(在 .tld 文件中)
and need to write one java class for this tag.
并且需要为此标签编写一个 java 类。
After this you can call method within that your own class and set result to pageCotext to retrive it on jsp.
在此之后,您可以在您自己的类中调用方法并将结果设置为 pageCotext 以在 jsp 上检索它。
回答by Braj
Simply create an object of the class using <jsp:useBean>
and call the method using JavaServer Pages Standard Tag Libraryor Expression Languagethat is more easy to use and less error prone.
只需使用更易于使用且不易出错的JavaServer Pages 标准标记库或表达式语言创建类的对象<jsp:useBean>
并调用该方法。
sample code:
示例代码:
<jsp:useBean id="test" class="com.x.y.z.XYZ"/>
${test.methodXYZ(object,"myString")}
Read more about Implicit Objectsthat might help you.
阅读有关可能对您有所帮助的隐式对象的更多信息。
回答by Balduz
Try with this:
试试这个:
<c:out value="${XYZbean.makeCall(someObjBean, 'value')}" />