Creating a form to add book
In this step we will further modify our update.jsp to define a simple form to add a book. Before the
“Go Back” link, let us have this code,
<form name="<portlet:namespace/>fm" method="POST" action="<%=updateBookURL.toString() %>">
Book Title: <input type="text" name="<portlet:namespace/>bookTitle" />
<br/>Author: <input type="text" name="<portlet:namespace/>author" />
<br/><input type="submit" value="Save" />
</form>
The interfaces “PortletURL” and “ActionRequest” will report problem. To get rid of them just add the following imports in your “init.jsp”.
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
Inside the JSP scriplet we have programmatically declared a variable “updateBookURL” which is of type “actionURL”. We have also set one attribute for this object, the ACTION_NAME.
Once you save all files and the portlet gets deployed, check the “Add Book” page and you will see something like this,
Enter some values and click “Add”, you will get some error on the page
“Portlet is temporarily unavailable.”
Let us check the eclipse console to know what is causing the problem,
Open the portlet class and add a new method – “updateBook”,
Now re-deploy the portlet and check the code in our “updateBook” method is being called properly.
Once you enter the details of a book and submit the form you should get the message on the
console,
Your inputs ==> Liferay In Action, Richard Sezov
Learnings from this chapter
1. RenderRequest and ActionRequest - difference
2. URL formation – declarative using tags and programmatic
3. <portlet:namespace/>
4. Did you notice the “ParamUtil” class. List down all other api's of this class.
Suite : http://rsuna.blogspot.com/2013/11/converting-simple-html-form-to-aui-form.html
In this step we will further modify our update.jsp to define a simple form to add a book. Before the
“Go Back” link, let us have this code,
<%
PortletURL updateBookURL = renderResponse.createActionURL();
updateBookURL.setParameter(
ActionRequest.ACTION_NAME, "updateBook");
%>
<form name="<portlet:namespace/>fm" method="POST" action="<%=updateBookURL.toString() %>">
Book Title: <input type="text" name="<portlet:namespace/>bookTitle" />
<br/>Author: <input type="text" name="<portlet:namespace/>author" />
<br/><input type="submit" value="Save" />
</form>
The interfaces “PortletURL” and “ActionRequest” will report problem. To get rid of them just add the following imports in your “init.jsp”.
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
Inside the JSP scriplet we have programmatically declared a variable “updateBookURL” which is of type “actionURL”. We have also set one attribute for this object, the ACTION_NAME.
Once you save all files and the portlet gets deployed, check the “Add Book” page and you will see something like this,
Enter some values and click “Add”, you will get some error on the page
“Portlet is temporarily unavailable.”
Let us check the eclipse console to know what is causing the problem,
It is clear from the message that the portal server is unable to find a method “updateBook”
In the next step let us see how and where to add this method.
Modify LibraryPortlet.java
Open the portlet class and add a new method – “updateBook”,
public void updateBook(ActionRequest actionRequest,
ActionResponse actionResponse)
throws IOException, PortletException {
String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
String author = ParamUtil.getString(actionRequest, "author");
System.out.println("Your inputs ==> " + bookTitle + ", " + author);
}
Now re-deploy the portlet and check the code in our “updateBook” method is being called properly.
Once you enter the details of a book and submit the form you should get the message on the
console,
Your inputs ==> Liferay In Action, Richard Sezov
Learnings from this chapter
1. RenderRequest and ActionRequest - difference
2. URL formation – declarative using tags and programmatic
3. <portlet:namespace/>
4. Did you notice the “ParamUtil” class. List down all other api's of this class.
Suite : http://rsuna.blogspot.com/2013/11/converting-simple-html-form-to-aui-form.html



0 commentaires:
Enregistrer un commentaire