ndmhta
ndmhta

Reputation: 536

Multiple Render and Action methods in Spring MVC portlet in Liferay

I'm creating a portlet which will take user name in form and display message Welcome in the same portlet after submission of form. For this I've used params in render method.

Note: I'm using spring-MVC with liferay 6.2.

The problem is when I submit name, it redirects to the same page instead of calling another render method.

Here is my view.jsp:

 <portlet:actionURL var="actionOneMethodURL">
       <portlet:param name="action" value="getUserName">
    </portlet:param>
    </portlet:actionURL>
   <form action="${actionOneMethodURL}"   method="post">
    Enter Your Name: 
        <input type="text" name="userName" />
        <input type="submit" value="OK!" />
    </form>

Here is controller code:

public class PortletController implements Controller {

    private static final String WELCOME_PAGE = "welcomeUser";

    public void handleActionRequest(ActionRequest request,
            ActionResponse response) throws Exception { 
    }

    @ActionMapping(params = "action=getUserName")
    public void actionOneMethod(ModelMap model, ActionRequest request,
            ActionResponse response) {
        String userName = request.getParameter("userName");
        model.addAttribute("userName", userName);
        response.setRenderParameter("action", "displayName");
    }

    @RenderMapping
    public ModelAndView handleRenderRequest(RenderRequest request,
            RenderResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("helloWorldMessage", "Hello There!");
        return new ModelAndView("helloWorld", model);
    }

    @RenderMapping(params = "action=displayName")
    public String displayName(ModelMap model, RenderRequest request,
            RenderResponse response) throws Exception { 
        return WELCOME_PAGE;
    }
}

Can someone help??

EDIT: Can this happen due to any xml file?? If so which one?

Upvotes: 3

Views: 2793

Answers (2)

tomic
tomic

Reputation: 613

Here is part of code should work:

view.jsp : Name of this form should tell which one of the methods use in controller.

<portlet:actionURL name="actionOneMethod" var="actionOneMethodURL"/>
<form:form action="${actionOneMethodURL}">
<!-- your form body... -->
</form:form>

Controller.java : The one change I did is commented some handler method, and changed your action method to be used by its name. Rest looks fine.

public class PortletController implements Controller {

    private static final String WELCOME_PAGE = "welcomeUser";

    //I dont think you need this part at all...
    /**public void handleActionRequest(ActionRequest request,
            ActionResponse response) throws Exception { 
    }**/

    @ActionMapping("actionOneMethod")
    public void actionOneMethod(ModelMap model, ActionRequest request,
            ActionResponse response) {
        String userName = request.getParameter("userName");
        model.addAttribute("userName", userName);
        response.setRenderParameter("action", "displayName");
    }

    @RenderMapping
    public ModelAndView handleRenderRequest(RenderRequest request,
            RenderResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("helloWorldMessage", "Hello There!");
        return new ModelAndView("helloWorld", model);
    }

    @RenderMapping(params = "action=displayName")
    public String displayName(ModelMap model, RenderRequest request,
            RenderResponse response) throws Exception { 
        return WELCOME_PAGE;
    }
}

I assume you have correct view resolver configured. Here is part of in case:

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean> 

I think the part of commented method caused problem. Hope it helps.

Upvotes: 1

Miroslav Ligas
Miroslav Ligas

Reputation: 1307

I would guess that the problem may be with the param name. In the action try to set a different render parameter. response.setRenderParameter("page","welcome") Have a look here simple-spring-portlet

Upvotes: 0

Related Questions