Struts1_学习笔记3_struts0300_taglib_bean_logic_Iterate标签

2024-02-13 10:18

本文主要是介绍Struts1_学习笔记3_struts0300_taglib_bean_logic_Iterate标签,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

bean标签:

Action:

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 普通属性
request.setAttribute("hello", "Hello World");
// html文本
request.setAttribute("hz", "<font color='red'>杭州欢迎您</font>");
// 日期
request.setAttribute("today", new Date());
// 数字
request.setAttribute("n", 123456.987);
// 结构
Group group = new Group();
group.setName("和盈");
User user = new User();
user.setUsername("张三");
user.setAge(18);
user.setGroup(group);
request.setAttribute("user", user);

return mapping.findForward("success");
}



页面显示:


<h1>测试BeanWrite</h1>
<hr>
<li>普通字符串</li><br>
hello(jsp脚本):<%=request.getAttribute("hello") %><br>
hello(标签):<bean:write name="hello"/><br>
<p>
<li>html文本</li><br>
hz(default):<bean:write name="hz"/><br>
hz(filter="true"):<bean:write name="hz" filter="true"/><br>
hz(filter="false"):<bean:write name="hz" filter="false"/><br>
<p>
<li>格式化日期</li><br>
today(default):<bean:write name="today"/><br>
today(format="yyyy-MM-dd HH:mm:ss"):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/>
<p>
<li>格式化数字</li><br>
n(default):<bean:write name="n"/><br>
n(format="###,###.####"):<bean:write name="n" format="###,###.####"/><br>
n(format="###,###.####"):<bean:write name="n" format="###,###.0000"/><br>
<p>
<li>结构</li><br>
姓名:<input type="text" value="<bean:write name="user" property="username"/>"><br>
年龄:<input type="text" value="<bean:write name="user" property="age"/>"><br>
所属组:<input type="text" value="<bean:write name="user" property="group.name"/>"><br>
</body>


配置文件:

<action-mappings>
<action path="/beanwrite" type="com.aowin.struts.BeanWriteTestAction">
<forward name="success" path="/beanwrite.jsp" />
</action>

<action path="/emptypresent" type="com.aowin.struts.EmptyPresentTestAction">
<forward name="success" path="/emptypresent.jsp" />
</action>

<action path="/iterate" type="com.aowin.struts.IterateTestAction">
<forward name="success" path="/iterate.jsp" />
</action>
</action-mappings>

<message-resources parameter="MessageResources" />


使用struts1 bean:write标签必须提供一份国际化文件: MessageResources
否则会报:
Cannot find message resources under key org.apache.struts.action.MESSAGE
在src下面提供一份文件即可 内容为空也可以


使用bean标签需要在页面中加入:

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>



logic标签:
Action:

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("attr1", null);
request.setAttribute("attr2", "");
request.setAttribute("attr3", new ArrayList());
return mapping.findForward("success");
}


页面使用标签:

<h1>测试empty,notEmpty,present,notPresent</h1>
<hr>
<logic:empty name="attr1">
attr1为空<br>
</logic:empty>
<logic:notEmpty name="attr1">
attr1不为空<br>
</logic:notEmpty>
<logic:present name="attr1">
attr1存在<br>
</logic:present>
<logic:notPresent name="attr1">
attr1不存在<br>
</logic:notPresent>

<p>
<logic:empty name="attr2">
attr2为空<br>
</logic:empty>
<logic:notEmpty name="attr2">
attr2不为空<br>
</logic:notEmpty>
<logic:present name="attr2">
attr2存在<br>
</logic:present>
<logic:notPresent name="attr2">
attr2不存在<br>
</logic:notPresent>

<p>
<logic:empty name="attr3">
attr3为空<br>
</logic:empty>
<logic:notEmpty name="attr3">
attr3不为空<br>
</logic:notEmpty>
<logic:present name="attr3">
attr3存在<br>
</logic:present>
<logic:notPresent name="attr3">
attr3不存在<br>
</logic:notPresent>



Iterate标签:


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Group group = new Group();
group.setName("和盈");

List userList = new ArrayList();
for (int i=0; i<10; i++) {
User user = new User();
user.setUsername("user_" + i);
user.setAge(18+i);
user.setGroup(group);
userList.add(user);
}

request.setAttribute("userlist", userList);

return mapping.findForward("success");
}



页面显示:

<h1>测试Iterate</h1>
<hr>
<li>jsp脚本</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<%
List userList = (List)request.getAttribute("userlist");
if (userList == null || userList.size() == 0) {
%>
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
<%
}else {
for (Iterator iter=userList.iterator(); iter.hasNext(); ) {
User user = (User)iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getAge() %></td>
<td><%=user.getGroup().getName() %></td>
</tr>
<%
}
}
%>
</table>

<p>
<li>标签</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<logic:empty name="userlist">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</logic:empty>
<logic:notEmpty name="userlist">
<logic:iterate id="u" name="userlist">
<tr>
<td>
<bean:write name="u" property="username"/>
</td>
<td>
<bean:write name="u" property="age"/>
</td>
<td>
<bean:write name="u" property="group.name"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>


使用Iterate标签加上:

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>

这篇关于Struts1_学习笔记3_struts0300_taglib_bean_logic_Iterate标签的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/705251

相关文章

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结

python解析HTML并提取span标签中的文本

《python解析HTML并提取span标签中的文本》在网页开发和数据抓取过程中,我们经常需要从HTML页面中提取信息,尤其是span元素中的文本,span标签是一个行内元素,通常用于包装一小段文本或... 目录一、安装相关依赖二、html 页面结构三、使用 BeautifulSoup javascript

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用