Announcement Announcement Module
Collapse
No announcement yet.
How to obtain the applicationContext in JSP file???? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to obtain the applicationContext in JSP file????

    Hi,

    I use spring as the container of my project. Now I come across a problem which is that I can not find a proper way of accuqiring the applicationContext.xml in the JSP file. i tried to use struts as the control which acts as a bridge between JSP view and Hibernate DAO layers, but struts isn't very robust as it is in some situations, So I decide to "Inject" service layer into JSP directly. I am not sure it is proper to use "Inject" here, but what I am trying to do is to "get" the applicationContext and use its getBean method to obtain the current service layer beans.

    Before I posted this thread, I searched the fourm and found a few suggestions that I am not supposed to use ClassPathXmlApplicationContext to obtain the applicationContext as it creates a new one which also creates bunch of new beans??? so what kind of method should I use to get the appcontext and beans without instantiating the new one?????

    I found one in the API which is WebApplicationContextUtils, but it's method
    require a parameter which is (ServletContext sc) ? how can i get ServletContext then???

    here is my code:

    Code:
    <%@ page import="org.springframework.context.ApplicationContext"%>
    <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
    
    <%@ page
    	import="org.springframework.context.support.FileSystemXmlApplicationContext"%>
    <%
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    %>

    Thanks

  • #2
    anyone knows????thanks

    Comment


    • #3
      If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:

      Code:
      <% ServletContext context = config.getServletContext(); %>
      However I would avoid doing things this way. JSPs are best used to transform a model into UI markup, they should avoid initiating service calls. So first I would try to investigate the following claim:

      struts isn't very robust as it is in some situations
      What situation are you facing that's leading you to come to this conclusion? Struts is a basic action-based MVC controller, I'm guessing there's a way to get it to do what you wish.

      Cheers,
      GB

      Comment


      • #4
        Originally posted by spiff View Post
        If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:

        Code:
        <% ServletContext context = config.getServletContext(); %>
        However I would avoid doing things this way. JSPs are best used to transform a model into UI markup, they should avoid initiating service calls. So first I would try to investigate the following claim:



        What situation are you facing that's leading you to come to this conclusion? Struts is a basic action-based MVC controller, I'm guessing there's a way to get it to do what you wish.

        Cheers,
        GB
        well, great, I will show you exactly where Struts turns to be sucks.

        but firstly, lets not get too far off the topic, would you plz show me in detail

        of how to get WebApplicationContext??? thanks

        Comment


        • #5
          As you suggested earlier, you could get the application context this way:

          Code:
          <% ApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext()); %>

          Comment


          • #6
            Originally posted by spiff View Post
            If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:

            Code:
            <% ServletContext context = config.getServletContext(); %>
            However I would avoid doing things this way. JSPs are best used to transform a model into UI markup, they should avoid initiating service calls. So first I would try to investigate the following claim:



            What situation are you facing that's leading you to come to this conclusion? Struts is a basic action-based MVC controller, I'm guessing there's a way to get it to do what you wish.

            Cheers,
            GB


            Ok, here is the situation, I am going to create a JSP called index.jsp. it populates news, members details from DB on to this JSP view. I know JSP's role is only to provide presentation to end users where Struts take user's control and pass it to service layer and get results back then put it on JSP.
            But when you come cross the problem like this one, You will get stuck on it if u r using struts. Index.jsp is the first page users will see, before that THERE IS NO REQUEST SENT TO STRUTS, SO how could struts possibly get user's request and pass it to service layer?? Because there aren't any requests to sturts' action yet. If you are using struts to do something like, register a member, post a comment which include explicit "post"/"get" action from a Form to Struts action class, then Struts is the best canditate.

            But for my case, struts is not, some of you may think that I can create a index.do so let struts handle the user request before it reaches to the index.jsp page. but it is not a good option though. Firstly, you can not make index.do as the welcome page, so when user type in http://youdomain/
            it will still get to index.jsp page with bunch of blank columns and fileds (coz struts didn't prepare for them ,index.do is not executed).

            There is a dirty trick of making the welcome page to be index.do, but it affects the chance of your website being collected by search engine.

            So Could any one plz, if you come cross the same situation, tell me how to solve this problem

            Comment


            • #7
              Redirecting to index.do inside your index.jsp should do it:
              Code:
              <c:redirect url="/index.do" />
              Cheers,
              GB

              Comment


              • #8
                Originally posted by spiff View Post
                Redirecting to index.do inside your index.jsp should do it:
                Code:
                <c:redirect url="/index.do" />
                Cheers,
                GB
                No, it doesn't.

                It redirect to index.do, then index.do get data from service layer and will forward it back to index.jsp, and then get back to index.do again, will be an infinite loop

                Comment


                • #9
                  Originally posted by spiff View Post
                  Redirecting to index.do inside your index.jsp should do it:
                  Code:
                  <c:redirect url="/index.do" />
                  Cheers,
                  GB
                  I wouldn't even consider using redirect in the first place,

                  1 redirect will get client side to request twice

                  2. reduce the chance of your site being collected by search engine

                  Comment


                  • #10
                    No, it doesn't.
                    Obviously if you send the redirect from index.jsp to index.do, your action should forward to a JSP other than index.jsp to avoid the infinite loop you mentioned.

                    1 redirect will get client side to request twice
                    That's usually not a problem. Your requirements may say otherwise.

                    2. reduce the chance of your site being collected by search engine
                    I don't know about that, someone else with more accurate experience could help you better.

                    Cheers,
                    GB

                    Comment


                    • #11
                      Originally posted by spiff View Post
                      Obviously if you send the redirect from index.jsp to index.do, your action should forward to a JSP other than index.jsp to avoid the infinite loop you mentioned.


                      That's usually not a problem. Your requirements may say otherwise.


                      I don't know about that, someone else with more accurate experience could help you better.

                      Cheers,
                      GB

                      I am simply saying that Struts is not a good option at this case.

                      As you suggested to let index.do forward to another page say index2.jsp

                      Then User enter http://youdomain.com/
                      firstly, they get to index.jsp --redirect to--> index.do-->forward to-->index2.jsp

                      I don't think this is a robust way of implementing this .
                      Why can't we just pust jsp script in index.jsp to retrieve data from DB?

                      Comment


                      • #12
                        Well it's up to you but I think the redirect approach is a pretty standard way to achieve what you want. The PetClinic sample bundled with the Spring distribution uses that strategy, for example.

                        Whether search engines will follow that redirect and gather information from your site, I don't know.

                        If you want to put scriptlets in your JSP and build the view from there, you can and it will work. I just wouldn't generalize this approach to all accessible URLs in my application.

                        Comment


                        • #13
                          Originally posted by spiff View Post
                          If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:

                          Code:
                          <% ServletContext context = config.getServletContext(); %>
                          However I would avoid doing things this way. JSPs are best used to transform a model into UI markup, they should avoid initiating service calls. So first I would try to investigate the following claim:



                          What situation are you facing that's leading you to come to this conclusion? Struts is a basic action-based MVC controller, I'm guessing there's a way to get it to do what you wish.

                          Cheers,
                          GB
                          BTW, Why did u say this is a dirty and quick way????? Is there any drawback of using this approach?? or is there any better ways ????

                          Comment


                          • #14
                            Quick because it was answering your immediate need. Dirty because I think Java code inside JSPs has several inconvenients:

                            . the Java code isn't reusable
                            . the JSP is harder to use and maintain by a web designer
                            . the JSP is harder to read

                            To me the better option is the one we've been discussing, namely using a Struts action to execute all service calls and build the model that will be rendered by the JSP view.

                            Putting your code in the JSP will work, surely. It's just not an approach that, if used throughout your webapp, will bring you any benefits.

                            Cheers,
                            GB

                            Comment


                            • #15
                              Originally posted by spiff View Post
                              Quick because it was answering your immediate need. Dirty because I think Java code inside JSPs has several inconvenients:

                              . the Java code isn't reusable
                              . the JSP is harder to use and maintain by a web designer
                              . the JSP is harder to read

                              To me the better option is the one we've been discussing, namely using a Struts action to execute all service calls and build the model that will be rendered by the JSP view.

                              Putting your code in the JSP will work, surely. It's just not an approach that, if used throughout your webapp, will bring you any benefits.

                              Cheers,
                              GB
                              Yes, I partially agree with you on some points

                              but, as you said the java code on jsp page isn't reuseable, this is not entirely true in some cases. Eg, You have a index page which has a column displays news to users. If you write a jsp fragments and save it as news.jsp(only for the news display) , then you can use jsp tag or script to include this news.jsp everywhere in your other pages, isn't this reuse of java code?

                              Comment

                              Working...
                              X