How can I call the action method inside the MVCPor

2019-03-03 18:36发布

问题:

I am using Liferay 6 for development.

I have added JQuery support to Liferay this way inside the file liferay-portlet.xml file

<header-portlet-javascript>https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js</header-portlet-javascript>
         <header-portlet-javascript>https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.min.js</header-portlet-javascript>

Now I have a form with Username and password as shown below:

  <form>
    <div>
        <input class="default-value" type="text" name="Name" value="Name" />
        <input class="default-value" type="text" name="Password" value="Password" />
    </div>
    </form>

<div class="fieldgroup">
 <input type="submit" value="Register" class="submit">
</div>

<liferay-portlet:actionURL name="registerUser" var="registerUserURL"></liferay-portlet:actionURL>

I have a CustomPortlet as shown below , and added this inside the portlet.xml file as shown

 <portlet>
    <portlet-name>Second</portlet-name>
    <display-name>Second</display-name>
    <portlet-class>com.SecondPort</portlet-class>


public class SecondPort extends MVCPortlet {

public void registerUser(ActionRequest request, ActionResponse response) 
{
// Some code here with respect to the registerUser .
}
}

Now please tell me on click of the Submit Button on the JSP Page, how can I call this registerUser method??

 <input type="submit" value="Register" class="submit">

Please let me know, thanks in advance .

回答1:

For your jsp you're close. Needs a bit of reordering, using the actionURL and the portlet's namespace. Warning - typed in stackoverflow answer window, didn't actually compile/run:

<liferay-portlet:actionURL name="registerUser" var="registerUserURL"></liferay-portlet:actionURL>
<form action="<%=registerUserURL%>">
    <div>
        <input class="default-value" type="text" name="<portlet:namespace/>Name" value="Name" />
        <input class="default-value" type="text" name="<portlet:namespace/>Password" value="Password" />
    </div>
</form>

<div class="fieldgroup">
 <input type="submit" value="Register" class="submit">
</div>

As you extend Liferay's MVC portlet the portlet side looks ok - just use request.getParameter("Name"); there.

This is not related to jQuery at all, right?



回答2:

Don't forget to add the following on the top in your jsp:

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>