Pass parameter from java class to jsp using ActionRequest Actionresponse

吃可爱长大的小学妹 提交于 2019-12-11 18:22:27

问题


I have a function say :

 public void display(ActionRequest areq, ActionResponse ares) throws Exception,PortletException,IOException {

 String name= areq.getParameter("name");
 String add= areq.getParameter("add");
 String phone= areq.getParameter("phone");
}

I have a jsp say disp.jsp which passes the user input to the above function display. Now I after doing some processing on the above data in display() function, I want to display the results on a jsp page say new.jsp. How should I go ahead with it? I tried something like:

areq.setAttribute("name",name);
areq.getRequestDispatcher("new.jsp").forward(areq, aresp); but it shows an error that getRequestDispatcher is not defined for actionrequest and actionresponse.

I am using liferay framework


回答1:


In your action you can set attributes and set the redirect page like that:

public void display(ActionRequest aReq, ActionResponse aResp){


    aReq.setAttribute("name",name);

    aResp.setRenderParameter("jspPage", "/new.jsp");
}

I Usually prefer setting attributes instead of Parameters, because it allows to pass non Sting variables. Then, in the jsp you can get the attributes

<%
String name = (String)renderRequest.getAttribute("name");   
%>

Just remember to include this, to have access to renderRequest object

<portlet:defineObjects />


来源:https://stackoverflow.com/questions/15986199/pass-parameter-from-java-class-to-jsp-using-actionrequest-actionresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!