gpt4 book ai didi

jsp - 在 JSP 中声明函数?

转载 作者:行者123 更新时间:2023-12-02 20:55:46 26 4
gpt4 key购买 nike

我来自 PHP 世界,在 PHP 页面中间声明函数非常简单。我尝试在 JSP 中执行相同的操作:

public String getQuarter(int i){
String quarter;
switch(i){
case 1: quarter = "Winter";
break;

case 2: quarter = "Spring";
break;

case 3: quarter = "Summer I";
break;

case 4: quarter = "Summer II";
break;

case 5: quarter = "Fall";
break;

default: quarter = "ERROR";
}

return quarter;
}

我收到以下错误:

An error occurred at line: 20 in the jsp file: /headers.jsp
Illegal modifier for the variable getQuarter; only final is permitted return;

最佳答案

您需要将其括在 <%! %> 如下:

<%!

public String getQuarter(int i){
String quarter;
switch(i){
case 1: quarter = "Winter";
break;

case 2: quarter = "Spring";
break;

case 3: quarter = "Summer I";
break;

case 4: quarter = "Summer II";
break;

case 5: quarter = "Fall";
break;

default: quarter = "ERROR";
}

return quarter;
}

%>

然后您可以在脚本或表达式中调用该函数:

<%
out.print(getQuarter(4));
%>

<%= getQuarter(17) %>

关于jsp - 在 JSP 中声明函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/826932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com