How to execute method of a bean?

Hello,

I'm really new at JSP and just build a jsp page with a javabean which communicate through get's and set's. I've a form where i can input values which the bean uses, but i also want to have a button which, when pressed, activates a set-default method of my bean. How can i connect this fmethod to my button. As far as i can see, only get are applied when the submit-button is pressed.

thanx

[426 byte] By [hendriksh] at [2007-9-26 6:38:24]
# 1

Hi

If I have understood your question correctly, you are trying to set the fields of a Java Bean when you hit the submit button of a form.

To clarify things - lets say there are two JSP pages Page1 and Page2.

Page1 has 2 text boxes whose name values are "firstName" & "lastName". This page also has a submit button. which would post the request to Page2. Now lets say the java bean that you are using is BeanX and it has two fields firstName & lastName. It would also have accessor and modifier methods, s(g)etFirstName and s(g)etLastName(). Using the jsp: usebean directive with the setproperty="*" in Page2 would automatically update the bean values by calling the corresponding set methods.

The rule here is that the textBox name's first letter is changed to upper case and a corresponding set method is looked for in the bean and called automatically. Here the text box name was firstName so the corresponding set called would be setFirstName().

Keep me posted on your progress.

Good Luck!

Eshwar Rao

Developer Technical Support

Sun microsystems Inc.

http://www.sun.com/developers/support

Eshwar_indts at 2007-7-1 15:54:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hello,

I think i explained it wrong. Let me try again. I have made a form where you can enter different fields. When you press the submit button, the values are read and processed by the bean. I now want to add another button which resets the values of the field to default. I made a method in the bean setValuesToDefault. So when i press the button on the form, the method setValuesToDefault is called. How do I make this button and how do I call the method? I hope this explains my problem right

thanks

Hugo Hendriks

hendriksh at 2007-7-1 15:54:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi Hugo, I'm new to JSP also but I think I can help you so here's my take on it:

For one, if you're going trying to communicate with the server prior to using the submit button, you'll have to use the onClick attribute for the button to call a JavaScript function which would call a JSP, (or servlet), in your same application to call your bean - which would need to have application scope.

I think that's the long way around the barn though.

What I would do, if I understand you correctly is:

Use some JavaScript function to have the button's onClick set a hidden form value, say to "true". Then upon submit your JSP will have access to that value and can use it in a conditonal to call whatever bean method you want...in this case the setDefault method. i.e.

String defaultButton=request.getParameter("defaultButton");

if(defaultButton.equals("true")){

myBean.setDefault();

}

Use this code at whatever point is appropriate in your code.

I hope that helps, let us know,

Brad

brads at 2007-7-1 15:54:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi Hugo

Yes, This time I think your question is clear. This is what you can do. There could be other more efficient ways but this way works. In your JSP page where you have the text input have 2 separate form fields . The first one would be for the submit button and you would submit it to the same jsp. The second form field also contains a submit button but will submit to a different jsp. - Reset.jsp

In reset.jsp call the setDefault method with some dummy value. as an argument. The method body can contain procedure to reset the attribute values to their default values. From the reset.jsp forward the request back to your initial page. You would have your default values.

I'll paste the code for your reference:

Test.jsp

<html>

<head><title>JSP Page</title></head>

<body>

<form method="post" action="test.jsp">

<jsp:useBean id="nameBean" scope="session" class="test.bean.NameBean" />

<jsp:setProperty name="nameBean" property="*" />

First Name: <input type="text" name="firstName" value="<jsp:getProperty name="nameBean" property="firstName" />"></input>

Last Name: <input type="text" name="lastName" value="<jsp:getProperty name="nameBean" property="lastName" />"></input>

<input type="submit" value="SUBMIT"></input>

</form>

<form method="post" action="reset.jsp">

<input type="submit" value="RESET">

</form>

</body>

</html>

Reset.jsp

--

<html>

<head><title>JSP Page</title></head>

<body>

<jsp:useBean id="nameBean" scope="session" class="test.bean.NameBean" />

<jsp:setProperty name="nameBean" property="default" value="xxx"/>

<jsp:forward page="test.jsp"/>

</body>

</html>

NameBean.java

-

public class NameBean implements Serializable{

private String _firstName = "DEFAULT_FIRST_NAME";

private String _lastName = "DEFAULT_LAST_NAME";

private String _default;

/** Creates new MeetingMinutesBean */

public NameBean() {

}

public void setFirstName(String firstName){

_firstName = firstName;

}

public String getFirstName(){

return _firstName;

}

public void setLastName(String lastName){

_lastName = lastName;

}

public String getLastName(){

return _lastName;

}

public void setDefault(String value){

_firstName = "DEFAULT_FIRST_NAME";

_lastName = "DEFAULT_LAST_NAME";

}

}

Let me know if this helped.

Good Luck!

Eshwar Rao

Developer Technical Support

Sun microsystems Inc.

http://www.sun.com/developers/support

Eshwar_indts at 2007-7-1 15:54:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hello again,

I tried making a default.jsp which calls my method setValuestodefault but is still get this

error:javax.servlet.ServletException: Cannot find any information on property 'valuestodefault' in a bean of type 'apacheconfigurator.BasicConfigurationBean'

Can't figure out what i'm doing wrong. Here's my code:

--default.jsp--

<html>

<head><title>Set default values</title></head>

<body>

<jsp:useBean id="BasicConfigurationBean" scope="session" class="apacheconfigurator.BasicConfigurationBean" />

<jsp:setProperty name="BasicConfigurationBean" property="valuestodefault"/>

<jsp:forward page="BasicConfiguration.jsp"/>

</body>

</html>

-BasicConfigurationBean.java--

package apacheconfigurator;

public class BasicConfigurationBean

{

private String servertype = "Network";

private String serverroot = "/etc/httpd/dolly";

private String servername = "dolly de webserver";

private String serveradmin = "dolly@madsheep.com";

private String listen = "*:80";

private int port = 80;

/**Default values-*/

public void setValuestodefault() {

servertype = "standalone";

serverroot = "/etc/httpd/";

servername = "apache server";

serveradmin = "webmaster@apache.com";

listen = "*:80";

port = 80;

}

and some other get and set function...

}

BasicConfiguration.jsp-

<%@ page errorPage="BasicConfigurationErrorPage.jsp" %>

<HTML>

<HEAD><TITLE>Apache Configuration Tool.</TITLE></HEAD>

<BODY bgcolor="#776699">

<jsp:useBean id="BasicConfigurationBean" scope="session" class="apacheconfigurator.BasicConfigurationBean" />

<jsp:setProperty name="BasicConfigurationBean" property="*" />

<font color="#ffffff">

<centre>Basic configuration:</centre>

<FORM method="post" action="BasicConfiguration.jsp">

<table>

<tr>

<td>ServerType:</td>

<td><input NAME="servertype" value="<jsp:getProperty name="BasicConfigurationBean" property="servertype"/>"></td>

</tr>

<tr>

<td>ServerRoot:</td>

<td><input NAME="serverroot" value="<jsp:getProperty name="BasicConfigurationBean" property="serverroot"/>"></td>

</tr>

<tr>

<td>ServerName:</td>

<td><input NAME="servername" value="<jsp:getProperty name="BasicConfigurationBean" property="servername"/>"></td>

</tr>

<tr>

<td>ServerAdmin:</td>

<td><input NAME="serveradmin" value="<jsp:getProperty name="BasicConfigurationBean" property="serveradmin"/>"></td>

</tr>

<tr>

<td>Listen:</td>

<td><input NAME="listen" value="<jsp:getProperty name="BasicConfigurationBean" property="listen"/>"></td>

</tr>

<tr>

<td>Port:</td>

<td><input NAME="port" value="<jsp:getProperty name="BasicConfigurationBean" property="port"/>"></td>

</tr>

</table>

<centre>

<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">

<form method="post" action="Default.jsp">

<input TYPE="SUBMIT" NAME="Submit" VALUE="Default">

</form></centre>

</FORM>

</font>

</BODY>

</HTML>

I apologize for the large quantity of code....

thanks

Hugo Hendriks

hendriksh at 2007-7-1 15:54:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi,

Already found the prob. You can't embed a form into a form. When i closed the first form and created the second form which pointed to default.jsp, it worked like a charm. The only thing i regret is that after you close a form, it automaticly creates a newline. Now you can't have the two buttons next to eachother, but only above eachother.

Anyway, thanks for the help

greetings

Hugo Hendriks

hendriksh at 2007-7-1 15:54:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...