在一个jsp文件中创建java函数并从另一个jsp文件中调用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18823581/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:23:56  来源:igfitidea点击:

create java function in a jsp file and call it from another jsp file

javajspjakarta-ee

提问by Prathap

We generally create methods in a java class, import them into a jsp file and call those methods in our jsp files.

我们通常在 java 类中创建方法,将它们导入一个 jsp 文件并在我们的 jsp 文件中调用这些方法。

But we are working in a client environment, we do not have access to create or modify .java files. So we desperately need to create a function in a jsp file and call it from another jsp file.

但是我们在客户端环境中工作,我们无权创建或修改 .java 文件。所以我们迫切需要在一个jsp文件中创建一个函数并从另一个jsp文件中调用它。

For example:

例如:

A.jsp

jsp

.....
<jsp:include page="B.jsp"/>
....
<%= getName(); %>

B.jsp ....

B.jsp ....

<%!
public String getName()
{
 return "Hello";
}
>%

Is there any way to do this?.

有没有办法做到这一点?

采纳答案by commit

Yes you can, instead of

是的,你可以,而不是

<jsp:include page="B.jsp"/>

Use

<%@include file="B.jsp"%>

Including page will just embed two jsp code so you are not getting that function but including file using directives will embed code first and then compile so you will get your function.

包含页面只会嵌入两个 jsp 代码,因此您不会获得该功能,但包含文件 using 指令将首先嵌入代码,然后进行编译,因此您将获得您的功能。

You can find difference here

你可以在这里找到不同之处

What is the difference between <jsp:include page = ... > and <%@ include file = ... >?

<jsp:include page = ... > 和 <%@ include file = ... > 有什么区别?

回答by Prasad Kharkar

You should not create a function in Jspfile. JSP's are meant for view purpose only .

您不应在Jsp文件中创建函数。JSP 仅用于查看目的。

You can write the function in separate java classand call that class from whatever Jsppages you want.

您可以单独编写该函数,java class然后从Jsp您想要的任何页面调用该类。

回答by Waqas Memon

Above comments are all valid. Do not do this. Its a bad design. But, if you just what to know any possible way of doing this, it can be using static includes of the JSP.

以上评论都是有效的。不要这样做。它的设计很糟糕。但是,如果您只是知道这样做的任何可能方式,则可以使用 JSP 的静态包含。

You can use <%@include %> directive to include JSP fragments

您可以使用 <%@include %> 指令来包含 JSP 片段

<%@include file="B.jsp" %> 

in A.jsp

在 A.jsp 中

The good design would be for you to create a Class in java and write all your methods in it, include it in all ur JSPs, and use the methods.

好的设计是让您在 java 中创建一个类并在其中编写所有方法,将其包含在所有 JSP 中,并使用这些方法。

Other people are confused with similar questions, like how to call a JS function in one JSP/HTML to another JSP/HTML, the answer remains same. The good design would be to use a .js file to write all JS Methods.

其他人对类似的问题感到困惑,例如如何将一个 JSP/HTML 中的 JS 函数调用到另一个 JSP/HTML,答案保持不变。好的设计是使用 .js 文件来编写所有 JS 方法。

回答by Bimalesh Jha

Above comments are all valid. However, if you must do this, I would improve it a bit by putting all functions in a separate file and call it methods.incand then include it in jsp file like

以上评论都是有效的。但是,如果您必须这样做,我会通过将所有函数放在一个单独的文件中并调用它methods.inc然后将其包含在 jsp 文件中来改进它,例如

<%@include file="methods.inc" %>

<%@include file="methods.inc" %>

This will help you to know the intention clearly and look somewhat cleaner too.

这将帮助您清楚地了解意图,并且看起来也更干净一些。