如何解决jsp页面错误? [英] how to solve error of jsp page?
问题描述
我在 java 文件中具有字符串类型方法,该方法包含字符串数组,当我尝试在 jsp 中调用时,它给我一个错误.
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]);
%>
错误:
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
对象.
The error says it all, your jsp is not finding actionRequest
and actionResponse
object.
这些对象需要包含在JSP中,方法是将这些代码放在JSP的顶部:
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 />
正如@RasabihariKumar正确提到的那样,这不是应该使用Portlet
类的方式.对于测试或学习,这可能很好,但是对于实际项目,我认为这不是一个好习惯,因为它们是昂贵的对象,并且使用Portlet作为Utility类来处理这样的数据似乎并不正确. ,它违反了 cohesion 的原理.
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类将请求发送(通过使用renderURL
或actionURL
或resourceURL
)并获得响应,就像对servlet一样.
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.
您可以浏览liferay 维基,要获得学习资源的有用链接,我将推荐开发人员指南以及Liferay in Action
和Portlet in Action
书籍,以获取在liferay中开发portlet的最佳方法.
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.
目前,最简单的方法是在portlet的doView
方法中编写代码,在呈现portlet JSP页面时会调用该代码,只需从doView
中的数据库中检索列表,然后将其作为请求属性:
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)
,然后在JSP中将此listAttr
用作:
and then use this listAttr
in the JSP as:
String[] str = (String[]) renderRequest.getAttribute("listAttr");
浏览示例Portlet 也可能有帮助.
这篇关于如何解决jsp页面错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!