Forums de discussion

Action of a form

Phuc Minh Bui, modifié il y a 15 années.

Action of a form

Junior Member Publications: 83 Date d'inscription: 02/02/09 Publications récentes
Dear all,
I try a sample portlet that allows user to get the sum of 2 number.
<form action="[b]????[/b]" method="POST">
<table border="1">
                <tbody><tr>
                    <td>First param</td>
                    <td><input type="text" name="param1" value=""></td>
                </tr>
                <tr>
                    <td>Second param</td>
                    <td><input type="text" name="param2" value=""></td>
                </tr>
        </tbody></table>
         <input type="submit" value="Sum">
</form>

I do not know what to fill in attribute 'action' (see ????)

I have a portlet class named AddSample

I have tried to supply the attributes 'action' the value as action = "AddSample", but it's wrong emoticon.
So please tell me what we should fill in the attribute action="????

P/S: I just use simple JSP, not Struts

Thanks,

Phuc Bui
thumbnail
Sonu Singh, modifié il y a 15 années.

RE: Action of a form

New Member Publications: 8 Date d'inscription: 02/01/09 Publications récentes
Hi Phuc Bui ,
Check this code

<form action="<portlet:actionURL />" method="post" name="<portlet:namespace />fm" onSubmit="submitForm(document.<portlet:namespace />fm); return false;">
<input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.ADD %>">
<input name="<portlet:namespace />redirect" type="hidden" value="<%= portletURL.toString() %>">

<table class="lfr-table">
<tbody><tr>
	<td>
		First Param
	</td>
	<td>
           <input type="text" name="param1" value="">
	</td>
</tr>
</tbody></table>

<br>

<input type="submit" value="<liferay-ui:message key=" add">" /&gt;

</form>
Phuc Minh Bui, modifié il y a 15 années.

RE: Action of a form

Junior Member Publications: 83 Date d'inscription: 02/02/09 Publications récentes
Hi Sonu Singh,
Sorry, I am a newcomer to Liferay, I do not understand your code well !!!
But I did it

<form action="<portlet:actionURL>                       
                      </portlet:actionURL>" method="POST" name="<portlet:namespace/>fm">
                          
        <table border="1">
                <tbody><tr>
                    <td>So hang thu nhat</td>
                    <td><input type="text" name="<portlet:namespace/>param1" value=""></td>
                </tr>
                <tr>
                    <td>So hang thu hai</td>
                    <td><input type="text" name="<portlet:namespace/>param2" value=""></td>
                </tr>
        </tbody></table>
        <input type="submit" value="Tinh">

        &lt;%
        String result = (String)request.getAttribute("result");
        if(result != null){
            out.println(result);
        }
        %&gt;
 </form>


It has gone to AddSample class,

public class AddSample extends GenericPortlet {

 public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
        System.out.print("I did it !!! ");
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");
        System.out.print("Param1 = " + param1);
        System.out.print("Param2 = " + param2);
        try {
            int result = Integer.parseInt(param1) + Integer.parseInt(param2);
            System.out.print("Result = " + result);
            request.setAttribute("result", result);
            getPortletContext().getRequestDispatcher("/WEB-INF/jsp/AddSample_result.jsp");
        } catch (NumberFormatException e) {
            //do nothing
        }

    }

    @Override
    public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/AddSample_view.jsp").include(request, response);
    }
..........


but I have got new error when I try to forward to another page (result page)

java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:384)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:366)
at com.sun.portal.portletcontainer.driver.DriverServlet.doGet(DriverServlet.java:101)
at com.sun.portal.portletcontainer.driver.DriverServlet.doPost(DriverServlet.java:110)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:272)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)


How to solve this ???

Thanks

Phuc Bui
Lam Le, modifié il y a 14 années.

RE: Action of a form

New Member Publications: 10 Date d'inscription: 16/06/09 Publications récentes
Hey Phuc,
I am Lam, Vietnamese too. I am working in a project about liferay also. I hope that we can get in touch to share our knowledge about this issue. Please give me a message: lebaolam8891@yahoo.com
Thanks.
thumbnail
Kolja Köster, modifié il y a 14 années.

RE: Action of a form

Regular Member Publications: 164 Date d'inscription: 11/05/09 Publications récentes
Phuc Minh Bui:

Sorry, I am a newcomer to Liferay, I do not understand your code well !!!

I am new to Liferay myself, but perhaps I can give a hint...
Phuc Minh Bui:




actionURLS can be used to trigger the processAction() Method in the java code of the portlet. When processAction() has finished it triggers doView() automatically. doView() is always triggered, when the portlet needs to be redrawn. You can trigger doView() directly by using an renderURL instead of an actionURL. My actionURL in a working example looks like this:
"
         method="POST"&gt;
         Parameter: <input type="text" name="value2"> <br>
      <input type="submit" name="Search">
   

The only thing I try to access in my java backend is 'value2'. I do not put a value to 'value2' because I want to grab whatever entered in the text field here, so your line in your JSP File:
<input type="text" name="<portlet:namespace/>param1" value="">

perhaps is not correct, but I do not know for sure.

In your AddSample Class you try to dispatch to a JSP File directly in your processAction() Method. But after that, doView() will be triggered, which will render a JSP itself. Apart from that, doView() will dispatch with the parameters from a renderRequest (and renderResponse), while dispatching from processAction() will give the parameters from a actionRequest (and actionResponse). So this looks wrong to me, and what perhaps causes your exception. Perhaps you should split logic and display on those two methods:
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
        System.out.print("I did it !!! ");
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");
        System.out.print("Param1 = " + param1);
        System.out.print("Param2 = " + param2);
            if (param1 != null &amp;&amp; param2 != null) { //check what kind of action happenend those values will only be not null when triggered
                                                                                 //through the form on the view.jsp
                 try {
                    int result = Integer.parseInt(param1) + Integer.parseInt(param2);
                   System.out.print("Result = " + result);
                   response.setRenderParameter("result",result); //the result of the addition of the two values is given as a parameter to the
                                                                                                         //doView method in this way. Perhaps you have to convert / cast the int to 
                                                                                                         //String first, I am not sure
                 } catch (NumberFormatException e) {
            //do nothing
                }
        }
}

In the doView Method we must now check, if there is a renderParameter named "result". If there is, we dispatch it to a result.jsp. If not, we just do not care and render our view.jsp again:
public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		renderResponse.setContentType("text/html");
		String res = renderRequest.getParameterValues("result");  //check for a value named 'result'
		if (res != null) {                                                                                 //if not null, we will proceed..
			renderRequest.setAttribute("result, res);
                        getPortletContext().getRequestDispatcher("/WEB-INF/jsp/AddSample_result.jsp"); // i am not sure, if dispatching is done
                                                                                                                                                                             //like that. portlets from the plugins
                                                                                                                                                                             //SDK use a predefined funktion for
                                                                                                                                                                             //that:
			//include(displayResultsJSP, renderRequest, renderResponse);
		} else {
			include(viewJSP, renderRequest, renderResponse);
		}
	}

I hope this helps a little bit. Watch my code samples closely however, I might have put millions of typos in it, since I edited source from one of my portlets to fit your example.

Regards
Kolja
Phuc Minh Bui, modifié il y a 14 années.

RE: Action of a form

Junior Member Publications: 83 Date d'inscription: 02/02/09 Publications récentes
Hi Kolja,
Thanks for your reply, I help me understand clearly.
I just study it by example so I do not understand it well.

Cheers,
Phuc Bui
Driss BENMOUMEN, modifié il y a 10 années.

RE: Action of a form

New Member Publications: 2 Date d'inscription: 10/03/14 Publications récentes
Hi all,

I just wanted to ask u about the portlet MVC framework.
I did my view.jsp like this:

<form method="post" action="<portlet:actionURL><portlet:param name=" Beans.java" value="mainview">"
        name="<portlet:namespace />fm"&gt;
       
                <table>
                    <tbody><tr>
                        <td> Entrez le nom de la discipline<b> (Unique)*</b></td>
                        <td><input type="text" name="Nom_discipline">
                        </td>
                    </tr>
                    <tr>
                        <td>Entrez une description de la discipline</td>
                        <td><input type="text" name="Desc_discipline">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" Value="Ajouter">
                        </td>
                    </tr>   
                </tbody></table>
        </form>


then I created a servlet Beans.java like that:

public class Beans extends MVCPortlet {
    public void doView (RenderRequest renderRequest,
          RenderResponse renderResponse) throws IOException, PortletException {
    renderResponse.setContentType("text/html");
    PrintWriter out = renderResponse.getWriter();
      String Nom = renderRequest.getParameter("Nom_discipline");
      String Desc = renderRequest.getParameter("Desc_discipline");
      Control cr = new Control();
      try {
        cr.addDiscipline(Nom, Desc);
    } catch (SQLException e) {
        e.printStackTrace();
    }
      out.println("");
      out.println("");
      out.println("<title>");
      out.println("</title>");
      out.println("");
      out.println("");
      out.println("<h4>Insertion réussie !!</h4>");
      out.println("");
      out.println("");
}
}


The view.jsp doesn't redirect me to this servlet.
please I'd like u to help me.


Thank u alot !