Java 如何从jsp页面引用用户函数

Java 如何从jsp页面引用用户函数,java,jsp,scriptlet,Java,Jsp,Scriptlet,我用板条箱包装了一个简单的助手,它根据控制器名称、操作名称和参数列表生成一个超链接 public static String actionLink(String text, String controller, String action, String[] args) { String result, URI = ""; URI = controller + "/" + action + "/"; for (int i = 0; i < args.length;

我用板条箱包装了一个简单的助手,它根据控制器名称、操作名称和参数列表生成一个超链接

public static String actionLink(String text, String controller, String action, String[] args) {
String result, URI = "";

    URI = controller + "/" + action + "/";
        for (int i = 0; i < args.length; i++) {
        URI = URI + args[i];
    }

    result = "<a href=\"" + URI + "\">" + text + "</a>";

    return result;

}
publicstaticstringactionlink(字符串文本、字符串控制器、字符串操作、字符串[]参数){
字符串结果,URI=“”;
URI=控制器+“/”+操作+“/”;
对于(int i=0;i
我从jsp页面调用它的函数:

<%@page import="com.ACME.mvc3.helpers.Utils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<c:forEach items="${model}" var="item">
      <tr>          
        <td height="20"><p><%=Utils.actionLink( ${item.name}, "topic", "", ${item.id} )%></p></td>
      </tr>
</c:forEach>

当显示网页时,会引发异常。我应该修复什么来使用我的助手函数

异常文本:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
Syntax error, insert ")" to complete MethodInvocation
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
Syntax error, insert ";" to complete Statement
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
$ cannot be resolved to a variable
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
item.name cannot be resolved to a type
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
An anonymous class cannot subclass the final class String
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
item.id cannot be resolved to a type
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 26 in the jsp file: /WEB-INF/views/home.jsp
Syntax error on token ")", { expected
23:             <td height="10"><hr width="100%" size="2" /></td>
24:           </tr>
25:           <tr>          
26:             <td height="20"><p><%=${Utils.actionLink(item.name, "topic", "", new String(item.id) )}%></p></td>
27:             <td width = 10%>
28:             </td>
29:           </tr>


An error occurred at line: 130 in the generated java file
Syntax error, insert "}" to complete Block

An error occurred at line: 130 in the generated java file
Syntax error, insert "while ( Expression ) ;" to complete BlockStatements
org.apache.jasper.jaspereException:无法为JSP编译类:
jsp文件:/WEB-INF/views/home.jsp的第26行出现错误
语法错误,请插入“)”以完成方法调用
23:
27: 28: 29: jsp文件:/WEB-INF/views/home.jsp的第26行出现错误 语法错误,请插入“;”以完成语句 23:
27: 28: 29: jsp文件:/WEB-INF/views/home.jsp的第26行出现错误 无法将$解析为变量 23:
27: 28: 29: jsp文件:/WEB-INF/views/home.jsp的第26行出现错误 无法将item.name解析为类型 23:
27: 28: 29: jsp文件:/WEB-INF/views/home.jsp的第26行出现错误 匿名类不能对最终的类字符串进行子类化 23:
27: 28: 29: jsp文件:/WEB-INF/views/home.jsp的第26行出现错误 无法将item.id解析为类型 23:
27: 28: 29: jsp文件:/WEB-INF/views/home.jsp的第26行出现错误 令牌“)”上的语法错误,{应为 23:
27: 28: 29: 生成的java文件的第130行出现错误 语法错误,插入“}”以完成块 生成的java文件的第130行出现错误 语法错误,请插入“while(Expression);”以完成BlockStatements
更好的方法是使用JSTL函数

您应该在
WEB-INF/yoursite.tld中定义它:

<function>
    <name>escape</name>
    <function-class>com.foo.util.WebUtils</function-class>
    <function-signature>String escape(java.lang.String)</function-signature>
</function>

逃跑
com.foo.util.WebUtils
字符串转义(java.lang.String)

然后您可以将其用作
${ys:escape(str)}
(导入名称空间后。查找任何.tld文件以查看其其余部分的外观)

我找到了解决问题的方法。JSP以以下方式查看:

<%@page import="com.epam.mvc3.helpers.Utils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<c:forEach items="${model}" var="item">
   <!-- The following line defines the type of the item variable -->
   <jsp:useBean id="item" type="com.ACME.mvc3.model.Topic"/>
   <tr>
       <!-- The following line doesn't need curly braces -->          
       <td height="20"><p><%=Utils.actionLink(item.getName(), "topic", "details") %></p></td>
   </tr>
</c:forEach>


异常消息/堆栈跟踪将非常有用。您是否尝试添加包:
复制并粘贴异常消息,以便我们也可以看到它。