Struts2 标签
Struts2 自己封装了一套标签,比 JSTL 强大,而且与 Struts2 中的其他功能无缝结合。 当然 Strust2 标签的内容很多,随着版本的升级,标签和属性越来越多。我们要掌握好核心标签及了解其他标签; 根据功能可以分为:数据标签,控制标签,界面标签,其他标签;(其实如果学过标准标签库stl基本上这部分内容简单的一批)
Property 标签:输出 OGNL 表达式的值; 取值
1 <% request.setAttribute("name","<font color=‘red‘ >张三</font>"); %> 2 <body> 3 <h1>property</h1> 4 <s:property value="#request.name"/><br/> 5 <s:property value="#request.name2" default="某某"/><br/> 6 <s:property value="#request.name" escapeHtml="false"/><br/> 7 </body>
Set 标签:设置变量;
1 <h1>set标签</h1> 2 <s:set var="i" value="1"/><br/> 3 i= <s:property value="#i"/><br/> 4 <s:set var="a" value="‘action的值类似于值栈(value-stack)‘" scope="action"></s:set> 5 <s:set var="p" value="‘page的值‘" scope="page"></s:set> 6 <s:set var="re" value="‘request值‘" scope="request"></s:set> 7 <s:set var="se" value="‘session值‘" scope="session"></s:set> 8 <s:set var="app" value="‘application值‘" scope="application"></s:set> 9 action:<s:property value="#a"/><br/> 10 page:<s:property value="#attr.p"/><br/> 11 request:<s:property value="#request.re"/><br/> 12 session:<s:property value="#session.se"/><br/> 13 application:<s:property value="#application.app"/><br/>
Bean 标签:定义 javaBean 对象;
1 <h1>bean标签</h1> 2 <s:bean name="com.java1234.model.Student" var="student"> 3 <s:param name="name" value="‘张三‘"></s:param> 4 <s:param name="age" value="18"></s:param> 5 </s:bean> 6 name:<s:property value="#student.name"/><br/> 7 age:<s:property value="#student.age"/><br/>
Date 标签:日期标签;
1 <% request.setAttribute("date", new Date()); %> 2 <body> 3 <h1>Date标签</h1> 4 日期: ${date }<br/> 5 日期: <s:date name="#request.date" format="yyyy-MM-dd"/> 6 </body>
Debug 标签:调试标签;
1 <h1>Debug标签</h1> 2 <s:debug></s:debug>
Url&a 标签:超链接标签;
1 <s:a action="hello" namespace="/forgroud" > 2 3 <s:param name="name" value="‘struts2‘"></s:param> 4 5 链接a 6 7 </s:a>
Include 标签:动态包含标签;
1 <s:include value="head.html"></s:include>
三 Struts2 控制标签
Ifelse 标签:条件判断标签;
1 <% int age=11; request.setAttribute("age", age); %> 2 <body> 3 <h1>if else标签</h1> 4 年龄:<s:property value="#request.age"/> 5 <s:if test="#request.age<18"> 6 小于18岁 7 </s:if> 8 <s:elseif test="#request.age<30"> 9 大于18岁 小于30岁 10 </s:elseif> 11 <s:else> 12 大于30岁 13 </s:else> 14 </body>
Iterator 标签:遍历标签;
1 <% 2 List<Student>studentList=new ArrayList<Student>(); 3 studentList.add(new Student(1,"张三",18)); 4 studentList.add(new Student(2,"李四",19)); 5 studentList.add(new Student(5,"王五",25)); 6 request.setAttribute("studentList", studentList); 7 %> 8 <body> 9 <h1>iterator</h1> 10 <table border="1"> 11 <tr> 12 <th>序号</th> 13 <th>学号</th> 14 <th>姓名</th> 15 <th>年龄</th> 16 </tr> 17 <s:iterator value="#request.studentList" var="sl" status="status"> 18 <tr> 19 <td><s:property value="#status.index+1"/></td> 20 <td><s:property value="id"/></td> 21 <td><s:property value="name"/></td> 22 <td><s:property value="age"/></td> 23 </tr> 24 </s:iterator> 25 </table> 26 </body>
Append 标签:叠加标签;
1 <% 2 List<Student>studentList=new ArrayList<Student>(); 3 List<Student>studentList1=new ArrayList<Student>(); 4 studentList.add(new Student(1,"张三",18)); 5 studentList.add(new Student(2,"李四",19)); 6 studentList.add(new Student(5,"王五",25)); 7 studentList1.add(new Student(6,"赵六",27)); 8 studentList1.add(new Student(7,"小七",28)); 9 request.setAttribute("studentList", studentList); 10 request.setAttribute("studentList1", studentList1); 11 %> 12 <body> 13 <s:append var="studentList2"> 14 <s:param value="#request.studentList"></s:param> 15 <s:param value="#request.studentList1"></s:param> 16 </s:append> 17 <h1>iterator</h1> 18 <table border="1"> 19 <tr> 20 <th>序号</th> 21 <th>学号</th> 22 <th>姓名</th> 23 <th>年龄</th> 24 </tr> 25 <s:iterator value="#request.studentList2" var="sl" status="status"> 26 <tr> 27 <td><s:property value="#status.index+1"/></td> 28 <td><s:property value="id"/></td> 29 <td><s:property value="name"/></td> 30 <td><s:property value="age"/></td> 31 </tr> 32 </s:iterator> 33 </table> 34 </body>
Generator 标签:分隔标签;
1 <s:generator separator="," val="‘张三,李四,王五‘" var="nameList"></s:generator> 2 <s:iterator value="#nameList"> 3 <s:property/> 4 </s:iterator>
Merge 标签:组合标签;
Sort 标签:排序标签;我们使用一个简单的例子学习一下:
Model:Student类:
1 public class Student { 2 private String name; 3 private int id; 4 private int age; 5 public Student() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 public Student( int id, String name,int age) { 10 super(); 11 this.name = name; 12 this.id = id; 13 this.age = age; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public int getId() { 22 return id; 23 } 24 public void setId(int id) { 25 this.id = id; 26 } 27 public int getAge() { 28 return age; 29 } 30 public void setAge(int age) { 31 this.age = age; 32 } 33 }
排序类:com.java1234.compator.MyComparator代码:
1 public class MyComparator implements Comparator<Student>{ 2 public int compare(Student s1,Student s2){ 3 if(s1.getAge()>s2.getAge()){ 4 return 1; 5 }else if(s1.getAge()<s2.getAge()){ 6 return -1; 7 }else{ 8 return 0; 9 } 10 } 11 }
1 <% 2 List<Student>studentList=new ArrayList<Student>(); 3 studentList.add(new Student(1,"张三",18)); 4 studentList.add(new Student(2,"李四",19)); 5 studentList.add(new Student(5,"王五",25)); 6 studentList.add(new Student(6,"王五",17)); 7 studentList.add(new Student(7,"王五",19)); 8 request.setAttribute("studentList", studentList); 9 %> 10 <body> 11 <h1>iterator</h1> 12 <s:bean id="Mycomparator" name="com.java1234.compator.MyComparator"></s:bean> 13 <table border="1"> 14 <tr> 15 <th>序号</th> 16 <th>学号</th> 17 <th>姓名</th> 18 <th>年龄</th> 19 </tr> 20 <s:sort comparator="#Mycomparator" source="#request.studentList"> 21 <s:iterator status="status"> 22 <tr> 23 <td><s:property value="#status.index+1"/></td> 24 <td><s:property value="id"/></td> 25 <td><s:property value="name"/></td> 26 <td><s:property value="age"/></td> 27 </tr> 28 </s:iterator> 29 </s:sort> 30 </table>
Subset 标签:截取标签;
1 <% 2 List<Student>studentList=new ArrayList<Student>(); 3 studentList.add(new Student(1,"张三",18)); 4 studentList.add(new Student(2,"李四",19)); 5 studentList.add(new Student(5,"王五",25)); 6 studentList.add(new Student(6,"赵六",28)); 7 request.setAttribute("studentList", studentList); 8 %> 9 <body> 10 <h1>subset</h1> 11 <table border="1"> 12 <tr> 13 <th>序号</th> 14 <th>学号</th> 15 <th>姓名</th> 16 <th>年龄</th> 17 </tr> 18 <s:subset source="#request.studentList" start="2" count="2"> 19 <s:iterator status="status"> 20 <tr> 21 <td><s:property value="#status.index+1"/></td> 22 <td><s:property value="id"/></td> 23 <td><s:property value="name"/></td> 24 <td><s:property value="age"/></td> 25 </tr> 26 </s:iterator> 27 </s:subset> 28 </table> 29 </body>
四 Strut2 界面标签
Form 标签:表单提交标签;
1 <s:form method="post" action="hello" namespace="/forgroud">提交内容</s:form>
Text 标签:文本标签;
1 用户名:<s:textfield name="name"></s:textfield> 2 密码:<s:password name="password"></s:password> 3 备注:<s:textarea name="desc"></s:textarea>
Radios 标签:单选标签;
1 性别单选框: <s:radio list="#{0:‘男‘,1:‘女‘ }" name="sex" value="0"></s:radio>
Checkboxlist 标签:复选框标签;
1 <s:checkboxlist list="#{0:‘唱歌‘,1:‘跳舞‘,2:‘篮球‘ }"name="hobby" value="1"></s:checkboxlist>
Select 标签:下拉框标签;
1 下拉框: <s:select list="#{0:‘跳舞‘,1:‘篮球‘,2:‘唱歌‘ }" name="bobby" value="0"></s:select>
原文:https://www.cnblogs.com/zyxsblogs/p/10940940.html