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

Source: Internet
Author: User

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

In general, the content of a custom label can be implemented without accessing the service, so inheriting tagsupport, overriding the doStartTag () method, enables basic functionality.

eg. A custom label that implements the/** function of an HTML tag

* @Comment * @Author Ron * @date August 30, 2016 a.m. 9:02:56 * * Public class Htmlunencodetags extends TagSupport {private Log GER Logger = Logmanager.getlogger (This.getclass ()); Private String encodestr; Private static final long serialversionuid = 1L; @Override public 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 = encodes Tr } }

However, in special cases, we need to access the service layer to obtain the information in the custom label's logical code, then how to assemble the service automatically through the annotation way. In spring MVC, it is 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, and we first need to write a way to get or calculate the unread number of messages in our service, which is assumed to be:

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

UserId: User only Mark

EmailConsts.ReadStatus.NO.getValue (): Gets the message unread status value for the enumeration

Custom label Logic class is Msgunreadmsgtags

To achieve 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; @Autowired private Usermessagefacade Usermessageservice; @Override public 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 >) {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 automatically assemble annotations with @autowired when defining Usermessageservice services, and you also need to use the ApplicationContext Getbean method to get the service bean when you use it.

As in the code block in the above code: Usermessageservice = This.getrequestcontext (). Getwebapplicationcontext (). Getbean ( Usermessagefacade.class);

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.