File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes How to display a dispatcher-servlet.xml bean id in a JSP page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to display a dispatcher-servlet.xml bean id in a JSP page?" Watch "How to display a dispatcher-servlet.xml bean id in a JSP page?" New topic
Author

How to display a dispatcher-servlet.xml bean id in a JSP page?

Rebeccah Quevedo-Prastein
Greenhorn

Joined: Mar 17, 2011
Posts: 16
I'm leaving this up in case anyone has any input, but I decided it was silly for me to try to do this this way. I just added an "environment" property to each of the beans, put the build.jsp page into the application proper as a view and set it within showForm() if the querystring contains a "build" parameter, and added the environment property to the model.


...

I have a Spring 2.5 MVC web application that can be deployed with any of several back-end environments. I have a bean for each environment defined in dispatcher-servlet.xml, and depending on the deployment environment, I edit my controller's bean in dispatcher-servlet.xml to reference the appropriate back-end environment bean.

I also have a JSP page accessible to the end user (not in web-inf, not mapped to a servlet), that gives build information. I would like the JSP page to also display the deployment environment information, since that is something that the user can't otherwise tell (the same front-end URL may use any of several different back-end environments).

Is there a way to include the environment bean name or bean id in the JSP page? That would be the id of the bean referenced by projreqController.dbSubmitter in the snippet below.

representative snippet of dispatcher-servlet.xml:


Thanks,

Rebeccah
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

I would upgrade your version of spring and use profiles. This is available in all Spring releases after Spring 3.1
http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/


If you still want to access the bean name you could implement BeanNameAware . This is considered bad practice for an application to have dependencies or to need to know these types of details. This is also mentioned in the java doc I linked. If you wanted to go this route anyway, when you implement this interface the framework will call the setter defined so you would have to define a bean name field and a getter for it so you could access it.


Previous to Spring adding support for profiles there were other workarounds people had used. If you google around you may be able to find some of them.


[How To Ask Questions][Read before you PM me]
Rebeccah Quevedo-Prastein
Greenhorn

Joined: Mar 17, 2011
Posts: 16
Sorry for the off-topic reply, but what's with the spam link at the bottom of the page disguised as a post? I've seen jprofiler, jrebel, and probably others. The fake post reads, "I agree. Here's the link. <link to some software product or other> - it saves me about five hours per week."

Rebeccah
Rebeccah Quevedo-Prastein
Greenhorn

Joined: Mar 17, 2011
Posts: 16
Anyway, thank you for the links, Bill. As mentioned, I decided trying to get at the bean names was not a great idea, and I did find a different way to get the information that I wanted into the jsp page.

It looks like using profiles is a lot more complicated than simply editing two bean references in my XML configuration file, so I'll stick with what I've got there. I've really never understood why one wants to use annotations (except for automated testing) for most of what I configure in the XML file. I like having, and one of the originally touted benefits of Spring's dependency injection was having, the ability to get at some of this stuff (to see it and to modify it) outside of the compiled code.

Rebeccah
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

No problem. Just as a note anything you can do with annotations can be done in XML including Bean profiles. One of the links I provided shows the XML way. Also unless you are not building your Spring configuration files inside your WAR it is unlikely somebody will ever crack open a deployed WAR to make changes to the XML, typically you would just rebuild it anyway. Anyways glad you got it figured out.
Rebeccah Quevedo-Prastein
Greenhorn

Joined: Mar 17, 2011
Posts: 16
Thanks again.

We deploy these (internal) applications typically to a server that expands the .war files on deployment, and we actually do tinker with the deployed files from time to time. Which is what led to the question. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display a dispatcher-servlet.xml bean id in a JSP page?