Announcement Announcement Module
Collapse
No announcement yet.
onclick event doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • onclick event doesn't work

    I've developed a jsp which has hyperlinks say (edit , delete, ManageMembers). The onClick event on those hyperlinks doesn't seem to work. Below is my jsp code.

    Code:
    <form:form commandName="sgObj" method="post" enctype="application/x-www-form-urlencoded">      
    <h2>Social Group</h2><br>
    	<!-- side bar left -->
    	<%@ include file="/WEB-INF/jsp/mosonexSidebarMenu.jsp" %>
    	<!-- side bar left ends -->
    	<!-- start content -->
    	<div id="sidebarmiddle">
    
                <h2>Here are the social groups you created.</h2><br><br>
                    <input type="hidden" name="sgObjArr" value="" />
                 <table align="center" border="2" >
                     <% 
                               if((null != sgArr)  && sgArr.length > 0) {                          
                     %>
                     <tr>
                         <th>Sl No.</th>
                         <th>Social Group Name</th
                         <th></th>
                         <th></th>
                         <th></th>
                     </tr>
                         <% 
                                   for(int i=0; i< sgArr.length ; i++) {
                                   
                             %>
                             <tr>
                             
                         <td><%=(i+1) %>
                          <input type="hidden" name="actionvalue"  value=""  />
                         </td>
                         <td><%=sgArr[i].getSgName() %>
                         </td>
                         <td><a  onclick='document.forms[0].sgObjArr.value="<%= sgArr[i].getSgId() %>"; alert(document.forms[0].sgObjArr.value);' href='<portlet:renderURL>
    						<portlet:param name="action" value="editSocialGroup"/>
                                                     </portlet:renderURL>'  >Edit</a></td>
                         <td><a onclick='document.forms[0].sgObjArr.value="2"; alert(document.forms[0].sgObjArr.value);' href='<portlet:renderURL>
    						<portlet:param name="action" value="deleteSocialGroup"/>
                                                     </portlet:renderURL>' >Delete</a></td>
                         <td><a  onclick='document.forms[0].sgObjArr.value="<%= sgArr[i].getSgId() %>"; alert(document.forms[0].sgObjArr.value);' href='<portlet:renderURL>
    						<portlet:param name="action" value="manageSocialGroupMembers"/>
                                                     </portlet:renderURL>' >ManageMembers</a></td>
                     </tr>
                     <% } %>
                       
                     <% } else {%>
                     <tr><td colspan="5">No Social Groups found</td></tr>
                     
                     <%} %>
                     
                 </table>
    <br>
    	<br>
    
    	<br>
    
    	<br>
    
    
    <br>
    
    	</div>
    	<!-- end content -->
    	<!-- end sidebar -->
    </form:form>
    Can anyone tell me what's wrong with my jsp? It will be a GREAT help to me.

    Basically i want to get the social group id when the user clicks on either edit or delete or ManageMembers hyperlinks. Is there nay other ways to do? PLease help.

  • #2
    After changing the form definition, onclick event is working. But, the i'm unable to get the value of hidden variable in controller.

    Here is my new form:

    Code:
    <form name="sgForm" id="sgForm" method="post" action=""  >
        <input type="hidden" name="sgid" value="" />
    <h2>Social Group</h2><br>
    	<!-- side bar left -->
    	<%@ include file="/WEB-INF/jsp/mosonexSidebarMenu.jsp" %>
    	<!-- side bar left ends -->
    	<!-- start content -->
    	<div id="sidebarmiddle">
    
                <h2>Here are the social groups you created.</h2><br><br>
                    
                 <table align="center" border="2" >
                     <% if((null != sgArr)  && sgArr.length > 0) { %>
                     <tr>
                         <th>Sl No.</th>
                         <th>Social Group Name</th
                         <th></th>
                         <th></th>
                         <th></th>
                     </tr>
                     <% for(int i=0; i< sgArr.length ; i++) { %>
                     <tr>                         
                         <td><%=(i+1) %></td>
                         <td><%=sgArr[i].getSgName() %></td>
                         <td><a  onclick='document.forms[0].sgid.value="<%= sgArr[i].getSgId() %>"; alert(document.forms[0].sgid.value);' href='<portlet:renderURL>
    						<portlet:param name="action" value="editSocialGroup"/>
                                                     </portlet:renderURL>'  >Edit</a>&nbsp;&nbsp;</td>
                         <td><a onclick='document.forms[0].sgid.value="<%= sgArr[i].getSgId() %>"; alert(document.forms[0].sgid.value);' href='<portlet:renderURL>
    						<portlet:param name="action" value="deleteSocialGroup"/>
                                                     </portlet:renderURL>' >Delete</a>&nbsp;&nbsp;</td>
                         <td><a  onclick='document.forms[0].sgid.value="<%= sgArr[i].getSgId() %>"; alert(document.forms[0].sgid.value);' href='<portlet:renderURL>
    						<portlet:param name="action" value="manageSocialGroupMembers"/>
                                                     </portlet:renderURL>' >ManageMembers</a>&nbsp;&nbsp;</td>
                     </tr>
                     <% }  } else {%>
                     <tr><td colspan="5">No Social Groups found</td></tr>                 
                     <%} %>
                     
                 </table>
    <br>
    	<br>
    
    	<br>
    
    	<br>
    
    
    <br>
    
    	</div>
    	<!-- end content -->
    </form>
    Here is my controller:

    Code:
       public class DeleteSocialGroupController extends AbstractController  {
        
        
        private RegistrationService regService;
    
        //@Override
        //protected void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException arg3) throws Exception {
        public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
            System.out.println("Entering DeleteSocialGroupController -> onSubmitAction");  
            
            String sgId = request.getParameter("sgid");
            System.out.println("Entering DeleteSocialGroupController -> onSubmitAction -------> sgId -------> "+ sgId);
            
            boolean flag = regService.deleteSocialGroup(sgId);
            
            Registration regObj = (Registration) request.getPortletSession(false).getAttribute("regObj");
            
            SocialGroup[] sgArr = regService.getSocialGroups(regObj);        
            
            System.out.println("Leaving DeleteSocialGroupController -> onSubmitAction");
            //response.setRenderParameter("action","inviteFriendsInfo");
            return new ModelAndView("viewSocialGroup", "sgObjArr", sgArr);
        }
        
    
        public void setRegService(RegistrationService regService) {
            this.regService = regService;
        }
    }

    Please help me to get sgid value from jsp to controller. ANy help is highly appreicated.

    THanks so much.

    Comment


    • #3
      Well the onclick doesn't submit the form so the hidden input field won't be submitted. You are effectively just following a standard hyperlink with the only parameter being passed is the action parameter. You could either pass the sgid as an extra portlet parameter on the url, or change the onclick to also submit the form.

      From a design perspective, you should avoid updating state in the render phase. The render phase can be executed multiple times so you don't want to redo the state changes. Move this logic to the action phase.

      Also, for HTML avoid using things like forms[0] to reference objects in the DOM. This is really vulnerable to any changes in your HTML structure and if other portlets on the same page also have forms then this will probably break straight away. Consider using id's instead - for portlets you can use <portlet:namespace/> to prefix these ids to ensure they are unique when multiple portlets are on a page.

      Hope this helps,

      Chris
      Last edited by cmelgar; Oct 13th, 2008, 04:54 AM. Reason: Typos

      Comment


      • #4
        Thanks Chris for the valuable inputs. Sure to follow them.

        I tried submitting the form in onclick event but no luck.

        SO, i'm passing sgid as one of the parameter in portlet url. Strange thing is: i'm able to get that sgid in DeleteSocialGroupController which was attached in the preious post. However, i'm unable to get the sgid in editsocialgroupcontroller. I'm attaching both jsp and controller for your reference. KIndly go thro' the same let me know if i'm doing something wrong.

        jsp is here:

        Code:
        <form name="sgForm" id="sgForm" method="post" action=""  >
            <input type="hidden" name="sgid" value="" />
        <h2>Social Group</h2><br>
        	<!-- side bar left -->
        	<%@ include file="/WEB-INF/jsp/mosonexSidebarMenu.jsp" %>
        	<!-- side bar left ends -->
        	<!-- start content -->
        	<div id="sidebarmiddle">
        
                    <h2>Here are the social groups you created.</h2><br><br>
                        
                     <table align="center" border="2" >
                         <% if((null != sgArr)  && sgArr.length > 0) { %>
                         <tr>
                             <th>Sl No.</th>
                             <th>Social Group Name</th
                             <th></th>
                             <th></th>
                             <th></th>
                         </tr>
                         <% for(int i=0; i< sgArr.length ; i++) { %>
                         <tr>                         
                             <td><%=(i+1) %></td>
                             <td><%=sgArr[i].getSgName() %></td>
                             <td><a  href='<portlet:renderURL>
        						<portlet:param name="action" value="editSocialGroup"/>
                                                         <portlet:param name="sgID" value="<%= sgArr[i].getSgId() %>" />
                                                         </portlet:renderURL>'  >Edit</a>&nbsp;&nbsp;</td>
                             <td><a href='<portlet:renderURL>
        						<portlet:param name="action" value="deleteSocialGroup"/>
                                                         <portlet:param name="sgid" value="<%= sgArr[i].getSgId() %>" />
                                                         </portlet:renderURL>' >Delete</a>&nbsp;&nbsp;</td>
                             <td><a  href='<portlet:renderURL>
        						<portlet:param name="action" value="manageSocialGroupMembers"/>
                                                         <portlet:param name="SGID" value="<%= sgArr[i].getSgId() %>" />
                                                         </portlet:renderURL>' >ManageMembers</a>&nbsp;&nbsp;</td>
                         </tr>
                         <% }  } else {%>
                         <tr><td colspan="5">No Social Groups found</td></tr>                 
                         <%} %>
                         
                     </table>
        <br>
        	<br>
        
        	<br>
        
        	<br>
        
        
        <br>
        
        	</div>
        	<!-- end content -->
        <!--</form:form>-->
        </form>
        Controller is here .......

        Code:
        public class EditSocialGroupController extends SimpleFormController {
            
            
            private RegistrationService regService;
            
        
            public void setRegService(RegistrationService regService) {
                this.regService = regService;
            }
            
        
            public EditSocialGroupController() { }
        
                protected void onSubmitAction(ActionRequest request, ActionResponse response,
            Object command, BindException errors) throws Exception {
                
                System.out.println("Entered EditSocialGroupController ----> onSubmitAction");
         
                SocialGroup sgObj = (SocialGroup) command;
                String sgID = request.getParameter("sgID");
                sgObj.setSgId(sgID);
                 System.out.println("Entered EditSocialGroupController ----> onSubmitAction -----> sgid -------> " + sgID);
                 
                regService.updateSocialGroup(sgObj);
                        
                System.out.println("Leaving EditSocialGroupController ----> onSubmitAction");
            }
        
        @Override
            protected ModelAndView onSubmitRender(RenderRequest req, RenderResponse res, Object command, BindException errors) throws Exception {
                
                Registration regObj = (Registration) req.getPortletSession(false).getAttribute("regObj");
                
                SocialGroup[] sgArr = regService.getSocialGroups(regObj);
                
                return new ModelAndView("viewSocialGroup", "sgObjArr", sgArr);
            }
        
            @Override
            protected Object formBackingObject(PortletRequest request) throws Exception {
                //return super.formBackingObject(arg0);
                
                String sgId = request.getParameter("sgID");
                 System.out.println("Entered EditSocialGroupController ----> formBackingObject -----> sgid -------> " + sgId);
                SocialGroup sg = regService.getSocialGroup(sgId);
                sg.setSgId(sgId);
                
                return sg;
            }
        }
        Once again, thank you so much for all the inputs. I really appreciate very much.

        Comment

        Working...
        X