Reputation: 41
I am following the following example: https://github.com/liferay/liferay-blade-samples/blob/7.1/maven/apps/configuration-action/src/main/java/com/liferay/blade/samples/configurationaction/MessageDisplayConfigurationAction.java I have multiple submit buttons in my form. I'd like to handle them in multiple methods. This is possible in Spring MVC Portlet. For example, you can do this: Attach an onlclick event of the button to a function that looks like this:
function <portlet:namespace />addGroup(){
var url = "<portlet:actionURL portletMode='edit'><portlet:param name='action' value='addGroup'/></portlet:actionURL>";
submitForm(document.<portlet:namespace />fm, url);
}
And in the code we can do like this:
@RequestMapping("EDIT")
@ActionMapping(params = "action=addGroup")
public void handleAddGroup(ActionRequest actionRequest, ActionResponse response) throws ResearchLibraryException, Exception {
PortletPreferences preferences = actionRequest.getPreferences();
// Add something to preferences
preferences.store();
}
How to do the same thing in Liferay MVC. I would like to use multiple methods. Right now, I can do only one method and switch based on the condition and identify the different clicks.
Upvotes: 0
Views: 876
Reputation: 96
This shouldn't be much different with Liferay MVC portlets. You should invoke a Liferay RenderCommand or ActionCommand. Check out https://portal.liferay.dev/docs/7-0/tutorials/-/knowledge_base/t/mvc-render-command or https://portal.liferay.dev/docs/7-0/tutorials/-/knowledge_base/t/mvc-action-command
For example, the action command will be its own OSGi component:
@Component(
immediate = true,
property = {
"javax.portlet.name=your_portlet_name_YourPortlet",
"mvc.command.name=/your/jsp/action/url"
},
service = MVCActionCommand.class
)
public class YourMVCActionCommand extends BaseMVCActionCommand {
// implement your action
}
with this URL generation:
<portlet:actionURL name="/your/jsp/action/url" />
Upvotes: 2
Reputation: 48057
In addition to Jan's good answer: Even the portlet spec allows you to have multiple action methods with a standardized annotation. This has been made even easier with LiferayMVC portlets, in that you can even omit the annotation and just use the method name as action name:
// this is the action method defined by the portlet spec (2.0)
@ProcessAction(name="doSomething")
public void doSomething(ActionRequest request, ActionResponse response) {
log.info("doSomething");
}
// this works in Liferay MVC, the method name is the action name
public void doSomethingElse(ActionRequest request, ActionResponse response) {
log.info("doSomethingElse");
}
You'll generate the URLs like this, for example in a JSP:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:actionURL name="doSomething" var="doSomethingURL" />
<a href="<%=doSomethingURL %>">do something</a>
<br/>
<portlet:actionURL name="doSomethingElse" var="doSomethingElseURL" />
<a href="<%=doSomethingElseURL %>">do something else</a>
On top of that, you can create ActionCommands and RenderCommands, as Jan mentions. This will break up the functionality even more, into individual classes. IMHO that's preferable (as it makes them all independent of each other and smaller)
A condition to get this functionality is to not override processAction
, as its default implementation tries to find annotated methods, or methods with the required interface.
Upvotes: 0