Link to home
Start Free TrialLog in
Avatar of matthew016
matthew016Flag for Belgium

asked on

Need a simple tag

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.
Avatar of jaggernat
jaggernat

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
Avatar of matthew016

ASKER

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.
may be if you can post some code, i will be able to help u better
<%
      application.IGestionUtilisateurs gestionUtilisateurs =
            (controleur.IGestionUtilisateurs) application.getAttribute("gestionUtilisateurs");
%>
Avatar of bloodredsun
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)
Thank u bloodredsun,
I understand this perfectly, and u must refer to the Setup/Submit pattern, no ?
If you are referring to the Setup/Submit pattern mentioned here http://wiki.apache.org/struts/DataEntryForm then yes exactly.

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 !
That's why i did everything in the jsp itself
SOLUTION
Avatar of Weiping Du
Weiping Du
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cannot you define SetupSubscription to extend SetupIndex instead of Action to eliminate redundant code?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u,

jaggernat, in your example, how do I handle the common code ?
what is your common code doing?
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.
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.
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 ?
>>>>>
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?
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, ...

so you want to construct the webpage (header, footer, menu_left, menu_right ) from the action class? am i right?
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, ...)
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 )

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"/>
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.
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
oh yes perfect :-)
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 !
u r welcome!