Discussions

Web tier: servlets, JSP, Web frameworks: JSP and JavaBeans

  1. JSP and JavaBeans (1 messages)

    Hi everyone.

    I have worked with ATG Dynamo, where is was possible to "associate" a bean property directly with a HTML form input field. In this scenario, when the page is being dynamically generated on the ATG server, if the corresponding bean property has a value - then the value will appear in the HTML page. Furthermore, when the page is submitted from the client browser, any inputted values will be set in the bean.

    [Note - This is in Dynamo4.5 - it uses jhtml and at the time was not fully compliant with J2EE - Dynamo5 is fully compliant]

    So to my questions - JSP has something very similar (but not quiet I think) - you can get properties from a bean using <jsp:getPropery ....

    Equally well, you can set bean values with <jsp:setproperty.

    Any examples I have seen seem to use these sperately and not totgether -

    Suppose I want to create a jsp page [say page1.jsp] which allows the user enter some data. When the data is submitted, validation occurs, and if there are errors, I would like page1.jsp to be redisplayed informing the user of the errors as well as containing the data that the user just entered. This was very simple to do in a SINGLE page in Dynamo, simply by just using the association that I spoke of.

    However any examples I have seen using JSP seem to need at least 2 seperate pages to achieve this. Is this correct? Can someone provide a simple sample?

    Thanks

    Paddy

    Threaded Messages (1)

  2. JSP and JavaBeans[ Go to top ]

    You can map all the fields on a page to a bean using JSP tags as follows.

    <jsp:useBean id="paddy" scope="request" class="com.theserverside.PaddysBean" />
    <jsp:setProperty name="paddy" property="*" />

    This will cause the JSP to "set" the references in the bean to be the reference in the field filled in (Taken from the request parameters in the HTTP request.

    In your form, you can use <jsp:getProperty name="paddy" property="paddysProperty" /> as the value for the fields. If there isn't one (or there is a default) then it will get put in the form.

    That way, when you first call the page for that session, the bean is created and stored as session scope. The form will build with default properties. When you submit it, you can validate it, and if there is a problem, simply redirect the request back to the input page, which will redisplay the form (Note, you can easily enough have an errorMessage property in the form / bean which you pull out and display on the page.

    This makes redisplaying input forms with the error messages highlighted pretty simple to do.

    The problem comes when building drop down lists and such like, where you can't simply say "get the value" due to the way the HTML for these fields has to be formed. That's a bit more painful to do, but you can easily write a utility class to make it easier.

    Is this along the lines you meant?

    Chz

    Tony