0 Replies Latest reply: Jun 16, 2009 1:12 AM by 801486 RSS

    Output problem

    801486
      Hi,
      Im trying to display the name, age and gender of student in view mode (ie., accesing the values from PortletReferences) and in Edit mode, it has to ask the values of Stud name, stud age, stud gender..I have written code for it..But when Im trying to run it, that portlet showing an error saying "An error has occured"..i was unable to get displayed portlet in my page. Please see the code below and tell me the mistakes I have done..


      Here Student.java
      package com.fiables.student.gui;
      
      import java.io.Serializable;
      
      public class Student implements Serializable{
           
           String name;
           int age;
           String gender;
           public String getName() {
                return name;
           }
           public void setName(String name) {
                this.name = name;
           }
           public int getAge() {
                return age;
           }
           public void setAge(int age) {
                this.age = age;
           }
           public String getGender() {
                return gender;
           }
           public void setGender(String gender) {
                this.gender = gender;
           }
      }
      Here StudentPortlet.java
      package com.fiables.student.gui;
      
      import javax.portlet.*;
      
      import java.io.IOException;
      import java.io.PrintWriter;
      
      public class StudentPortlet extends GenericPortlet {
           PortletPreferences prefs;
           String name;
           Integer age;
           String gender;
           
           public void processAction(ActionRequest arequest,ActionResponse aresponse) throws  IOException {
                prefs = arequest.getPreferences();
                
                /* name = prefs.getValue("name", "Hari");
                 age = Integer.parseInt(prefs.getValue("age", "21"));
                 gender = prefs.getValue("gender", "male");*/
                
                //Edit
           if(arequest.getPortletMode().equals(PortletMode.EDIT)) {
                name = arequest.getParameter("name");
                age = Integer.parseInt(arequest.getParameter("age"));
                gender = arequest.getParameter("gender");
                
                if(name == null)
                     name = prefs.getValue("name", "Hari");
                else if(age < 0)
                     age = Integer.parseInt(prefs.getValue("age", "21"));
                else if(gender == null)
                     gender = prefs.getValue("gender", "male");
                
                
                try {
                     prefs.setValue("name",name);
                     prefs.setValue("age",age.toString());
                     prefs.setValue("gender", gender);
                } catch (ReadOnlyException e1) {
                     // TODO Auto-generated catch block
                     System.out.println("This is Read Only Exception...");
                }
                
                
                 try {
                      prefs.store();
                     aresponse.setPortletMode(PortletMode.VIEW);
                } catch (PortletModeException e) {
                     // TODO Auto-generated catch block
                     System.out.println("This is PortletMode Exception...");
                } catch (ValidatorException e) {
                     // TODO Auto-generated catch block
                     System.out.println("This is Validator Exception...");
                }
                
                
           }
           
           /*//View
           if(arequest.getPortletMode().equals(PortletMode.VIEW)) {
                
                Student std = new Student();
                std.setName(prefs.getValue("name", "Hari"));
                std.setAge(Integer.parseInt(prefs.getValue("age", "21")));
                std.setGender(prefs.getValue("gender", "male"));
           }*/
                
           }
      
        public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
          response.setContentType("text/html");
          prefs = request.getPreferences();
          
          Student std = new Student();
          std.setName(prefs.getValue("name", "Hari"));
          std.setAge(Integer.parseInt(prefs.getValue("age", "21")));
          std.setGender(prefs.getValue("gender", "male"));
          
          request.setAttribute("student", std);
          
       //   super.doView(request, response);
      
          PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/jsp/view.jsp");
          rd.include(request, response);
        }
      
        public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {
          response.setContentType("text/html");
      
          PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/jsp/edit.jsp");
          rd.include(request, response);
          
          
        }
      
      }
      Here View.jsp
      <%@page import="javax.portlet.PortletPreferences"%>
      <%@page import="javax.portlet.RenderResponse"%>
      <%@page import="com.fiables.student.gui.Student"%>
      <%@page import="javax.portlet.filter.ActionResponseWrapper"%>
      <%@page import="javax.portlet.RenderRequest"%>
      <%@ taglib uri="http://java.sun.com/portlet"; prefix="portlet" %>
      
      <jsp:useBean id="student" class="com.fiables.student.gui.Student" scope="request"/>
      
      <%
           RenderRequest req = (RenderRequest)request.getAttribute("javax.portlet.RenderRequest");
           Student stud = (Student) req.getAttribute("student");
      
       %>
      
      <html>
           <body>
                <center><h1>Student Information</h1></center><br><br>
                
                <table>
                     <thead>
                          <tr>
                               <th>Name</th>
                               <th>Age</th>
                               <th>Gender</th>
                          </tr>
                     </thead>
                     
                     <tbody>
                          <tr>
                               <td><%= stud.getName() %></td>
                               <td><%= stud.getAge()%></td>
                               <td><%= stud.getGender() %></td>
                          </tr>
                     
                     </tbody>
                
                </table>
                
                
           </body>
      </html>
      Here Edit.jsp
      <html>
           <body>
                <form action="<portlet:actionURL/>" method="post">
                Enter the Students Name <input type="text" name="name"><br>
                Enter the Students age <input type="text" name="age"><br>
                Enter the Students Gender <input type="text"  name="gender"><br>
                <input type="submit" value="Update">
                </form>
           </body>
      </html>
      Here portlet.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
                version="1.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd">
                
           <portlet>
              <description>Student Details</description>
                
              <portlet-name>StudentPortlet</portlet-name>
                
              <display-name>Student Details Portlet</display-name>
                
              <portlet-class>com.fiables.student.gui.StudentPortlet</portlet-class>
      
              <supports>
                     <mime-type>text/html</mime-type>
                  <portlet-mode>VIEW</portlet-mode>
                  <portlet-mode>EDIT</portlet-mode>
                  <portlet-mode>HELP</portlet-mode>
                  <portlet-mode>CONFIGURE</portlet-mode>
                </supports>
      
              <supported-locale>en</supported-locale>        
      
                <portlet-info>
                     <title>Sample Student Portlet</title>
                     <short-title>Student</short-title>
                     <keywords>Student</keywords>
              </portlet-info>
              
              <portlet-preferences>
                   <preference>
                        <name>name</name>
                        <value>Harishwar</value>
                   </preference>
                   <preference>
                        <name>age</name>
                        <value>22</value>
                   </preference>
                   <preference>
                        <name>gender</name>
                        <value>male</value>
                   </preference>
              </portlet-preferences>
           </portlet>
       </portlet-app>
      As far as I know , somewhere code went wrong on StudentPortlet.java and view.jsp...