我在 java 文件中有字符串类型的方法,它包含字符串数组,当我尝试在 jsp 中调用时,它给我一个错误。
public String[] ordering(ActionRequest actionRequest,ActionResponse actionResponse)
throws IOException,PortletException
JSP:
<%
TestiPortlet obj=new TestiPortlet();
String str[]=obj.ordering(actionRequest,actionResponse);
out.println(str[0]);
%>
错误:
Multiple annotations found at this line:- actionResponse cannot be resolved to a variabl-actionRequest cannot be resolved to a variable
Stacktrace:
javax.portlet.PortletException: org.apache.jasper.JasperException: An exception occurred processing JSP page /html/testi/list.jsp at line 8
5:
6: <%
7: TestiPortlet obj=new TestiPortlet();
8: String str[]=obj.ordering(actionRequest,actionResponse);
9: out.println(str[0]);
10: %>
11:
最佳答案
错误说明了一切,您的 jsp 没有找到 actionRequest
和 actionResponse
对象。
这些对象需要包含在 JSP 中,方法是将以下代码放在 JSP 的顶部:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
正如@RasabihariKumar 正确提到的那样,这不是 Portlet
类的使用方式。对于测试或学习,这可能很好,但对于实际项目,我认为这不是一个好的做法,因为这些都是昂贵的对象,使用 Portlet 作为实用程序类来处理这样的数据似乎是不正确的, 它打破了 cohesion 的原则.
Portlet 类应该像我们一样用于发送请求(通过使用 renderURL
或 actionURL
或 resourceURL
)并获得响应用于 servlet。
在他的comment之后编辑:
你可以通过 liferay wiki 以获得有用的学习资源链接,我会推荐开发人员指南和书籍 Liferay in Action
和 Portlet in Action
以获得在 liferay 中开发 portlet 的最佳方式。
目前,最简单的方法是在 portlet 的 doView
方法中编写代码,当呈现 portlet JSP 页面时将调用该方法,只需从 中的数据库中检索列表doView
并作为请求属性放入:
renderRequest.setAttribute("listAttr", listFromDatabase)
然后在 JSP 中使用此 listAttr
作为:
String[] str = (String[]) renderRequest.getAttribute("listAttr");
查看 sample portlets 的源代码由 liferay 开发的也可能有所帮助。
关于java - jsp页面报错怎么解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170597/