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);