Question

I've crated a simple helper, which produces a hyperlink from controller's name, action's name and the parameters list.

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;

}

And i'm calling on it function from jsp page:

<%@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>

And the exception is raised, when the web-page is displayed. What should I fix to use my helper-function?

Exception text:

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
Was it helpful?

Solution 2

I've found the way out with the problem. The JSP is looking in the following way:

<%@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>

OTHER TIPS

A better way would be to use JSTL functions.

You should just define it in 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>

And then you can use it as ${ys:escape(str)} (after importing the namespace. Look for any .tld file to see how the rest of it looks like)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top