如何解决jsp页面的错误?

Kap*_*pil 1 java eclipse jsp liferay

我在java文件中有字符串类型方法,它包含字符串数组,当我尝试在jsp中调用时,它给了我一个错误.

public String[] ordering(ActionRequest actionRequest,ActionResponse actionResponse)  
    throws IOException,PortletException
Run Code Online (Sandbox Code Playgroud)

JSP:

<% 
  TestiPortlet obj=new TestiPortlet();
  String str[]=obj.ordering(actionRequest,actionResponse);
  out.println(str[0]);
%>
Run Code Online (Sandbox Code Playgroud)

错误:

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: 
Run Code Online (Sandbox Code Playgroud)

Pra*_*h K 5

错误说明了一切,你的jsp没有发现actionRequestactionResponse反对.

通过将此代码放在JSP的顶部,需要将这些对象包含在JSP中:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<portlet:defineObjects />
Run Code Online (Sandbox Code Playgroud)

正如@RasabihariKumar正确提到的那样,这不是Portlet应该如何使用类的方式.对于测试或学习,这可能没问题但是对于实际项目我不认为这是一个很好的做法,因为这些是昂贵的对象,并且使用Portlet作为Utility类来处理这样的数据似乎是不对的. ,它打破了凝聚力的原则.

Portlet类应该用于发送请求(通过使用renderURLactionURLresourceURL)来获取响应,就像我们对servlet一样.

评论后编辑:

您可以通过 wiki获取有用的学习资源链接,我会推荐开发人员指南和书籍Liferay in Action以及Portlet in Action在liferay中开发portlet的最佳方法.

现在,最简单的方法是在doViewportlet的方法中编写代码,这些代码将在呈现portlet JSP页面时调用,只需从您的数据库中检索列表doView并将其作为请求属性放入:

renderRequest.setAttribute("listAttr", listFromDatabase)
Run Code Online (Sandbox Code Playgroud)

然后listAttr在JSP中使用它:

String[] str = (String[]) renderRequest.getAttribute("listAttr");
Run Code Online (Sandbox Code Playgroud)

通过liferay开发的示例portlet的源代码也可能有所帮助.