Simple Form Tutorial

Here is a very basic tutorial on creating a simple input form to add a record to a database. It leaves a lot to the reader. For example, you would want to have nicer messages for duplicate ids and you wouldn't want your error messages to display internal database information such as table names and column names. If the record was added sucessfully, you would probably want the page to navigate elsewhere.

1. Drop three Label components on the left side of the page, one under the other. Set the label text to ID:, Name:, and Description: respectively.

2. Drop three Text Field components on the page, one next to each label. Set the ids for the components to idTF, nameTF, and descriptionTF respectively.

4. Set the required property to true for each Text Field component.

6. Drop three Message components on the page, one next to each text field.

7. Ctrl-Shift-Drag from each Text Field component to its associated label.

8. Ctrl-Shift-Drag from each Text Field component to its associated message.

9. Drop a Button component on the page, under the text fields. Set its text to Add. Set its id to addButton.

10. Drop a Message Group component on the page to the right of the button. Set its showGlobalOnly property to true.

11. Access the Servers window. Drag Data Sources > Travel > Tables > TRIPTYPE and drop it on a blank spot on the page.

11. Double-click the button to access its addButton_action() method. Replace the method's body with the following code shown in bold.

    public String addButton_action() {
        if ( triptypeDataProvider.canAppendRow() ) {
            try {
                RowKey rowKey = triptypeDataProvider.appendRow();
                triptypeDataProvider.setCursorRow(rowKey);
                triptypeDataProvider.setValue("TRIPTYPE.TRIPTYPEID",
                        rowKey,  idTF.getText());
                triptypeDataProvider.setValue("TRIPTYPE.NAME",
                        rowKey, nameTF.getText());
                triptypeDataProvider.setValue("TRIPTYPE.DESCRIPTION",
                        rowKey, descriptionTF.getText());               
                triptypeDataProvider.commitChanges();

            } catch (Exception e) {
                error("Cannot append new trip type: " + e);
                triptypeDataProvider.refresh();
                return null;
            }
        } else {
            error("Cannot append trip types");
            return null;
        }
        //clear fields
        idTF.setText(null);
        nameTF.setText(null);  
        descriptionTF.setText(null);        
        return null;

12. Fix imports.

13. Run and test. It will reject existing IDs (1 through 10).

14. To see the database updates, right-click on the TRIPTYPE node in the Servers window and choose View Data.

Comments:

how do i could access remote database

Posted by Ashok on February 26, 2006 at 12:56 PM PST #

See the tutorial at http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/dbserversources.html. I have been told that once you have the right drivers in place, all you need is the right URL that points to the remote machine and the port that the DBMS on that machine is using. In the tutorial, Figure 2 shows an example in the URL Template field. The URL can point to a database on your local machine, or anywhere the Internet can reach.

Posted by Diva 2 on February 27, 2006 at 01:47 AM PST #

How do I fix imports on step 12? What package do I need to import?

Posted by guest on October 28, 2007 at 10:57 AM PDT #

To fix imports, you right-click in the source and choose Fix Imports from the pop-up menu.

Posted by Diva #2 on October 29, 2007 at 01:46 AM PDT #

Why is it that sometimes when you open a project in design view and you try to make the navigation window available, it will open the window without displaying anything even when there are components on the page?

Posted by Ogunfunminiyi Frankfurt on May 06, 2008 at 07:09 PM PDT #

Hi Ogunfunminiyi,

I talked to a QA person and she said she does not know of this happening in the final 6.1 bits. There could be some problem with refresh. She recommends that you file a bug at http://www.netbeans.org/kb/articles/issuezilla.html. She was not sure of the category...possibly the flow editor.

For problems such as this, a good place to go are the NetBeans IDE mailing lists. See http://www.netbeans.org/community/index.html.

Posted by diva #2 on May 08, 2008 at 01:12 AM PDT #

i have following code

public String save_btn_action() {
if(saveDataProvider.canAppendRow()){
try{
RowKey row = saveDataProvider.appendRow();
saveDataProvider.setCursorRow(row);
saveDataProvider.setValue("SAVE.NAME", row, nameTF.getText());
saveDataProvider.setValue("SAVE.DATE1", row, dt.getText());
saveDataProvider.commitChanges();
}
catch(Exception ex){
error("Cannot append new trip type: " + ex);
saveDataProvider.refresh();
return null;
}
}
else{
error("Cannot append trip types");
return null;
}// TODO: Process the action. Return value is a navigation
// case name where null will return to the same page.
return null;
}

when i run the project it give me the errors as

E:\\NetBeans Projects\\save\\src\\java\\save\\Page1.java:156: cannot find symbol
symbol : variable nameTF
location: class save.Page1
saveDataProvider.setValue("SAVE.NAME", row, nameTF.getText());
E:\\NetBeans Projects\\save\\src\\java\\save\\Page1.java:157: cannot find symbol
symbol : variable dt
location: class save.Page1
saveDataProvider.setValue("SAVE.DATE1", row, dt.getText());
2 errors
E:\\NetBeans Projects\\save\\nbproject\\build-impl.xml:392: The following error occurred while executing this line:
E:\\NetBeans Projects\\save\\nbproject\\build-impl.xml:213: Compile failed; see the compiler error output for details.

what to do?

Posted by prashant on July 10, 2008 at 02:23 AM PDT #

Prashant,

It is so hard to tell from your fragment what your problem is, but I am guessing that you do not have a text field component on your page with its id set to nameTF, and the same is true for a text field component with its id set to dt.

Posted by diva#2 on July 10, 2008 at 10:43 AM PDT #

my jsp code is(visual jsf page fragment) as follows

<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : srentry
Created on : Jul 8, 2008, 1:34:10 PM
Author : india
-->
<div style="height: 400px; width: 600px; -rave-layout: grid" xmlns:f="http://java.sun.com/jsf/core" xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
<f:subview id="srentry">
<webuijsf:staticText id="staticText1"
style="color: rgb(204, 0, 204); font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: bold; height: 22px; left: 192px; top: 24px; position: absolute; width: 238px" text="Support And Resistance Entry"/>
<webuijsf:label for="srnoTF" id="label1"
style="font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: normal; height: 24px; left: 68px; top: 72px; position: absolute; width: 60px" text="Sr. No"/>
<webuijsf:textField id="srnoTF" style="height: 20px; left: 192px; top: 74px; position: absolute; width: 50px" valueChangeListenerExpression="#{srentry.srnoTF_processValueChange}"/>
<webuijsf:label for="niftydwLT" id="label2"
style="font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: normal; left: 71px; top: 120px; position: absolute" text="Nifty Name"/>
<webuijsf:textField id="levelTF" style="height: 24px; left: 192px; top: 172px; position: absolute; width: 192px"/>
<webuijsf:label for="levelTF" id="label3"
style="font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: normal; height: 24px; left: 71px; top: 168px; position: absolute; width: 72px" text="Level"/>
<webuijsf:dropDown id="niftydwLT" items="#{srentry.niftydwLTDefaultOptions.options}" style="position: absolute; left: 192px; top: 120px" width="200"/>
<webuijsf:label for="calendarTF" id="label4"
style="color: rgb(0, 0, 0); font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: normal; left: 74px; top: 218px; position: absolute" text="Date"/>
<webuijsf:calendar dateFormatPattern="dd-MM-yyyy" id="calendarTF" style="left: 192px; top: 218px; position: absolute"/>
<webuijsf:button actionExpression="#{srentry.button1_action}" id="button1"
style="color: black; font-family: Verdana,Arial,Helvetica,sans-serif; font-style: normal; font-weight: bold; height: 34px; left: 195px; top: 288px; position: absolute; width: 116px" text="Save"/>
<webuijsf:pageSeparator id="pageSeparator1" style="left: 0px; top: 336px; position: absolute"/>
<webuijsf:staticText id="staticText2"
style="color: rgb(204, 0, 204); font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: bold; height: 22px; left: 144px; top: 384px; position: absolute; width: 334px" text="Support And Resistance Master Entry"/>
<webuijsf:label for="srnomTF" id="label5"
style="color: #000000; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 14px; font-style: normal; font-weight: normal; height: 24px; left: 72px; top: 432px; position: absolute" text="Sr.No"/>
<webuijsf:textField id="srnomTF" style="left: 191px; top: 436px; position: absolute"/>
<webuijsf:label for="niftynameTF" id="label6"
style="color: rgb(0, 0, 0); font-family: 'Verdana','Arial','Helvetica',sans-serif; font-size: 14px; font-style: normal; font-weight: normal; left: 72px; top: 480px; position: absolute" text="Nifty Name"/>
<webuijsf:textField columns="50" id="niftynameTF" style="position: absolute; left: 192px; top: 480px"/>
<webuijsf:button actionExpression="#{srentry.button2_action}" id="button2"
style="font-size: 12px; font-style: normal; font-weight: bold; height: 30px; left: 202px; top: 515px; position: absolute; width: 102px" text="Add"/>
</f:subview>
</div>

i added the code to store data for master entry to database on button_action()

then java file code looks like as

package monemul;

import com.sun.data.provider.RowKey;
import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;

/\*\*
\* <p>Fragment bean that corresponds to a similarly named JSP page
\* fragment. This class contains component definitions (and initialization
\* code) for all components that you have defined on this fragment, as well as
\* lifecycle methods and event handlers where you may add behavior
\* to respond to incoming events.</p>
\*
\* @author india
\*/
public class srentry extends AbstractFragmentBean {
// <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

/\*\*
\* <p>Automatically managed component initialization. <strong>WARNING:</strong>
\* This method is automatically generated, so any user-specified code inserted
\* here is subject to being replaced.</p>
\*/
private void _init() throws Exception {
sandrmasterDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.sandrmasterRowSet}"));
sandrtransactionDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.sandrtransactionRowSet}"));
}
private SingleSelectOptionsList niftydwLTDefaultOptions = new SingleSelectOptionsList();

public SingleSelectOptionsList getNiftydwLTDefaultOptions() {
return niftydwLTDefaultOptions;
}

public void setNiftydwLTDefaultOptions(SingleSelectOptionsList ssol) {
this.niftydwLTDefaultOptions = ssol;
}
private CachedRowSetDataProvider sandrmasterDataProvider = new CachedRowSetDataProvider();

public CachedRowSetDataProvider getSandrmasterDataProvider() {
return sandrmasterDataProvider;
}

public void setSandrmasterDataProvider(CachedRowSetDataProvider crsdp) {
this.sandrmasterDataProvider = crsdp;
}
private CachedRowSetDataProvider sandrtransactionDataProvider = new CachedRowSetDataProvider();

public CachedRowSetDataProvider getSandrtransactionDataProvider() {
return sandrtransactionDataProvider;
}

public void setSandrtransactionDataProvider(CachedRowSetDataProvider crsdp) {
this.sandrtransactionDataProvider = crsdp;
}
// </editor-fold>

public srentry() {
}

/\*\*
\* <p>Callback method that is called whenever a page containing
\* this page fragment is navigated to, either directly via a URL,
\* or indirectly via page navigation. Override this method to acquire
\* resources that will be needed for event handlers and lifecycle methods.</p>
\*
\* <p>The default implementation does nothing.</p>
\*/
@Override
public void init() {
// Perform initializations inherited from our superclass
super.init();
// Perform application initialization that must complete
// \*before\* managed components are initialized
// TODO - add your own initialiation code here

// <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
// Initialize automatically managed components
// \*Note\* - this logic should NOT be modified
try {
_init();
} catch (Exception e) {
log("Page1 Initialization Failure", e);
throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
}

// </editor-fold>
// Perform application initialization that must complete
// \*after\* managed components are initialized
// TODO - add your own initialization code here
}

/\*\*
\* <p>Callback method that is called after rendering is completed for
\* this request, if <code>init()</code> was called. Override this
\* method to release resources acquired in the <code>init()</code>
\* resources that will be needed for event handlers and lifecycle methods.</p>
\*
\* <p>The default implementation does nothing.</p>
\*/
@Override
public void destroy() {
sandrmasterDataProvider.close();
sandrtransactionDataProvider.close();
}

public String button1_action() {

// TODO: Process the action. Return value is a navigation
// case name where null will return to the same page.
return null;
}

public void srnoTF_processValueChange(ValueChangeEvent vce) {
}

public String button2_action() {
if(sandrmasterDataProvider.canAppendRow())
{
try {
RowKey rw = sandrmasterDataProvider.appendRow();
sandrmasterDataProvider.setCursorRow(rw);
sandrmasterDataProvider.setValue("SANDRMASTER.SRNO", rw, srnoTF.getText());
sandrmasterDataProvider.setValue("SANDRMASTER.NIFTYNAME", rw, niftynameTF.getText());
sandrmasterDataProvider.commitChanges();
} catch (Exception e) {
error("Cannot append new share type type: " + e);
sandrmasterDataProvider.refresh();
return null;

}

}
else{
error("Cannot append share types");
return null;

}
srnoTF.setText(null);
niftynameTF.setText(null);
// TODO: Process the action. Return value is a navigation;
// case name where null will return to the same page.
return null;
}

}

now when i compile my application it gives me the following errors

Compiling 1 source file to D:\\netbeans projects\\monemul\\build\\web\\WEB-INF\\classes
D:\\netbeans projects\\monemul\\src\\java\\monemul\\srentry.java:132: cannot find symbol
symbol : variable srnoTF
location: class monemul.srentry
sandrmasterDataProvider.setValue("SANDRMASTER.SRNO", rw, srnoTF.getText());
D:\\netbeans projects\\monemul\\src\\java\\monemul\\srentry.java:133: cannot find symbol
symbol : variable niftynameTF
location: class monemul.srentry
sandrmasterDataProvider.setValue("SANDRMASTER.NIFTYNAME", rw, niftynameTF.getText());
D:\\netbeans projects\\monemul\\src\\java\\monemul\\srentry.java:148: cannot find symbol
symbol : variable srnoTF
location: class monemul.srentry
srnoTF.setText(null);
D:\\netbeans projects\\monemul\\src\\java\\monemul\\srentry.java:149: cannot find symbol
symbol : variable niftynameTF
location: class monemul.srentry
niftynameTF.setText(null);
4 errors
D:\\netbeans projects\\monemul\\nbproject\\build-impl.xml:392: The following error occurred while executing this line:
D:\\netbeans projects\\monemul\\nbproject\\build-impl.xml:213: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

what should i do to fix it

Posted by prashant on July 18, 2008 at 05:31 PM PDT #

Prashant,

Are you able to do http://www.netbeans.org/kb/60/web/helloweb.html without problems?

Posted by guest on July 21, 2008 at 02:35 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

divas

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today