Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-19-2004, 06:27 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
The Java Bean ?

is a bean just a class in your project?

i'm having trouble getting a jsp app to work.

i made a class in the WEB-INF/classes dir.

it's a simple user class. username , email

in my jsp, i am calling it like this:
Code:
<jsp:useBean id="mybean" class="User" scope="session" />
<jsp:setProperty name="mybean" property="username" value="sde" />
<jsp:setProperty name="mybean" property="email" value="me@somewhere.com" />
i get an error that says: 'User mybean = null'

am i going about this the right way?
__________________
Mike
sde is offline   Reply With Quote
Old 04-22-2004, 02:38 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
Is the bean in a package? I've always packaged my beans.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-23-2004, 04:15 PM   #3 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
Does your class have public getter and setter methods? You'll need those to interact correctly with the get and setProperty tags.
technobard is offline   Reply With Quote
Old 04-23-2004, 04:33 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i can't just declare the properties in the class public?
__________________
Mike
sde is offline   Reply With Quote
Old 04-23-2004, 05:13 PM   #5 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
No. To act properly as a javabean, you need public getter and setter methods.
technobard is offline   Reply With Quote
Old 04-23-2004, 05:32 PM   #6 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
A little more info:
The bean specification examines the class and expects certain naming conventions to figure out which properties are available, etc.

Your class should look something like:
Code:
public class User {
    String username="";
    Strimg email="";

    public User() {
    }

    public String getUsername() {
       return username;
    }

    public void setUsername(String username) {
       this.username = username;
    }

    public String getEmail() {
       return email;
    }

    public void setEmail(String email) {
       this.email = email;
    }
}
If I remember correctly, the getter and setter methods must be named by using the prefixes "get" and "set" followed by the variable name with the first letter capitalized.
technobard is offline   Reply With Quote
Old 04-23-2004, 06:37 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
ok .. so the properties aren't really 'username' from the bean's perspective? it is 'getUsername' .. that sorta makes sense.

although it would be nice if i could just declare username public and access it. hehe, i'm sure there's a good reason.

thanks
__________________
Mike
sde is offline   Reply With Quote
Old 04-24-2004, 03:08 AM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
It's generally considered good form in Java to access an object through methods rather than public fields. You could access a public field in a bean by doing <%=object.field%>, but depending on whom you talk to, it's better to use the tags in order to keep seperation between the parts of MVC.

Why they chose this particular route for the get/setProperty tags was probably due to the fact that your get/set methods might be more complex than just referencing a String variable (you might access a database), at which point referencing a field won't do you a lot of good.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Developer Position in Kansas City justplaindoug Java 0 10-29-2004 11:52 AM
JAVA Hobbyists Wanted emteam Java 0 08-29-2004 11:27 AM
IBM urges Sun to make Java open source redhead Code Newbie News 0 03-01-2004 10:39 PM


All times are GMT -8. The time now is 05:24 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting