Obtain the bean registered in spring from the custom tag subclass.

Source: Internet
Author: User
Tags apache tomcat

The custom tag subclass is:
Javax. servlet. jsp. tagext. simpletagsupport
Javax. servlet. jsp. tagext. tagsupport

......
Subclass. of course, the bean accessing spring in these subclasses also represents a close connection with the Spring framework. because Spring IoC. you can only use spring: webapplicationcontextutils. getwebapplicationcontext (SC) method to find the desired bean. someone will say this is not good! Not recommended. In another aspect, the webapplicationcontextutils. getwebapplicationcontext method must exist. The following is a scenario:

Public resources
For example, insurance in the social security system (elder care, medical care, work injury ,...). he is used in insurance declaration, billing, and insurance. which should be reused. there are many ways to save these resources: XML, properties, and database.

XML:
You can use a single class.

Properties:
You can use a single class, tool class

Database:
For enterprise applications (using a browser as the presentation layer ). it is necessary to consider reuse in multiple JSP pages. in JSP, the preferred choice for reuse is custom tags. in this way, Java code in JSP can be eliminated. there are two situations:
1. Use struts to get spring-managed bean to read the type of insurance. Put a list in the request and pass the type list as a parameter to the custom tag in JSP.
I used this method at the beginning. different HTML tags can be displayed in the insurance list: select and checkbox can be used. but later I found that almost all of them use the same HTML Tag: select. A problem was also found: for example, a JSP page. it only uses the list of insurance types. but I write an action. so can I retrieve the list of types of insurance from custom tags .?

2. Get spring-managed bean read type in custom tags
With 1 experience. declare a class inheritance immediately: simpletagsupport. use webapplicationcontextutils in the dotag () method. the getwebapplicationcontext method obtains the spring management bean and calls the bean method to obtain the list of insurance types. a null pointer exception (nullpointerexception) is thrown immediately ). so with this post:

Http://topic.csdn.net/u/20090902/16/b0400979-11fe-4744-bd84-2e49fb27facd.html

Someone said it was used: webapplicationcontextutils. getrequiredwebapplicationcontext () method. this method is also recommended in the spring reference manual. but this is not the key here. the two methods are in webapplicationcontext. the getwebapplicationcontext method returns NULL when the key value root_web_application_context_attribute does not correspond to any object. The getrequiredwebapplicationcontext () method throws the illegalstateexception

According to his prompt, I checked the spring reference manual in Section 15.2: general configuration:
All that one need do is to declare a contextloaderlistener in the standard J2EE servlet web. XML file of one's web application, and add a contextconfiglocation <context-Param/> section (in the same file) that defines which set of spring XML cpnfiguration files to load.
Find below the <listener/> Configuration:
<Listener> <br/> <listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class> <br/> </listener>
Find below the <context-Param/> Configuration:
<Context-param> <br/> <param-Name> contextconfiglocation </param-Name> <br/> <param-value>/WEB-INF/applicationcontext *. XML </param-value> <br/> </context-param>

I don't understand. I want to load a listener in Web. xml. But in struts. XML, I have loaded applicationcontext. xml:
<Plug-in classname = "org. springframework. web. struts. contextloaderplugin "> <br/> <set-Property =" contextconfiglocation "value ="/WEB-INF/classes/applicationcontext. XML "/> <br/> </plug-in>

Let's take a look at the startup message of Tomcat:
A. The following is the order in which org. springframework. Web. Context. contextloaderlistener is not configured in Web. xml:
...
Information: Starting Servlet Engine: Apache Tomcat/6.0.18
2009-9-8 14:15:57 org. Apache. Catalina. Core. applicationcontext log
Information: Set web app root system property: 'webapp. root '= [D:/workspace /. metadata /. plugins/org. eclipse. WST. server. core/tmp0/wtpwebapps/proangle/]

2009-9-8 14:15:57 org. Apache. Catalina. Core. applicationcontext log
Information: initializing log4j from [D:/workspace/. Metadata/. plugins/org. Eclipse. WST. server. Core/tmp0/wtpwebapps/proangle/WEB-INF/log4j. properties]

2009-9-8 14:15:58 org. Apache. Catalina. Core. applicationcontext log
Information: initializing webapplicationcontext for struts actionservlet 'action', module''

2009-9-8 14:16:02 org. Apache. Coyote. http11.http11protocol start
Information: Starting coyote HTTP/1.1 on http-8080
...
Information: server startup in 7744 MS

B. The order in which org. springframework. Web. Context. contextloaderlistener is configured in Web. XML is as follows:
...
Information: Starting Servlet Engine: Apache Tomcat/6.0.18
2009-9-8 10:16:20 org. Apache. Catalina. Core. applicationcontext log
Information: Set web app root system property: 'webapp. root '= [D:/workspace /. metadata /. plugins/org. eclipse. WST. server. core/tmp0/wtpwebapps/proangle/]

2009-9-8 10:16:20 org. Apache. Catalina. Core. applicationcontext log
Information: initializing log4j from [D:/workspace/. Metadata/. plugins/org. Eclipse. WST. server. Core/tmp0/wtpwebapps/proangle/WEB-INF/log4j. properties]
//----------------------------------------------------------------------
2009-9-8 10:16:21 org. Apache. Catalina. Core. applicationcontext log
Information: loading spring root webapplicationcontext
//----------------------------------------------------------------------
2009-9-8 10:16:26 org. Apache. Catalina. Core. applicationcontext log
Information: initializing webapplicationcontext for struts actionservlet 'action', module''

2009-9-8 10:16:26 org. Apache. Coyote. http11.http11protocol start
Information: Starting coyote HTTP/1.1 on http-8080
...
Information: server startup in 9609 MS

It can be seen from the sequence that starting webapplicationcontext in struts is later than web. listener in XML. you can also guess this. after all, Struts is registered to the Web as a servlet. XML. not on the web. contextloaderlistener configured in XML cannot retrieve spring bean. this can only be inferred. custom tags cannot share the webapplicationcontext loaded by struts ..

Environment for testing the above results:
Spring-version: 2.0.2
Struts 1.2.9
JDK 6
Tomcat 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.