|
|
|
Re: JPA and JSP [message #390028 is a reply to message #390023] |
Thu, 09 July 2009 05:55 |
Enrico Messages: 82 Registered: July 2009 |
Member |
|
|
tbee ha scritto:
> I have no direct experience with JSP and JPA, but I'll just yell some
> generics. Maybe it will trigger an idea.
>
> First, a hidden field practically can only contain a prirary key value;
> your B-id. And when you set the property, you need to execute the find
> to convert the id to an entity. As you show in the second post, this
> could be included in the entity itself, but that is not what you want.
> So then the find must be done in the JSP page.
>
> However no JSP binding logic will do that find for you, so you need to
> do this manually and that means you cannot use any automatic binding.
> From that perspective a special setter which does a find may not be a
> bad idea; the find code has to go somewhere and putting it in the entity
> at least makes it reusable.
>
> Tom
>
>
>
Tom,
thank you for yopur reply.
So, you mean is better to find in the jsp and so set the persistent
property from jsp?I would like to have less possible logic (java code)
in JSP pages. I would like just to have data presentation and data input
on JSP pages, while all the logic is in java beans and core.
I use java generics to make persissten operation (load, save, find
etc..), so I have this method (no in the entity but in a baseDAO
class....then any entity has an own dao class that extend the base dao
and, basing on generics, is specific for the related entity).
Just a point...I know there is a jpa-tag library for JSP
( http://weblogs.java.net/blog/ss141213/archive/2006/01/custom _tags_to.html
and https://jpa-taglib.dev.java.net/). Do you think it could be useful
and solve my problem?
Best Regards,
Enrico
|
|
|
Re: JPA and JSP [message #390029 is a reply to message #390028] |
Thu, 09 July 2009 06:12 |
Tom Eugelink Messages: 817 Registered: July 2009 |
Senior Member |
|
|
> So, you mean is better to find in the jsp and so set the persistent
> property from jsp?I would like to have less possible logic (java code)
> in JSP pages. I would like just to have data presentation and data input
> on JSP pages, while all the logic is in java beans and core.
> I use java generics to make persissten operation (load, save, find
> etc..), so I have this method (no in the entity but in a baseDAO
> class....then any entity has an own dao class that extend the base dao
> and, basing on generics, is specific for the related entity).
>
> Just a point...I know there is a jpa-tag library for JSP
> ( http://weblogs.java.net/blog/ss141213/archive/2006/01/custom _tags_to.html
> and https://jpa-taglib.dev.java.net/). Do you think it could be useful
> and solve my problem?
What I'm trying to say is that somewhere between the id that is stored in the form and the setter of your entity, you have to do a "find". You can put that find in the JSP or in directly the entity; it is the same logic to codde, but when you put it in the entity you can reuse it.
What will not work is the automatic binding of JSP; if JSP automatically writes a submitted value to a setter in a bean, it will never do the find. So you cannot use JSP binding.
You can use the JPA tag libraries you mention to implement the find, but then you still have to do it yourself, see the examples of the tag libraries:
<% String name = String.class.cast(request.getParameter("name")); %>
<jpa:injectDefaultPC id="em1"/>
<jpa:find em="em1" clazz="example.UserCredential" pk="<%= name %>" id="credential"/>
Would you rather have that in the JSP or this in your entity:
public void setB(int id)
{
setB( findbyid(id) );
}
Personally I go for the last.
Tom
|
|
|
Powered by
FUDForum. Page generated in 0.03250 seconds