View Full Version : How to obtain the applicationContext in JSP file????
df2334
May 7th, 2008, 07:41 AM
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:
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.web.context.support.WebApplica tionContextUtils" %>
<%@ page
import="org.springframework.context.support.FileSystemXmlA pplicationContext"%>
<%
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
%>
Thanks
df2334
May 7th, 2008, 05:29 PM
anyone knows????thanks
spiff
May 7th, 2008, 09:51 PM
If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:
<% 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
df2334
May 7th, 2008, 10:08 PM
If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:
<% 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
spiff
May 7th, 2008, 10:20 PM
As you suggested earlier, you could get the application context this way:
<% ApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContex t(config.getServletContext()); %>
df2334
May 7th, 2008, 10:21 PM
If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:
<% 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
spiff
May 7th, 2008, 10:39 PM
Redirecting to index.do inside your index.jsp should do it:
<c:redirect url="/index.do" />
Cheers,
GB
df2334
May 7th, 2008, 10:42 PM
Redirecting to index.do inside your index.jsp should do it:
<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
df2334
May 7th, 2008, 10:45 PM
Redirecting to index.do inside your index.jsp should do it:
<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
spiff
May 7th, 2008, 10:57 PM
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
df2334
May 7th, 2008, 11:04 PM
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?
spiff
May 7th, 2008, 11:15 PM
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.
df2334
May 8th, 2008, 01:01 AM
If you want the quick and dirty way, you can retrieve the ServletContext using the implicit "config" variable inside your JSP:
<% 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 ????
spiff
May 8th, 2008, 10:13 AM
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
df2334
May 8th, 2008, 04:28 PM
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?:)
spiff
May 8th, 2008, 05:09 PM
That's more markup reuse, but I guess you're right in a way. You're a positive person aren't you ;)
I've asked around and apparently you can give hints to search engine crawlers (such as following redirects) with the robots.txt on your web server. Maybe that's something to investigate.
Cheers,
GB
df2334
May 9th, 2008, 03:12 AM
yeah, apparently only you and me are discussing this issue. Hope anybody else could give us better approach:)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.