Question

J'ai citée une aide simple, qui produit un hyperlien du nom du contrôleur, du nom de l'action et de la liste des paramètres.

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;

}

et j'appelle la fonction informatique de la page 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>

et l'exception est soulevée lorsque la page Web est affichée.Que dois-je résoudre pour utiliser ma fonction d'assistance?

Texte d'exception:

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

Était-ce utile?

La 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>

Autres conseils

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)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top