user1457957
user1457957

Reputation: 305

Portlet:actionurl is not going to @Actionmapping in controller

I am using Liferay portal-6 with Spring-3.

In my portlet, first it goes to the @Rendermapping without params and displays the default jsp, when I click on a button I am passing the actionurl, but its not going to the corresponding @Actionmapping.

My searchForm.jsp looks like this:

<portlet:actionURL var="showSearchResultsUrl">
    <portlet:param name="myaction" value="searchResults" />
</portlet:actionURL>

<form:form id="searchForm" name="searchForm" commandName="patientStoryForm"    method="POST" action="${showSearchResultsUrl}" enctype="multipart/form-data" >
    <input type="button" name="search_btn" value="Search"/></td>
</form:form>

My controller is like this:

@Controller(value = "searchPatientStoryController")
@RequestMapping(value = "VIEW")
@SessionAttributes(types = PatientStoryForm.class)
public class SearchPatientStoryController {

    @RenderMapping
    public String showSearchForm(RenderResponse response, Model model) {
        return "searchForm";
    }

    @RenderMapping(params = "myaction=searchResultsForm")
    public String showSearchResultsForm(RenderResponse response, Model model) {
        return "searchResultsForm";
    }

    @ActionMapping(params = "myaction=searchResults")
    public void searchResults(
        @ModelAttribute(value = "patientStoryForm") PatientStoryForm patientStoryForm,
        BindingResult bindingResult, ActionResponse response, ActionRequest request,
        SessionStatus sessionStatus, Model model) {

        response.setRenderParameter("myaction", "searchResultsForm");
    }
}

When I click on button in searchForm.jsp it is supposed to go to @Actionmapping but it is not going.

Please help me.

Thanks in advance.

Upvotes: 0

Views: 5717

Answers (3)

Uresh K
Uresh K

Reputation: 1136

You would need to submit the form in order to invoke the associated action method.

However, in the jsp code, I see the below code which indicates that it is just a button.

<input type="button" name="search_btn" value="Search"/>

In order to submit the form, you can do either of the following ways:

  1. Use a submit button instead of a normal form button like this:

    <input type="submit" name="search_btn" value="Search"/>

  2. Invoke a javascript function on click event of the button and submit the form:

<script type="text/javascript">
    function submitForm(form){
        var actionUrl = '<portlet:actionURL var="showSearchResultsUrl"><portlet:param name="myaction" value="searchResults"/></portlet:actionURL>';
        form.action = actionUrl;
        form.submit();
    }
</script>

and

<input type="button" name="search_btn" value="Search" onclick="javascript:submitForm(this.form)"/>

Upvotes: 2

Adrian Zaharia
Adrian Zaharia

Reputation: 115

You need to set the action of your form, something like this:

<form:form action="<%=showSearchResultsUrl%>" id="searchForm" name="searchForm" commandName="patientStoryForm"    method="POST" action="${showSearchResultsUrl}" enctype="multipart/form-data" >
<input type="button" name="search_btn" value="Search"/></td>

This way your action name will be "myaction" with the value "searchResults" as your mapping on the controller side.

Upvotes: 0

Sandeep Nair
Sandeep Nair

Reputation: 3660

Can you try replacing your actionURL like the following

<portlet:actionURL name="searchResults" var="showSearchResultsUrl"> 
        <portlet:param name="myaction" value="searchResults" /> 
</portlet:actionURL>

Upvotes: 0

Related Questions