如何解决jsp页面的错误?

时间:2013-04-23 13:27:16

标签: java eclipse jsp liferay

我在 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: 

1 个答案:

答案 0 :(得分:5)

错误说明了一切,你的jsp没有找到actionRequestactionResponse对象。

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

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

<portlet:defineObjects />

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

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

comment

之后编辑

您可以浏览 wiki以获取有用的学习资源链接,我建议使用开发人员指南以及书籍Liferay in ActionPortlet in Action,以便了解开发portlet的最佳方法的liferay。

目前,最简单的方法是在portlet的doView方法中编写代码,在呈现portlet JSP页面时调用该方法,只需从doView中的数据库中检索列表,作为请求属性输入:

renderRequest.setAttribute("listAttr", listFromDatabase)

然后在JSP中使用此listAttr

String[] str = (String[]) renderRequest.getAttribute("listAttr");

通过liferay开发的sample portlets源代码也可能有所帮助。

相关问题