您的位置:网站首页 > 精华知识 > 精华知识

晶核自定义标签(自定义标签)

摘要你们好,最近小活发现有诸多的小伙伴们对于晶核自定义标签,自定义标签这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看吧。

你们好,最近小活发现有诸多的小伙伴们对于晶核自定义标签,自定义标签这个问题都颇为感兴趣的,今天小活为大家梳理了下,一起往下看看吧。

1、 自定义一个在表格中显示用户信息的简单标签。

2、 对于简单标签的开发,我们只需要实现标签接口。为了简单起见,我们可以直接继承实现这个接口的TagSupport类。

3、 创建:类别自定义标签

4、 publicclassUserInfoTagextendsTagSupport {

5、 privateUserInfo user;

6、 @Override

7、 publicintdoStartTag()throwsJspException {

8、 try{

9、 JspWriter out=this.pageContext.getOut();

10、 if(user==null) {

11、 out.println('No UserInfo Found.');

12、 returnSKIP_BODY;

13、 }

14、 out.println('table width='500px' border='1' align='center'');

15、 out.println('tr');

16、 out.println('td width='20%'Username:/td');

17、 out.println('td' + user.getUserName() + '/td');

18、 out.println('/tr');

19、 out.println('tr');

20、 out.println('tdAge:/td');

21、 out.println('td' + user.getAge() + '/td');

22、 out.println('/tr');

23、 out.println('tr');

24、 out.println('tdEmail:/td');

25、 out.println('td' + user.getEmail() + '/td');

26、 out.println('/tr');

27、 out.println('/table');

28、 }catch(Exception e) {

29、 thrownewJspException(e.getMessage());

30、 }

31、 returnSKIP_BODY;

32、 }

33、 @Override

34、 publicintdoEndTag()throwsJspException {

35、 returneval_PAGE;

36、 }

37、 @Override

38、 publicvoidrelease() {

39、 super.release();

40、 this.user=null;

41、 }

42、 //getter and setters

43、 publicUserInfo getUser() {

44、 returnuser;

45、 }

46、 publicvoidsetUser(UserInfo user) {

47、 this.user=user;

48、 }

49、 }

50、 Create a tag library description file in Web-Inf. Tag library description

51、 ?xml version='1.0'encoding='UTF-8'?

52、 taglib version='2.0'xmlns='http://java.sun.com/xml/ns/j2ee'

53、 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'

54、 xsi:schemaLocation='http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd'

55、 tlib-version1.0/tlib-version

56、 jsp-version2.0/jsp-version

57、 short-namecc/short-name

58、 uri/mytaglib/uri

59、 tag

60、 nameshowUserInfo/name

61、 tag-classcom.mytags.UserInfoTag/tag-class

62、 body-contentempty/body-content

63、 attribute

64、 nameuser/name

65、 requiredfalse/required

66、 rtexprvaluetrue/rtexprvalue

67、 /attribute

68、 /tag

69、 /taglib

70、 配置web.xml

71、 jsp-config

72、 taglib

73、 taglib-uri/mytaglib/taglib-uri

74、 taglib-location/WEB-INF/mytaglib.tld/taglib-location

75、 /taglib

76、 /jsp-config

77、 在需要使用这个标签的jsp页面头中引入。

78、 %@ taglib uri='/mytaglib' prefix='cc'%

79、 使用(参考上面的使用步骤)

80、 因此,开发了一个简单的JSP标记。

以上就是自定义标签这篇文章的一些介绍,希望对大家有所帮助。

免责声明:本文章由会员“张同”发布如果文章侵权,请联系我们处理,本站仅提供信息存储空间服务如因作品内容、版权和其他问题请于本站联系