Pages

Wednesday, 10 April 2013

example for edit mode

After creating the project you r suppose to configure the edit mode in portlet.xml
EX:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<init-param>
<name>view-jsp</name>
<value>/html/editing/view.jsp</value>
</init-param>
<init-param>
<name>edit-jsp</name>
<value>/html/editing/edit.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
</supports>
add the following code in view jsp
1
2
3
4
<jsp:useBean id="userName" class="java.lang.String" scope="request"/> #1 
<p>Hello <%=userName %>!</p>
#1 Gets the bean out of the request
add the following code in edit jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
 <jsp:useBean class="java.lang.String" id="addNameUrl" scope="request" />
 <portlet:defineObjects />
 <form
  id = "<portlet:namespace />helloForm"
  action="<%=addNameUrl %>"
  method="post">
  <table>
   <tr>
   <td>Name:</td>
   <td><input type="text" name="username"></td>
   </tr>
  </table>
  <input type="submit" id="nameButton" title="Add Name" value="Add Name">
 </form>

the complete java code for the portlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.liferayinaction.portlet;
 import java.io.IOException;
 import javax.portlet.ActionRequest;
 import javax.portlet.ActionResponse;
 import javax.portlet.GenericPortlet;
 import javax.portlet.PortletException;
 import javax.portlet.PortletMode;
 import javax.portlet.PortletPreferences;
 import javax.portlet.PortletRequestDispatcher;
 import javax.portlet.PortletURL;
 import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 public class HelloYouPortlet extends GenericPortlet {
  public void init() throws PortletException {
   editJSP = getInitParameter("edit-jsp");
   viewJSP = getInitParameter("view-jsp");
  }
  public void doEdit(RenderRequest renderRequest,
   RenderResponse renderResponse)
   throws IOException, PortletException {
   renderResponse.setContentType("text/html");
   PortletURL addName = renderResponse.createActionURL();
   addName.setParameter("addName", "addName");
   renderRequest.setAttribute("addNameUrl", addName.toString());
   include(editJSP, renderRequest, renderResponse);
  }
  public void doView(RenderRequest renderRequest,
   RenderResponse renderResponse)
   throws IOException, PortletException {
   PortletPreferences prefs = renderRequest.getPreferences();
   String username = (String) prefs.getValue("name", "no");
   if (username.equalsIgnoreCase("no")) {
    username = "";
   }
   renderRequest.setAttribute("userName", username);
   include(viewJSP, renderRequest, renderResponse);
  }
  public void processAction(
   ActionRequest actionRequest,
   ActionResponse actionResponse)
   throws IOException, PortletException {
   String addName = actionRequest.getParameter("addName");
   if (addName != null) {
    PortletPreferences prefs =
     actionRequest.getPreferences();
    prefs.setValue(
     "name", actionRequest.getParameter("username"));
    prefs.store();
    actionResponse.setPortletMode(PortletMode.VIEW);
   }
  }
  protected void include(
   String path, RenderRequest renderRequest,
   RenderResponse renderResponse)
   throws IOException, PortletException {
   PortletRequestDispatcher portletRequestDispatcher =
    getPortletContext().getRequestDispatcher(path);
   if (portletRequestDispatcher == null) {
    _log.error(path + " is not a valid include");
   } else {
    portletRequestDispatcher.include(
    renderRequest, renderResponse);
   }
  }
  protected String editJSP;
  protected String viewJSP;
  private static Log _log = LogFactory.getLog(HelloYouPortlet.class);
 }




No comments:

Post a Comment