Colin Sampaleanu wrote:

> Keep in mind that it is perfectly ok (depending on how you want to design
> your app) for the user to come in through either the JSP page or through the
> action url.
>
> On that basis, you need to be able to specify and create the form bean in
> each of those two places, and those two places don't know anything about
> each other. The JSP page doesn't know about action mapppings, and the
> mapping doesn't know anything about all the particular JSP pages that may
> want to use that form. There is not even any stipulation that a form used in
> a JSP page has to be used in a mapping, e.g. the submit from the form could
> theoretically go to any URL. The inverse also applies.
>

Your reasoning on why it needs to be created in either place is correct.

What's still screwed up in the current design is that you have to specify the
bean class and scope in two places (the page itself and the configuration
file).  Ideally the form tag should be smart enough to look up the right action
mapping (based on where it's going to submit to) and therefore infer the right
bean class and scope if not specified.  I just haven't gotten that far yet.

>
> As to why the bean is automatically created if it doesn't exist, I do agree
> that there are cases in which you might want to have some sort of error if
> the bean doesn't already exist, but you can easily do that by having a
> special member variable in the bean that indicates that it has just been
> created. On a manual create you would reset that yourself, but on an auto
> create it would remain set, and the validate method would detect it (or you
> could check manually).
>

For testing whether a bean was newly created by the JSP page, a particular
feature of the <jsp:useBean> tag is useful -- the body of this tag is only
evaluated when the bean is created.  Consider the following:

    <jsp:useBean id="beanname" scope="session" class="com.mycompany.MyBean">
        <jsp:setProperty name="pageCreated" value="true"/>
    </jsp:useBean>

In this scenario, setPageCreated(true) will be called if the bean was created by
this page.  If the "pageCreated" property defaults to false, you now have an
easy test.

For general purpose detection of existing beans, the new logic tags make this
easy:

    <logic:notPresent name="beanname" scope="session">
        You are a bad boy ... the "beanname" bean is missing!
    </logic:notPresent>

Craig McClanahan




Reply via email to