Struts2 标签

一 Struts标签的简介:

  Struts2 自己封装了一套标签,比 JSTL 强大,而且与 Struts2 中的其他功能无缝结合。 当然 Strust2 标签的内容很多,随着版本的升级,标签和属性越来越多。我们要掌握好核心标签及了解其他标签; 根据功能可以分为:数据标签,控制标签,界面标签,其他标签;(其实如果学过标准标签库stl基本上这部分内容简单的一批)

二 Struts2 数据标签

Property 标签:输出 OGNL 表达式的值; 取值

 <% request.setAttribute("name","<font color='red' >张三</font>"); %>
<body>
<h1>property</h1>
<s:property value="#request.name"/><br/>
<s:property value="#request.name2" default="某某"/><br/>
<s:property value="#request.name" escapeHtml="false"/><br/>
</body>

Set 标签:设置变量;

 <h1>set标签</h1>
<s:set var="i" value="1"/><br/>
i= <s:property value="#i"/><br/>
<s:set var="a" value="'action的值类似于值栈(value-stack)'" scope="action"></s:set>
<s:set var="p" value="'page的值'" scope="page"></s:set>
<s:set var="re" value="'request值'" scope="request"></s:set>
<s:set var="se" value="'session值'" scope="session"></s:set>
<s:set var="app" value="'application值'" scope="application"></s:set>
action:<s:property value="#a"/><br/>
page:<s:property value="#attr.p"/><br/>
request:<s:property value="#request.re"/><br/>
session:<s:property value="#session.se"/><br/>
application:<s:property value="#application.app"/><br/>

Bean 标签:定义 javaBean 对象;

 <h1>bean标签</h1>
<s:bean name="com.java1234.model.Student" var="student">
<s:param name="name" value="'张三'"></s:param>
<s:param name="age" value="18"></s:param>
</s:bean>
name:<s:property value="#student.name"/><br/>
age:<s:property value="#student.age"/><br/>

Date 标签:日期标签;

 <%  request.setAttribute("date", new Date());  %>
<body>
<h1>Date标签</h1>
日期:&nbsp;&nbsp; ${date }<br/>
日期:&nbsp;&nbsp; <s:date name="#request.date" format="yyyy-MM-dd"/>
</body>

Debug 标签:调试标签;

 <h1>Debug标签</h1>
<s:debug></s:debug>

Url&a 标签:超链接标签;

 <s:a action="hello" namespace="/forgroud" >

 <s:param name="name" value="'struts2'"></s:param>

 链接a

 </s:a>

Include 标签:动态包含标签;

 <s:include value="head.html"></s:include>

三 Struts2 控制标签

Ifelse 标签:条件判断标签;

 <% int age=11;   request.setAttribute("age", age);    %>
<body>
<h1>if else标签</h1>
年龄:<s:property value="#request.age"/>
<s:if test="#request.age<18">
小于18岁
</s:if>
<s:elseif test="#request.age<30">
大于18岁 小于30岁
</s:elseif>
<s:else>
大于30岁
</s:else>
</body>

Iterator 标签:遍历标签;

 <%
List<Student>studentList=new ArrayList<Student>();
studentList.add(new Student(1,"张三",18));
studentList.add(new Student(2,"李四",19));
studentList.add(new Student(5,"王五",25));
request.setAttribute("studentList", studentList);
%>
<body>
<h1>iterator</h1>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<s:iterator value="#request.studentList" var="sl" status="status">
<tr>
<td><s:property value="#status.index+1"/></td>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</table>
</body>

Append 标签:叠加标签;

 <%
List<Student>studentList=new ArrayList<Student>();
List<Student>studentList1=new ArrayList<Student>();
studentList.add(new Student(1,"张三",18));
studentList.add(new Student(2,"李四",19));
studentList.add(new Student(5,"王五",25));
studentList1.add(new Student(6,"赵六",27));
studentList1.add(new Student(7,"小七",28));
request.setAttribute("studentList", studentList);
request.setAttribute("studentList1", studentList1);
%>
<body>
<s:append var="studentList2">
<s:param value="#request.studentList"></s:param>
<s:param value="#request.studentList1"></s:param>
</s:append>
<h1>iterator</h1>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<s:iterator value="#request.studentList2" var="sl" status="status">
<tr>
<td><s:property value="#status.index+1"/></td>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</table>
</body>

Generator 标签:分隔标签;

 <s:generator separator="," val="'张三,李四,王五'" var="nameList"></s:generator>
  <s:iterator value="#nameList">
<s:property/>
</s:iterator>

Merge 标签:组合标签;

Sort 标签:排序标签;我们使用一个简单的例子学习一下:

Model:Student类:

 public class Student {
private String name;
private int id;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student( int id, String name,int age) {
super();
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

排序类:com.java1234.compator.MyComparator代码:

  public class MyComparator  implements Comparator<Student>{
public int compare(Student s1,Student s2){
if(s1.getAge()>s2.getAge()){
return 1;
}else if(s1.getAge()<s2.getAge()){
return -1;
}else{
return 0;
}
}
 <%
List<Student>studentList=new ArrayList<Student>();
studentList.add(new Student(1,"张三",18));
studentList.add(new Student(2,"李四",19));
studentList.add(new Student(5,"王五",25));
studentList.add(new Student(6,"王五",17));
studentList.add(new Student(7,"王五",19));
request.setAttribute("studentList", studentList);
%>
<body>
<h1>iterator</h1>
<s:bean id="Mycomparator" name="com.java1234.compator.MyComparator"></s:bean>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<s:sort comparator="#Mycomparator" source="#request.studentList">
<s:iterator status="status">
<tr>
<td><s:property value="#status.index+1"/></td>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</s:sort>
</table>

Subset 标签:截取标签;

 <%
List<Student>studentList=new ArrayList<Student>();
studentList.add(new Student(1,"张三",18));
studentList.add(new Student(2,"李四",19));
studentList.add(new Student(5,"王五",25));
studentList.add(new Student(6,"赵六",28));
request.setAttribute("studentList", studentList);
%>
<body>
<h1>subset</h1>
<table border="1">
<tr>
<th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<s:subset source="#request.studentList" start="2" count="2">
<s:iterator status="status">
<tr>
<td><s:property value="#status.index+1"/></td>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="age"/></td>
</tr>
</s:iterator>
</s:subset>
</table>
</body>

四 Strut2 界面标签

Form 标签:表单提交标签;

 <s:form method="post" action="hello" namespace="/forgroud">提交内容</s:form> 

Text 标签:文本标签;

 用户名:<s:textfield name="name"></s:textfield>
密码:<s:password name="password"></s:password>
备注:<s:textarea name="desc"></s:textarea>

Radios 标签:单选标签;

 性别单选框: <s:radio list="#{0:'男',1:'女' }" name="sex" value="0"></s:radio>

Checkboxlist 标签:复选框标签;

 <s:checkboxlist list="#{0:'唱歌',1:'跳舞',2:'篮球' }"name="hobby" value="1"></s:checkboxlist>

Select 标签:下拉框标签;

 下拉框: <s:select list="#{0:'跳舞',1:'篮球',2:'唱歌' }" name="bobby" value="0"></s:select>

Struts2的标签三大类是什么?的更多相关文章

  1. Struts2+Spring+Hibernate 三大框架的合并集成

    这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样.  首先看一 ...

  2. Struts2之Struts2的标签库

    前言: Struts2提供了大量的标签 ,用来帮助开发表现层页面,这些表现一方面解决了美观性的需求,因为它们具有html标签一样的外观,另一方面它们解决了功能性的需求, 因为它们具有jsp脚本一样的逻 ...

  3. 【Java EE 学习 36】【struts2】【struts2系统验证】【struts2 ognl值栈】【struts2 ongl标签】【struts2 UI标签】【struts2模型驱动和令牌机制】

    一.struts2系统验证 1.基于struts2系统验证的方式实际上就是通过配置xml文件的方式达到验证的目的. 2.实际上系统校验的方法和手工校验的方法在底层的基本实现是相同的.但是使用系统校验的 ...

  4. Struts2常用标签

    Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的 ...

  5. Struts2的标签库(五)——表单标签

    Struts2的标签库(五) --表单标签 几个特殊的表单标签的使用: 1.checkboxlist标签 该标签用于创建多个复选框,用于同时生成多个<input type="check ...

  6. Struts2的标签库(四)——数据标签

    Struts2的标签库(四) --数据标签 1.action标签 该标签用于在jsp页面直接调用一个Action,通过指定executeResult参数,还可以将Action的处理结果包含到此页面中来 ...

  7. Struts2的标签库(三)——控制标签

    Struts2的标签库(三) --控制标签 1.if/elseif/else标签 用于分支控制,取代JSP中的if语句,根据一个boolean(test属性的值)值判断是否进行下一步运算或者输出等. ...

  8. Struts2的标签库(一)——使用Struts2的标签库

    Struts2的标签库(一) --使用Struts2的标签库 1.Struts2的标签库其实就是一个自定义的标签库,所以它也有它的标签处理类和标签库定义文件: 2.和所有自定义标签一样,我们可以找到S ...

  9. JavaWeb框架_Struts2_(五)----->Struts2的标签库

    1.  Struts2的标签库 1.1 Struts2标签库概述 Struts2的标签库可以分为以下3类:用户界面标签.非用户界面标签.AJAX标签; 2.1.1 Struts2标签库的分类和使用 1 ...

随机推荐

  1. 国庆集训 || Wannafly Day1

    网址:https://www.nowcoder.com/acm/contest/201#question A.签到 手速石头剪刀布 #include <cstdio> #include & ...

  2. js正则函数match、exec、test、search、replace、split使用集合

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...

  3. word转HTML在layuiadmin中锚点调转问题

    前言: 在以前我们讲过把word转成HTML移植入自己的web项目使用:Word转html并移植到web项目 正文: 发现如果在layuiadmin框架中,页面里锚点跳转时会不正常(会跳转到新页面): ...

  4. Ubuntu中安装配置 JDK与apache

    一,前期准备: 1.下载apach网址:https://tomcat.apache.org/download-90.cgi 3.下载:jdk网址:http://www.oracle.com/techn ...

  5. perl 对ENV环境变量的使用

    1.hash 方式访问. %ENV  key为环境变量名,value为环境变量值 2.调用ENV模块 . use Env qw(PATH); print "path is $ENV{path ...

  6. C++代码学习之一:组合模式例子

    #include"AbstractFile.h" void AbstractFile::add(AbstractFile*) { } void AbstractFile::remo ...

  7. Linux文件权限与文件夹权限实践

    文件权限在基础中有介绍,不在重复 一.文件夹权限: 示例: 解释说明: r --read 既ls w --write     既创建新的目录或者文件 x --execute 既cd 现在有4个用户分属 ...

  8. sweetalert使用随笔

    删除前确认框: //找到删除那天记录的按钮,触发点击事件 $(".del").on('click', function () { swal({ title: "操作确认& ...

  9. Codeforces Round #439 (Div. 2) A. The Artful Expedient

    A. The Artful Expedient 题目链接http://codeforces.com/contest/869/problem/A 解题心得:就是一个水题,读懂题就好,题意是,(i,j)a ...

  10. MySQL常用命令(三)---最值的搜索

    表结构存储数据如下: 需求如下: 1.每项物品的的最高价格是多少?(下面这个查询语句的结果集中你会发现 经销商(dealer)的值不对,第二个语句就是对的)2.对每项物品,找出最贵价格的物品的经销商. ...