Solved

Need a simple tag

Posted on 2006-11-15
28
298 Views
Last Modified: 2010-04-01
Hi,

I am new to tags,
and I'm pretty surprised to see that there isn't a standard tag to
access a java class in the application context,
without initializing it.

Can't use jsp:useBean because I access via an interface and useBean try to instantiate it, so there is a compiling error.
0
Comment
Question by:matthew016
  • 14
  • 10
  • 2
  • +1
28 Comments
 
LVL 10

Expert Comment

by:jaggernat
ID: 17947348
well, why not? you can use a <jsp:useBean> in application scope

useBean tag gets values from the java class  (primarily a Bean which holds the values) and populates the jsp fields

http://java.sun.com/products/jsp/tags/11/syntaxref11.fm14.html
0
 
LVL 9

Author Comment

by:matthew016
ID: 17947439
No, it doesn't work, because as I said, I try to get a java class from the application,
and I want to cast it to the interface (I want to respect the IoC concept, I want interfaces to interact with other layer).

And jsp:useBean make me error because he tries to instantiate the interface if he gets null from the attribute.
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17948567
may be if you can post some code, i will be able to help u better
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Author Comment

by:matthew016
ID: 17954079
<%
      application.IGestionUtilisateurs gestionUtilisateurs =
            (controleur.IGestionUtilisateurs) application.getAttribute("gestionUtilisateurs");
%>
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 17955416
As long as you have imported the classes in your page declaration in the top of the top page, you should be able to get interfaces and classes from the servletContext/application object.

You really should leave JSPs for presentation only. If you are respecting IOC then you should also respect MVC and only use JSP for view. In this case, the objects would be set up in a servlet or action then accesssed by the JSP in the request after using a RequestDispatcher (as in the case of Struts and other frameworks)
0
 
LVL 9

Author Comment

by:matthew016
ID: 17956042
Thank u bloodredsun,
I understand this perfectly, and u must refer to the Setup/Submit pattern, no ?
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 17956632
If you are referring to the Setup/Submit pattern mentioned here http://wiki.apache.org/struts/DataEntryForm then yes exactly.

0
 
LVL 9

Author Comment

by:matthew016
ID: 17956692
Exactly,
well I hope u can help me because I don't know how to implement it in my case ... that's why I do everything in my jsp,
I explain you :

(in the following question I make difference between a jsp page and a webpage (that can be composed of several jsp pages, : header.jsp, menu.jsp, content.jsp, ...)

I understand how to apply it for one JSP page.
But I made a layout, so I have a set of jsp pages for one webpage :
header.jsp , footer.jsp, menu_left.jsp, menu_top.jsp, content_index.jsp, ...

So for example to display the index *webpage*,
I make one SetupIndex action that need to setup the header.jsp, the menu_left.jsp, ...

public class SetupIndex extends Action {
    public ActionForward execute(
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
          // setup header.jsp (put things inr equest ...)
         // setup menu_left.jsp
         // setup index content
         // ...
    }
}

But to setup the subscription webpage, I will have redundant code, because he has the same layout as index webpage

public class SetupSubscription extends Action {
    public ActionForward execute(
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
          // setup header.jsp (put things inr equest ...)
         // setup menu_left.jsp
         // setup subscription content
         // ...
    }
}

Now if I want to change the setup of  just header.jsp,
I need to modify the code of all the Setup actions,



Do u understand my problem ??

If u can help me with this I would really really appreciate ! thank u !
0
 
LVL 9

Author Comment

by:matthew016
ID: 17956726
That's why i did everything in the jsp itself
0
 
LVL 9

Assisted Solution

by:owenli27
owenli27 earned 100 total points
ID: 17974691
matthew,

In jsp:useBean, you could use both 'class' and 'type' attribute.  'type' is either the class itself, a superclass of class or an Interface implemented by class specified.  The referenced object is required to be of this 'type'.

Owen
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17974735
Cannot you define SetupSubscription to extend SetupIndex instead of Action to eliminate redundant code?
0
 
LVL 10

Accepted Solution

by:
jaggernat earned 400 total points
ID: 17974929
Have a single action class called "Setup" which extends DispatchAction so that you can have your own custom methods : setupIndex()  and  SetupSubscription()

public class Setup extends DispatchAction {

// common code  for  index *webpage*  and   subscription webpage  goes here
          // setup header.jsp (put things inr equest ...)
         // setup menu_left.jsp
         // setup index content
         // ...

//custom method : setupIndex
    public ActionForward setupIndex(
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
             
//other specific logic for setUpIndex
return  mapping.findForward("SetupIndexPage");  //forwards to index page

 }


//custom method : setupSubscription
public ActionForward setupSubscription (
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
         
//other specific  logic for setupSubscription
return  mapping.findForward("SetupSubscriptionPage"); //forwards to SetupSubscription Page
    }



}


hope that helps
J
0
 
LVL 9

Author Comment

by:matthew016
ID: 17974978
Thank u,

jaggernat, in your example, how do I handle the common code ?
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17974980
what is your common code doing?
0
 
LVL 9

Author Comment

by:matthew016
ID: 17974984
Owen,
>>In jsp:useBean, you could use both 'class' and 'type' attribute.  
>>'type' is either the class itself, a superclass of class or
>>an Interface implemented by class specified.  
>>The referenced object is required to be of this 'type'.

Thank u, but I don't want to have the class coded in the JSP, only interface,
to respect the IoC pattern.
I think I just need to make my own tag just to get a bean with type interface.
0
 
LVL 9

Author Comment

by:matthew016
ID: 17974993
jaggernat,
my commen code is setting up the header.jsp, footer.jsp, menu_left.jsp, menu_right.jsp,

the "not common" code is the content : content_index.jsp, content_subscription.jsp

For example in the common code of header.jsp,
I want to put the number of sessions, subscribed, and authenticated users in the request.
0
 
LVL 9

Author Comment

by:matthew016
ID: 17974996
In your example the common code is in comments //
But do I put this in methods ? then each custom execute methods would call these method with common code ?
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17975015
>>>>>
For example in the common code of header.jsp,
I want to put the number of sessions, subscribed, and authenticated users in the request.


so for index page and subscription page you are using the same page called  " header.jsp " . Am i right?

Is the information such as  "number of sessions, subscribed, and authenticated users in the request"  different for index page and subscription page?
0
 
LVL 9

Author Comment

by:matthew016
ID: 17975029
Well actually, the index page and subscription page have the same layout.µ
So per consequent :
It has the same header (so the same information in it as u asked)
same menu right, same footer, ...

0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17975040
so you want to construct the webpage (header, footer, menu_left, menu_right ) from the action class? am i right?
0
 
LVL 9

Author Comment

by:matthew016
ID: 17975056
Yes, I want to "setup" the jsp pages, with action classes.

Because e.g. :
it avoids me such code in header.jsp
(to get number of sessions in application, subscribed and authenticated people)

<%
     application.IGestionUtilisateurs gestionUtilisateurs =
          (controleur.IGestionUtilisateurs) application.getAttribute("gestionUtilisateurs");
%>
      String nbInscrits = ""+gestionUtilisateurs.getNbUtilisateurs();
      String nbConnectés = ""+gestionUtilisateurs.getNbAuthentifiés();
      String nbSessions = ""+main.GestionSessions.getNbSessions();
       <% if(nbInscrits != null && !nbInscrits.equals("")) { %>
            <%= nbInscrits%> inscrits,
      <% }
         if(nbConnectés != null && !nbConnectés.equals("")) { %>
            <%= nbConnectés%> connectés,
      <% }
         if(nbSessions != null && !nbSessions.equals("")) { %>
            <%= nbSessions%> sessions
      <% } %>

Instead I use a Setup action, that setups all my jsps (I put e.g. those information in request)
In this example header.jsp is shared by almost all the contents (index, subscription, profile, ...)
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17975091
since the above code is a common code you can put it in the common area

public class Setup extends DispatchAction {


// common code  for  index *webpage*  and   subscription webpage  goes here
// all your above code goes here. Number of sessions in application, subscribed and authenticated people

and then have custom methods

  public ActionForward setupIndex(
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
             
//other specific logic for setUpIndex
return  mapping.findForward("SetupIndexPage");  //forwards to a tile which would construct the jsp page for you.

 }


//custom method : setupSubscription
public ActionForward setupSubscription (
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
         
//other specific  logic for setupSubscription
return  mapping.findForward("SetupSubscriptionPage"); //forwards to a tile which would construct the jsp page for you.

    }


}

**  //forwards to a tile which would construct the jsp page for you.

do you have a file called tiles-def.xml which will construct the jsp page for you (header, footer, menu_left, menu_right )

0
 
LVL 9

Author Comment

by:matthew016
ID: 17975109
jaggernat,
sorry for botering !
But when u say "goes here", goes where ??
In methods ?
then I call them in the custom methods ?

Yes I have tiles-def.xml,
E.G.  in the index page I just have this :

<%@ taglib  uri="struts-tiles"  prefix="tiles" %>
<tiles:insert  definition=".index.loggedon"  flush="true"/>
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17975164
not inside methods. If its a common code, dont you think it should be outside the methods to avoid redundancy?

because this is what you posted earlier
>>But to setup the subscription webpage, I will have redundant code, because he has the same layout as index webpage

So, to avoid redundancy we put it at a common place so that we dont have to repeat it over and over in every method. Only if you feel the values will change for every method , you can put them inside the method.

now you will have to construct the jsp page using tiles.


for example for the index page you have
return  mapping.findForward("SetupIndexPage");  
in struts-config.xml file you will have something like
<action name="formbeanname"
                        path="/mapping"
                        scope="request"
                        validate="false"
                        parameter="method"
                        type="YourActionClassName">
                  
      <forward name="SetupIndexPage" path="indexpage.display"/>


In tiles-def you would have the definition for "indexpage.display" which is your actual index page
<definition name="indexpage.display" extends="yourLayoutPage">
<put name="header"                value="your header jsp page for index"/>
<put name="menuleft"       value="menu_left.jsp page"/>
<put name="menuright"       value="menu_right.jsp page"/>
<put name="body"                  value="your main body jsp page"/>      
<put name="footer"                value="your footer jsp page"/>      
</definition>

the above definition would construct the index jsp page for you.
You have to do the same thing with subscription page.
0
 
LVL 9

Author Comment

by:matthew016
ID: 17975177
Yes thank u,,
u didn't understand what I meant,
I mean I have to do somethionbg like this ?

public class Setup extends DispatchAction {

 PUBLIC VOID COMMENCODE() {
           // common code  for  index *webpage*  and   subscription webpage  goes here
          // setup header.jsp (put things inr equest ...)
         // setup menu_left.jsp
         // setup index content
         // ...
}

//custom method : setupIndex
    public ActionForward setupIndex(
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
 COMMENCODE();
//other specific logic for setUpIndex
return  mapping.findForward("SetupIndexPage");  //forwards to index page

 }


//custom method : setupSubscription
public ActionForward setupSubscription (
                    ActionMapping mapping, ActionForm form,
                    HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
COMMENCODE();
//other specific  logic for setupSubscription
return  mapping.findForward("SetupSubscriptionPage"); //forwards to SetupSubscription Page
    }
}


that's what I meant with puttinh the common method in the custom methods
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17975185
oh yes perfect :-)
0
 
LVL 9

Author Comment

by:matthew016
ID: 17975188
Ok i'll try it !
If I have problem I make new question ! U did more than enough !!
thank u very much again for your help !
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17975321
u r welcome!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java beans and EJBs 5 196
some methods private some are not 8 220
Coldfusion 10 - JSP File Not Found - 404 Error 5 98
Books that can get me started on JAVA 2 105
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
Building a successful professional career is a long and difficult journey, especially in case if your decisions are not chosen carefully. For example, if you think that you can get to the desired position without experience and apply for it, your ch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question