You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Daniel Łaś <da...@e-direct.pl> on 2005/07/22 15:03:44 UTC

Design question

Hi

I'm interested in yours opinions about following design approach. I'd 
like to create some kind of "view" beans for viewable model beans. Those 
view beans can generate HTML for attached model bean. This generated 
HTML is based on resources, so I am able to generate different HTML code 
depending on session locale.

Lets say I create one model bean User
I created UserView with reference pointing to User.
I attached reference to MessageResources in UserView bean during action 
execution with getResources(request)
In my JSP I added: <bean:write name="UserView" property="content" 
filter="false"/> which calls getContent() method
UserView method body is:
public String getContent()
{
    return messages.getMessage("user.login")+user.getLogin();
}
where:
messages - private UserView member of class MessageResources
user - private UserView member of class User
user.login - resources key

Everything worked well, but when I changed session locale my UserView 
bean generates output for previus locale. I think that I have to update 
messages member of UserView bean or use reference to other object, maybe 
servlet which will be constant during session.

I'm new to struts, so I'd like to know if there are some other issues 
connected to mentioned design and struts?

Regards

-- 
Daniel Łaś <da...@e-direct.pl>

e-direct Polska sp. z o.o.
ul. 1-go Maja 9
45-068 Opole
tel. +48 77 44 17 868


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Design question

Posted by Ed Griebel <ed...@gmail.com>.
Hi Daniel-

The idea of a "smart bean" which knows how to render itself is a
pretty cool idea, and it's a clean idea conceptually. But, I think the
Struts framework and struts jsp tags removes a lot of the hassle
because of what it gives you out of the box.

I've been faced with the same situation, and we used the same jsp for
view and edit mode, with the html markup/form/etc., based on if you
are in edit or view mode, and just tweak the output. For example,
<td>A Field:</td>
<td>
<c:if test="${inAddEditMode}">
  <html:text ... property="aField"/>
</c:if>
<c:if test="${not inAddEditMode}">
  <c:out value="${formBean.aField}"/>
</c:if>
</td>

I think you're going to find that adding a ton of complexity to
generate HTML in a form bean (or helper or wherever) and recreating
everything that comes 'for free' with Struts isn't worth saving one
step in your plan below (changing custom jsp view logic), especially
following my example above.

Also a suggestion, you can condense steps 3 and 4 by using a
dynaFormBean and then in the action map the struts form bean to your
user bean using Jakarta BeanUtils methods to copy back and forth, so
you will never have to explicitly declare setters and getters for most
of your fields. You'll only need custom logic (in your action or
better in a stateless helper class) to set a declared form bean field
when automatic translation can't occur, such as datatypes are
incompatible or you have a form field set based on some fields'
values.

Another idea, assume your User bean is populated from the database
based on the data returned (again with bean utils) and the new
field(s) are added in a DAO/persistence layer which has to be done
anyway. In this case the only changes required in step 1 are adding a
private field and get/set methods, which is pretty trivial.

It looks like you're adding a lot of complexity for not much gain.
Maybe I'm missing something though, and the concept does sound
interesting too, it's certainly thought-provoking!

-ed

On 7/22/05, Daniel Łaś <da...@e-direct.pl> wrote:
> Well, I thing about something else.
> 
> If UserView bean can generate HTML form fields dependig on User
> attributes, one for adding, another one for editing I could only change
> my User bean (model) and my view layer will work without change. I think
> that it is possible to write action that will work with this (controller
> layer) without change too.
> 
> Lets say, I add new attribute to User, eg. lastName.
> I have to:
> 1. modify User bean code (model)
> 2. then modify appropriate jsp pages, first with edit form, second with
> add form
> 3. then modify struts-config.xml (form properties)
> 4. then modify appropriate form bean
> 5. then modify two actions (one for edit, one for add operation)
> 
> It would be nice, to change only my model and have view layer adapting
> to model.
> 
> Mabe I'm re-inventing the wheel. If so, please tell me. I'm new to
> struts and Java technologies, just trying to make right decisions before
> my projects start.
> 
> Regards
>

Re: Design question

Posted by BH...@powersystems.rockwell.com.
There are several possibilities to do what you are asking.  You will still
have to modify HTML code, it is just where.  I would not typically create
multiple jsp pages for Add, Update, Display.  I would create a single page
using a DynaValidatorForm.  this way you can use the same form and based on
the action you call, the appropriate validation will be performed.
Additionally, I would look into the CRUD Action Examples to prevent me from
developing multiple actions.  Based on the funtion you are calling the
appropriate Data Access function can be called.  If this is set up properly
you can get away with changing only two components for this to work the
Business Object Bean and the HTML page.

Granted, if you have this information on multiple pages like a login page,
then as a display in the corner of each page within the application, you
would need to update that on each JSP page.  But it is likely you would
want to do that anyway to insure the look and feel of each page.



                                                                           
             Daniel Łaś                                                    
             <daniel.las@e-dir                                             
             ect.pl>                                                    To 
                                       Struts Users Mailing List           
             07/22/2005 10:07          <us...@struts.apache.org>            
             AM                                                         cc 
                                                                           
                                                                   Subject 
             Please respond to         Re: Design question                 
               "Struts Users                                               
               Mailing List"                                               
             <user@struts.apac                                             
                  he.org>                                                  
                                                                           
                                                                           




Well, I thing about something else.

If UserView bean can generate HTML form fields dependig on User
attributes, one for adding, another one for editing I could only change
my User bean (model) and my view layer will work without change. I think
that it is possible to write action that will work with this (controller
layer) without change too.

Lets say, I add new attribute to User, eg. lastName.
I have to:
1. modify User bean code (model)
2. then modify appropriate jsp pages, first with edit form, second with
add form
3. then modify struts-config.xml (form properties)
4. then modify appropriate form bean
5. then modify two actions (one for edit, one for add operation)

It would be nice, to change only my model and have view layer adapting
to model.

Mabe I'm re-inventing the wheel. If so, please tell me. I'm new to
struts and Java technologies, just trying to make right decisions before
my projects start.

Regards

BHansard@powersystems.rockwell.com napisał(a):
> I think you are re-inventing something here.
>
> To do what you are wanting is already in struts to some degree.
>
> you have your User Bean, You define an formbean which either contains
> UserBean or contains fields that match UserBean
>
> on your jsp page you create something like
>
> <html:html local="true">
> <html:form action="/loginAction.do" method="post">
> <bean:message key="resource.key"/><html:text propery="userId"/>
> </html:form>
> </html:html>
>
> This will print the UserID's label from the Resource file and give you
> internationalization.
>
> This might be easier than embedding HTML into a tag or bean.
>
> Inactive hide details for Daniel Łaś <da...@e-direct.pl>Daniel Łaś
> <da...@e-direct.pl>
>
>
>                         *Daniel Łaś <da...@e-direct.pl>*
>
>                         07/22/2005 09:03 AM
>                         Please respond to
>                         "Struts Users Mailing List"
<us...@struts.apache.org>
>
>
>
> To
>
> Struts Users Mailing List <us...@struts.apache.org>
>
> cc
>
>
> Subject
>
> Design question
>
>
>
>
> Hi
>
> I'm interested in yours opinions about following design approach. I'd
> like to create some kind of "view" beans for viewable model beans. Those
> view beans can generate HTML for attached model bean. This generated
> HTML is based on resources, so I am able to generate different HTML code
> depending on session locale.
>
> Lets say I create one model bean User
> I created UserView with reference pointing to User.
> I attached reference to MessageResources in UserView bean during action
> execution with getResources(request)
> In my JSP I added: <bean:write name="UserView" property="content"
> filter="false"/> which calls getContent() method
> UserView method body is:
> public String getContent()
> {
>    return messages.getMessage("user.login")+user.getLogin();
> }
> where:
> messages - private UserView member of class MessageResources
> user - private UserView member of class User
> user.login - resources key
>
> Everything worked well, but when I changed session locale my UserView
> bean generates output for previus locale. I think that I have to update
> messages member of UserView bean or use reference to other object, maybe
> servlet which will be constant during session.
>
> I'm new to struts, so I'd like to know if there are some other issues
> connected to mentioned design and struts?
>
> Regards
>
> --
> Daniel Łaś <da...@e-direct.pl>
>
> e-direct Polska sp. z o.o.
> ul. 1-go Maja 9
> 45-068 Opole
> tel. +48 77 44 17 868
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


--
Daniel Łaś <da...@e-direct.pl>

e-direct Polska sp. z o.o.
ul. 1-go Maja 9
45-068 Opole
tel. +48 77 44 17 868

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Design question

Posted by Daniel Łaś <da...@e-direct.pl>.
Well, I thing about something else.

If UserView bean can generate HTML form fields dependig on User 
attributes, one for adding, another one for editing I could only change 
my User bean (model) and my view layer will work without change. I think 
that it is possible to write action that will work with this (controller 
layer) without change too.

Lets say, I add new attribute to User, eg. lastName.
I have to:
1. modify User bean code (model)
2. then modify appropriate jsp pages, first with edit form, second with 
add form
3. then modify struts-config.xml (form properties)
4. then modify appropriate form bean
5. then modify two actions (one for edit, one for add operation)

It would be nice, to change only my model and have view layer adapting 
to model.

Mabe I'm re-inventing the wheel. If so, please tell me. I'm new to 
struts and Java technologies, just trying to make right decisions before 
my projects start.

Regards

BHansard@powersystems.rockwell.com napisał(a):
> I think you are re-inventing something here.
> 
> To do what you are wanting is already in struts to some degree.
> 
> you have your User Bean, You define an formbean which either contains 
> UserBean or contains fields that match UserBean
> 
> on your jsp page you create something like
> 
> <html:html local="true">
> <html:form action="/loginAction.do" method="post">
> <bean:message key="resource.key"/><html:text propery="userId"/>
> </html:form>
> </html:html>
> 
> This will print the UserID's label from the Resource file and give you 
> internationalization.
> 
> This might be easier than embedding HTML into a tag or bean.
> 
> Inactive hide details for Daniel Łaś <da...@e-direct.pl>Daniel Łaś 
> <da...@e-direct.pl>
> 
> 
>                         *Daniel Łaś <da...@e-direct.pl>*
> 
>                         07/22/2005 09:03 AM
>                         Please respond to
>                         "Struts Users Mailing List" <us...@struts.apache.org>
> 
> 	
> 
> To
> 	
> Struts Users Mailing List <us...@struts.apache.org>
> 
> cc
> 	
> 
> Subject
> 	
> Design question
> 
> 	
> 
> 
> Hi
> 
> I'm interested in yours opinions about following design approach. I'd
> like to create some kind of "view" beans for viewable model beans. Those
> view beans can generate HTML for attached model bean. This generated
> HTML is based on resources, so I am able to generate different HTML code
> depending on session locale.
> 
> Lets say I create one model bean User
> I created UserView with reference pointing to User.
> I attached reference to MessageResources in UserView bean during action
> execution with getResources(request)
> In my JSP I added: <bean:write name="UserView" property="content"
> filter="false"/> which calls getContent() method
> UserView method body is:
> public String getContent()
> {
>    return messages.getMessage("user.login")+user.getLogin();
> }
> where:
> messages - private UserView member of class MessageResources
> user - private UserView member of class User
> user.login - resources key
> 
> Everything worked well, but when I changed session locale my UserView
> bean generates output for previus locale. I think that I have to update
> messages member of UserView bean or use reference to other object, maybe
> servlet which will be constant during session.
> 
> I'm new to struts, so I'd like to know if there are some other issues
> connected to mentioned design and struts?
> 
> Regards
> 
> -- 
> Daniel Łaś <da...@e-direct.pl>
> 
> e-direct Polska sp. z o.o.
> ul. 1-go Maja 9
> 45-068 Opole
> tel. +48 77 44 17 868
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


-- 
Daniel Łaś <da...@e-direct.pl>

e-direct Polska sp. z o.o.
ul. 1-go Maja 9
45-068 Opole
tel. +48 77 44 17 868

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Design question

Posted by BH...@powersystems.rockwell.com.
I think you are re-inventing something here.

To do what you are wanting is already in struts to some degree.

you have your User Bean,  You define an formbean which either contains
UserBean or contains fields that match UserBean

on your jsp page you create something like

<html:html local="true">
    <html:form action="/loginAction.do" method="post">
    <bean:message key="resource.key"/><html:text propery="userId"/>
    </html:form>
</html:html>

This will print the UserID's label from the Resource file and give you
internationalization.

This might be easier than embedding HTML into a tag or bean.



                                                                           
             Daniel Łaś                                                    
             <daniel.las@e-dir                                             
             ect.pl>                                                    To 
                                       Struts Users Mailing List           
             07/22/2005 09:03          <us...@struts.apache.org>            
             AM                                                         cc 
                                                                           
                                                                   Subject 
             Please respond to         Design question                     
               "Struts Users                                               
               Mailing List"                                               
             <user@struts.apac                                             
                  he.org>                                                  
                                                                           
                                                                           




Hi

I'm interested in yours opinions about following design approach. I'd
like to create some kind of "view" beans for viewable model beans. Those
view beans can generate HTML for attached model bean. This generated
HTML is based on resources, so I am able to generate different HTML code
depending on session locale.

Lets say I create one model bean User
I created UserView with reference pointing to User.
I attached reference to MessageResources in UserView bean during action
execution with getResources(request)
In my JSP I added: <bean:write name="UserView" property="content"
filter="false"/> which calls getContent() method
UserView method body is:
public String getContent()
{
    return messages.getMessage("user.login")+user.getLogin();
}
where:
messages - private UserView member of class MessageResources
user - private UserView member of class User
user.login - resources key

Everything worked well, but when I changed session locale my UserView
bean generates output for previus locale. I think that I have to update
messages member of UserView bean or use reference to other object, maybe
servlet which will be constant during session.

I'm new to struts, so I'd like to know if there are some other issues
connected to mentioned design and struts?

Regards

--
Daniel Łaś <da...@e-direct.pl>

e-direct Polska sp. z o.o.
ul. 1-go Maja 9
45-068 Opole
tel. +48 77 44 17 868


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org