问题
I have string type method in java file it contain string array and when I try to call in jsp, it give me an error.
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]);
%>
Error:
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:
回答1:
The error says it all, your jsp is not finding actionRequest
and actionResponse
object.
These objects need to be included in the JSP by having this code at the top of your JSP:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
And as @RasabihariKumar correctly mentions, this is not the way how a Portlet
class should be used. For testing or learning, this may be fine but for real projects I don't think this is a good practice to do because these are expensive objects and it simply does not seem right to use a Portlet as a Utility class to process data like this, it breaks the principle of cohesion.
Portlet classes should be used to send request (by using renderURL
or actionURL
or resourceURL
) to and get the response just like we do for servlets.
Edit after his comment:
You can go through the liferay wiki to get useful links to learning resources, I would recommend the developer guide and the books Liferay in Action
and Portlet in Action
for the best way to develop portlets in liferay.
For now, the simplest way is to write code in doView
method of your portlet which would be called when your portlet JSP page is rendered, just retrieve the list from the database in your doView
and put in as a request attribute:
renderRequest.setAttribute("listAttr", listFromDatabase)
and then use this listAttr
in the JSP as:
String[] str = (String[]) renderRequest.getAttribute("listAttr");
Going through source code of sample portlets developed by liferay might also help.
来源:https://stackoverflow.com/questions/16170597/how-to-solve-error-of-jsp-page