我已经贬低了一个简单的帮助程序,它从控制器名称,动作的名称和参数列表中生成一个超链接。

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

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>

其他提示

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)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top