Friday 23 March 2012

Create a Form and submit the values to the database

After you have created service buider as I mentioned in my previous post.
1. Go to your view.jsp and create a form.For now, I will be using html <form> tag

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<portlet:defineObjects />

<form action='<portlet:actionURL/>' method="post">
    <table>
        <tr>
            <td>Fname:</td>
            <td><input type="text" name="fname" /></td>
        </tr>
        <tr>
            <td>Lname:</td>
            <td><input type="text" name=lname " /></td>
        </tr>
        <tr>
            <td>Gender:</td>
            <td><select name="gender" >
            <option value="0">Male</option>
            <option value="1">Female</option>
            </select></td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><input type="text" name="address" /></td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td><input type="text" name="phone" /></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type="text" name="email" /></td>
        </tr>
        <tr>
            <td>Date of Birth:</td>
            <td><input type="text" name="dob" /></td>
        </tr>
        <tr>
            <td colspan=2><input type="submit" value="submit" /></td>
        </tr>
    </table>
</form>

2. Now open your .java file and add the following method
     
public void processAction(
            ActionRequest actionRequest, ActionResponse actionResponse)
        throws IOException, PortletException {

      String fname=actionRequest.getParameter("fname");
      String lname=actionRequest.getParameter("lname");
      Date dob=new Date(actionRequest.getParameter("dob"));
      int gender=Integer.parseInt(actionRequest.getParameter("gender"));
      String address=actionRequest.getParameter("address");
      long phone=Long.parseLong(actionRequest.getParameter("phone"));
      String email=actionRequest.getParameter("email");
     
      empLocalServiceUtil.add(fname, lname, dob, gender, address, phone,
            email);
   
    }

   
3. After this go to the xxLocalServiceImpl and add the following method.

public void add(String fname, String lname, Date dob, int gender,
            String address, long phone, String email) {
        try {
            emp employee =  empLocalServiceUtil
                    .createemp(CounterLocalServiceUtil.increment());
            employee.setFname(fname);
            employee.setLname(lname);
            employee.setDob(dob);
            employee.setGender(gender);
            employee.setAddress(address);
            employee.setPhone(phone);
            employee.setEmail(email);
            empLocalServiceUtil.updateemp(employee);
        } catch (SystemException e) {
            e.printStackTrace();
        }


4. Build-Services again. Refresh the project.

5. Deploy the portlet.

Ummhmmm.... it feels great to see the portlet helping us, taking our values to the database..NO????

Thanks

8 comments:

  1. Hi Priyanka,
    How do I check a attribute of a table if it exist? For example, I have a table named "User". In this table, I create 2 columns, first is the primary key, namely "UserID", and the second is "Name". I want to check if there is a user name called, for instance, "Aqua". Please help me!

    ReplyDelete
    Replies
    1. so you want to know search...in other words? then just implement finder in service.xml

      Delete
    2. i cant see my liferay table in mysql database
      i use liferay tomcate 6.1 in ubuntu

      Delete
  2. very well explained,just the material what i needed .Thank you very much

    ReplyDelete
  3. when i put portal-ext.properties in classes folder i cant login in liferay..
    but when i delete portal-ext.properties and try to run it work fine...
    i want to create table using service.xml and i want to see where is my table in mysql database...
    pls help me ... as soon as posible


    From... Kapil

    ReplyDelete
    Replies
    1. Please provide the content of your portal-ext.properties

      Delete
  4. where we have to write the processAction method

    ReplyDelete
  5. And what are the changes we have to do in service.xml file after using processAction method

    ReplyDelete