Spring MVC Custom Tags How to use @autowired to assemble annotations automatically

Source: Internet
Author: User

In the Spring MVC framework for Web application development, under some special requests we will use custom tags to accomplish some special functions.

In general, the content of a custom label can be implemented without access to the service, so inheriting TagSupport, overriding the doStartTag () method can implement the basic functionality.

eg. A custom label that implements the inverse semantics of an HTML tag

/**

* @Comment * @Author Ron * @date August 30, 2016 morning 9:02:56 */public class Htmlunencodetags extends TagSupport {Private Logger Logger = Logmanager.getlogger (This.getclass ());Private String encodestr;Private static final long serialversionuid = 1L;@Overridepublic int doStartTag () throws Jspexception {String outstr = "";try {if (Stringutils.isnotblank (ENCODESTR)) {outstr = STRINGESCAPEUTILS.UNESCAPEHTML4 (ENCODESTR);}Pagecontext.getout (). write (OUTSTR);} catch (IOException e) {Logger.error ("", e);}return Super.dostarttag ();}Public String Getencodestr () {return encodestr;}public void Setencodestr (String encodestr) {This.encodestr = Encodestr;}}

But in special cases, we need to access the service layer in the logic code of the custom tag to get the information, so how to automatically assemble the service through annotations? In spring MVC, it's not possible to use the method described above.

Custom tags are required here to inherit requestcontextawaretag instead of TagSupport.

For example, we need to implement a function to get the number of unread messages, we first need to write in our own service to get or calculate the number of unread messages, this is assumed to be:

Querymsgcount (Userid,emailconsts.readstatus.no.getvalue ());

UserId: User unique indication

EmailConsts.ReadStatus.NO.getValue (): Gets an enumerated message unread status value

The custom label logic class is msgunreadmsgtags

So to implement the ability to get the number of unread messages, the implementation code looks like this:

public class Msgunreadmsgtags extends Requestcontextawaretag {Private Logger Logger = Logmanager.getlogger (This.getclass ());private int userId;Private static final long serialversionuid = 1L;@AutowiredPrivate Usermessagefacade Usermessageservice;@Overridepublic int dostarttaginternal () throws Jspexception {String outstr = "";try {JspWriter writer = Pagecontext.getout ();Usermessageservice = This.getrequestcontext (). Getwebapplicationcontext (). Getbean (Usermessagefacade.class);int count = Usermessageservice.querymsgcount (Userid,emailconsts.readstatus.no.getvalue ());if (Count > 0) {if (Count > 99) {outstr = "<span class=\" badge\ ">99+</span>";}else{outstr = "<span class=\" badge\ ">" + Count + "</span>";}}Writer.write (OUTSTR);Writer.flush ();} catch (IOException e) {Logger.error ("Get Messages Count ioexception-------------->>>>>>>>>>", e);}return 0;}public int getUserId () {return userId;}public void Setuserid (int userId) {This.userid = userId;}}

Attention:

It is not enough to use @Autowired to automatically assemble annotations when defining a usermessageservice service . You also need to use ApplicationContext's Getbean method to get the bean for the service when you use it.

As the code block in the above code:

Usermessageservice = This.getrequestcontext (). Getwebapplicationcontext (). Getbean (Usermessagefacade.class); 

Spring MVC Custom Tags How to use @autowired to assemble annotations automatically

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.