掲示板

How to write data from a form to a database

12年前 に Nico Schreiber によって更新されました。

How to write data from a form to a database

New Member 投稿: 16 参加年月日: 11/11/28 最新の投稿
Hi,

I have created a simple Liferay portlet with a form. In this form are displayed some userdata. By clicking the submit button I want to save the displayed userdata of
the current user in a database. To creat a database table I used the Liferay Service Builder. Everything works fine.
I read that I have to modify a file called ...LocalServiceImpl.java, which was created during the build process of the Service Builder.
What I have to do to save the data in the database?

Here ist my service.xml

<!--?xml version="1.0" encoding="UTF-8"?-->

<service-builder package-path="de.test">
	<author>Nico Schreiber</author>
	<namespace>Test</namespace>
 	<entity name="Antraege" local-service="true" remote-service="false">
        <column name="userId" type="String" primary="true"></column>
        <column name="firstName" type="String"></column>
        <column name="lastName" type="String"></column>
        <column name="email" type="String"></column>
        <column name="telephone" type="String"></column>
        <column name="department" type="String"></column>
        <column name="costCenter" type="String"></column>
        <column name="costCenterChief" type="String"></column>
        <column name="date" type="Date"></column>
        
        <order by="asc"> 
			<order-column name="userId" />
		</order>
        
        <finder name="FirstName" return-type="Collection">
            <finder-column name="firstName"></finder-column>
        </finder>
        <finder name="LastName" return-type="Collection">
            <finder-column name="lastName"></finder-column>
        </finder>
        <finder name="EMail" return-type="Collection">
            <finder-column name="email"></finder-column>
        </finder>
        <finder name="Telephone" return-type="Collection">
            <finder-column name="telephone"></finder-column>
        </finder>
        <finder name="Department" return-type="Collection">
            <finder-column name="department"></finder-column>
        </finder>
        <finder name="CostCenter" return-type="Collection">
            <finder-column name="costCenter"></finder-column>
        </finder>
        <finder name="CostCenterChief" return-type="Collection">
            <finder-column name="costCenterChief"></finder-column>
        </finder>
    </entity>
</service-builder>


Here is my bean to display the userdata of the current usere

package de.test.beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

@ManagedBean(name = "userdata")
@SessionScoped
public class Userdata implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String userId;
	private String firstName;
	private String lastName;
	private String eMail;
	private String telephone;
	private String department;
	private String costCenter;
	private String costCenterChief;
	
	public Userdata() throws PortalException, SystemException {
		FacesContext ctx = FacesContext.getCurrentInstance();
		String remoteUser = ctx.getExternalContext().getRemoteUser();
		User user = UserLocalServiceUtil.getUserById(Long.parseLong(remoteUser));
		userId = user.getScreenName(); 
		firstName = user.getFirstName();
		lastName= user.getLastName();
		eMail = user.getEmailAddress();	
		telephone = (String) user.getExpandoBridge().getAttribute("telephonenumber");
		department = (String) user.getExpandoBridge().getAttribute("department");
		costCenter = (String) user.getExpandoBridge().getAttribute("costCenter");
		costCenterChief = (String) user.getExpandoBridge().getAttribute("costCenterChief");		
	}
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public void seteMail(String eMail) {
		this.eMail = eMail;
	}

	public String geteMail() {
		return eMail;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getDepartment() {
		return department;
	}

	public void setCostCenter(String costCenter) {
		this.costCenter = costCenter;
	}

	public String getCostCenter() {
		return costCenter;
	}
	
	public void setCostCenterChief(String costCenterChief) {
		this.costCenterChief = costCenterChief;
	}

	public String getCostCenterChief() {
		return costCenterChief;
	}

}



Here is my view.xhtml

[...]

	<h:form>
	<p:fieldset legend="#{msg.userdata_head}"><br>
		<h:panelgrid columns="2" cellpadding="10" columnclasses="userdata, userdata_output" styleclass="userdata">
			<h:outputlabel for="userID" value="#{msg.userId}" />
			<h:outputtext id="userId" value="#{userdata.userId}" />
			<h:outputlabel for="firstName" value="#{msg.firstName}" />
			<h:outputtext id="firstName" value="#{userdata.firstName}" />				
			<h:outputlabel for="lastName" value="#{msg.lastName}" />
			<h:outputtext id="lastName" value="#{userdata.lastName}" />
			<h:outputlabel for="eMail" value="#{msg.eMail}" />
			<h:outputtext id="eMail" value="#{userdata.eMail}" />				
			<h:outputlabel for="telephone" value="#{msg.telephone}" />
			<h:outputtext id="telephone" value="#{userdata.telephone}" />
			<h:outputlabel for="dapartment" value="#{msg.department}" />
			<h:outputtext id="department" value="#{userdata.department}" />
			<h:outputlabel for="costCenter" value="#{msg.costCenter}" />
			<h:outputtext id="costCenter" value="#{userdata.costCenter}" />
			<h:outputlabel for="costCenterChief" value="#{msg.costCenterChief}" />
			<h:outputtext id="costCenterChief" value="#{userdata.costCenterChief}" />	
		</h:panelgrid>
	</p:fieldset>
	<br>

	[...]

	<h:commandbutton class="appointment_button_send" id="sendAppointment" value="#{msg.send_button}" action="view2.xhtml" />
	</h:form>


I hope anybody can help me.

Regards,
Nico
thumbnail
12年前 に Rajesh Chaurasia によって更新されました。

RE: How to write data from a form to a database

Regular Member 投稿: 183 参加年月日: 11/08/18 最新の投稿
In portlet class of your sample portlet add code on similar lines as given below:
package com.sify.npp.portlet;

import java.rmi.RemoteException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.sify.npp.businesslogic.model.Metadata;
import com.sify.npp.businesslogic.model.impl.MetadataImpl;
import com.sify.npp.businesslogic.service.MetadataLocalServiceUtil;

/**
* Portlet implementation class Metadata
*/
public class MetadataPortlet extends MVCPortlet {

public Metadata createMetadata(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException, RemoteException {
long metadataId = 0L;
metadataId = CounterLocalServiceUtil.increment(this.getClass().getName());
Metadata metadata = new MetadataImpl();
metadata.setListtypeid(metadataId);
metadata.setName("tintin");
metadata.setType_("com.sify.npp.businesslogic.model.Metadata");
MetadataLocalServiceUtil.addMetadata(metadata);
return metadata;
}

public List<Metadata> displayMetadata(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException, RemoteException {
int start=0;
int end=20;
List<com.sify.npp.businesslogic.model.Metadata> listMetadata = MetadataLocalServiceUtil.getMetadatas(start, end);
actionRequest.setAttribute("metadataList", listMetadata);
return listMetadata;
}
}


write jsp code whcih takes calls from UI to portlet class given above as:
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ include file="/html/metadata/init.jsp" %>
<h1>Create Metadata Form</h1>
<%
PortletURL updateMetadataURL = renderResponse.createActionURL();
updateMetadataURL.setParameter(ActionRequest.ACTION_NAME, "createMetadata");
%>
<aui:form name="fm" method="POST" action="<%=updateMetadataURL.toString() %>">
<aui:input name="name" label="Metadata Name:"/>
<aui:button type="submit" value="Save"/>
</aui:form>


Hope this helps emoticon