0
votes

I have migrated to spring portlet MVC 4.0.2 for writing portlets. Everything is ok and just like Spring Portlet MVC 3, but when I pass the form values from JSP

Page to the (action or render) method I cannot get that values. The ActionRequest.getParameter() and RenderRequest.getParameter() methods in the controller always return null value (meanwhile I have many textbox on the JSP page).

My controller class:

 package org.basa.maskan.controller;

 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.portlet.ModelAndView;
 import org.springframework.web.portlet.bind.annotation.ActionMapping; 
 import org.springframework.web.portlet.bind.annotation.RenderMapping;

 import javax.portlet.ActionRequest;
 import javax.portlet.ActionResponse;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 import java.util.HashMap;
 import java.util.Map;


 @Controller
 @RequestMapping("VIEW")
 public class mdnApptest {

 @RenderMapping
  public ModelAndView simpleRenderMethod(RenderRequest renderRequest, RenderResponse  
  renderResponse) {


    ModelAndView view = new ModelAndView("page");

    return view;

   }
  @RequestMapping(method = RequestMethod.POST)
  @ActionMapping(params = "page=form_set_values")
  public void simpleActionMethod(ActionRequest actionRequest, ActionResponse
  actionResponse) {

    String n = actionRequest.getParameter("firstname"); 

    printParams("ACTION PARAMETERS:", actionRequest.getParameterMap());

    System.out.println("Act *********************  this is the inserted name of the   
    user :  " + n); 

      actionResponse.setRenderParameter("page", "test");
   }

@RenderMapping(params = "page=test")
public ModelAndView renderMethod(RenderRequest renderRequest,                        
                                     RenderResponse   renderResponse) {

    String n = renderRequest.getParameter("name");
    printParams("Render PARAMETERS:" ,renderRequest.getParameterMap());
    System.out.println("*************" + n);


    return new ModelAndView("page2");
}

    private void printParams(String pefix, Map<String, String[]> map) {
       for (String sk : map.keySet()) {
          String[] val = map.get(sk);
          String rv = "";
          for (String vv : val) {
            rv = rv + vv + " , ";
        }
        System.out.println(pefix + " -> " + sk + " = " + rv);
       }

     }
  }

My JSP:

   <%@ page import="java.text.Format" %>
   <%@ page import="java.text.SimpleDateFormat" %>
   <%@ page import="java.util.Set" %>
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
   <%@ page contentType="text/html;charset=UTF-8" language="java" %>
   <%@include file="./include.jsp"%>


   <%
    PortletURL url = renderResponse.createActionURL();

    url.setParameter("page","form_set_values");

   %>
  <form  action="<%=url.toString()%>" method="post">
  First name: <input type="text" name="firstname"><br>
  Last name: <input type="text" name="lastname">
   <input type="submit" value="Submit"/>
  </form>

The n object is always null, is there any new concept in Spring that I have not used?

2
You should past your whole controller class and how you are binding request to form data! - tmarwen

2 Answers

0
votes

First change the way you are creating your action url as follows:

<portlet:actionURL var="formSetValuesURL">  
  <portlet:param name="action" value="formSetValuesAction"></portlet:param>  
</portlet:actionURL>

Then replace the form action field with the above generated one:

<form  action="${formSetValuesURL}" method="post">
  First name: <input type="text" name="firstname"><br>
  Last name: <input type="text" name="lastname">
  <input type="submit" value="Submit"/>
</form>

The last step comes to your controller mapped method:

@ActionMapping(params = "action=formSetValuesAction")
  public void simpleActionMethod(ActionRequest actionRequest, 
    ActionResponse actionResponse,
    @RequestParam("firstname") String firstName) {

  System.out.println("Here you have your fist user: "+firstName); 
  actionResponse.setRenderParameter("page", "test");
}
0
votes

I found the answer, in the newer version of the liferay you should add the following tag to the liferay-portlet.xml file:

    <requires-namespaced-parameters>false</requires-namespaced-parameters>

now you can build and deploy the portlet.

cheers