bean taglib 学习

本文介绍并演示了Struts框架中Bean标签库的基本用法,包括如何定义、操作字符串和集合,以及如何进行国际化消息展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. <%@ page contentType="text/html;charset=GBK"%>
  2. <%@ page import="java.util.*"%>
  3. <%@ page import="edu.hust.vo.*"%>
  4. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
  5. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
  6. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
  7. <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
  8. <!-- bean:define学习 -->
  9. <bean:define id="str" value="hello vivian" />
  10. <h2>定义的String对象的内容为:${str}</h2><hr>
  11. <!-- bean:size学习 -->
  12. <%
  13.     Map map = new HashMap();
  14.     map.put("1", "One");
  15.     map.put("2", "Two");
  16.     map.put("3", "Three");
  17.     request.setAttribute("mapLen", map);
  18. %>
  19. <bean:size id="len" name="mapLen" scope="request" />
  20. <h2>Map的长度为:${len}</h2>
  21. <%
  22.     Collection col = new ArrayList();
  23.     col.add("One");
  24.     col.add("Two");
  25.     request.setAttribute("colLen", col);
  26. %>
  27. <bean:size id="len" name="colLen" scope="request" />
  28. <h2>Collection的长度为:${len}</h2><hr>
  29. <!-- bean:write学习 -->
  30. <!-- 1 常用方式 -->
  31. <%
  32.     /*
  33.     User user = new User();
  34.     user.setUid("u07");
  35.     user.setName("Forrest");
  36.     request.setAttribute("user", user);
  37.     */
  38. %>
  39. <!-- 2 改进:jsp中尽量不出现java代码 -->
  40. <!-- 只要是标签,在程序开发中就可以相互调用,因为所有的标签基本都是对四种属性范围的操作 -->
  41. <jsp:useBean id="user" scope="request" class="edu.hust.vo.User" />
  42. <jsp:setProperty name="user" property="uid" value="u07" />
  43. <jsp:setProperty name="user" property="name" value="Forrest" />
  44. <!-- 进行输出 -->
  45. <h2>使用el输出:user.uid --> ${user.uid} && user.name --> ${user.name}</h2>
  46. <h2>使用bean:write输出:user.uid --> <bean:write name="user" property="uid" scope="request" /> && 
  47.                         user.name --> <bean:write name="user" property="name" scope="request" /></h2><hr>
  48. <!-- bean:message学习 -->
  49. <!--
  50.     在ApplicationResources.properties中写入如下代码:
  51.     #欢迎光临
  52.     welcome = <h2>/u6b22/u8fce/u5149/u4e34</h2>
  53.     然后在bean:message中显示出来
  54. -->
  55. <bean:message key="welcome" />
  56. <!--
  57.     bean:message标签提供了占位功能, 即在输出的文件中占着一位, 这一位的数据根据标签动态填写。
  58.     在ApplicationResources.properties中改为如下代码:
  59.     #欢迎光临
  60.     welcome = <h2>/u6b22/u8fce {0} /u5149/u4e34</h2>
  61.     然后在bean:message中显示出来
  62. -->
  63. <bean:message key="welcome" arg0="Forrest" /><hr>
  64. <!-- bean:message 国际化 -->
  65. <!--
  66.     所有内容通过bean:message输出,之后所有的语言单独形成一个*.properties文件
  67.     在ApplicationResources.properties中配置中文;在在ApplicationResources_en_us.properties配置美国英语。
  68.     根据浏览器的不同,显示的语言也不同(浏览器默认英文则显示英文--浏览器自动识别, 自动调用相应的.properties文件),因为所有显示内容在*.properties中已经完成配置了。
  69.     补:浏览器默认语言:工具 -- Internet选项 -- 语言。
  70. -->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值