如何从jsp-page引用用户功能

时间:2012-03-10 15:59:43

标签: 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; i++) {
        URI = URI + args[i];
    }

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

    return result;

}

我正在从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

2 个答案:

答案 0 :(得分:2)

更好的方法是使用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>

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

答案 1 :(得分:1)

我找到了解决问题的方法。 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>