【问题标题】:how to solve error of jsp page?jsp页面报错怎么解决?
【发布时间】:2013-04-16 17:47:33
【问题描述】:

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

【问题讨论】:

  • 这是调用带有参数ActionRequest 和ActionResponse 的方法的一种非常有趣的方式。我真的很想知道你在这里试图实现的这个美妙的用例是什么:-)。您能否粘贴整个堆栈跟踪,例如此错误出现在哪一行。谢谢
  • @RasabihariKumar 显示以上版本

标签: java eclipse jsp liferay


【解决方案1】:

错误说明了一切,您的 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之后编辑:

您可以通过 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");

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

【讨论】:

  • 然后告诉我我应该做什么...我有portlet 类,我尝试使用结果集从数据库获取记录到portlet 类。现在我想向网络浏览器显示我的所有数据。我不能在 jsp 文件中写我的这个逻辑.. 这就是为什么我使用 portlet 类作为业务逻辑和 jsp 文件用于显示目的。请指导我..如何做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 2023-01-30
  • 2014-02-07
  • 2022-01-25
相关资源
最近更新 更多