一、数据标签
index.jsp里:<a href="testAction1!test1?name=jack">测试Struts2的数据标签</a><br>
struts.xml里:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <package name="xsl" namespace="/" extends="struts-default"> 5 <action name="testAction1" class="com.xsl.action.TestAction1"> 6 <result name="test1">/tag1.jsp</result> 7 </action> 8 </package> 9 </struts>
TestAction1.java里:
1 package com.xsl.action; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.xsl.entity.Users; 7 8 import javax.servlet.http.HttpServletRequest; 9 10 import org.apache.struts2.interceptor.ServletRequestAware; 11 12 import com.opensymphony.xwork2.ActionSupport; 13 14 public class TestAction1 extends ActionSupport implements ServletRequestAware { 15 private String name; 16 private HttpServletRequest request; 17 public String test1(){ 18 String sname="tom"; 19 request.setAttribute("sname", sname); 20 return "test1"; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public void setServletRequest(HttpServletRequest request) { 29 this.request = request; 30 } 31 32 }
tag1.jsp里:
${name }---${sname }<br>
<s:property value="name"/><br>
<s:property value="#request.sname"/><br>
最终页面上显示:
jack---tom
jack
tom
二、控制标签
index.jsp里:<a href="testAction1!test2">测试struts2的控制标签</a><br>
struts.xml里:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <package name="xsl" namespace="/" extends="struts-default"> 5 <action name="testAction1" class="com.xsl.action.TestAction1"> 6 <result name="test2">/tag2.jsp</result> 7 </action> 8 </package> 9 </struts>
TestAction1.java里:
1 package com.xsl.action; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.xsl.entity.Users; 7 8 import javax.servlet.http.HttpServletRequest; 9 10 import org.apache.struts2.interceptor.ServletRequestAware; 11 12 import com.opensymphony.xwork2.ActionSupport; 13 14 public class TestAction1 extends ActionSupport implements ServletRequestAware { 15 private String name; 16 private HttpServletRequest request; 17 public String test2(){ 18 List<Users> list = new ArrayList<Users>(); 19 list.add(new Users(1,"tom1")); 20 list.add(new Users(2,"tom2")); 21 list.add(new Users(3,"tom3")); 22 list.add(new Users(4,"tom4")); 23 list.add(new Users(5,"tom5")); 24 list.add(new Users(6,"tom6")); 25 request.setAttribute("ulist",list ); 26 return "test2"; 27 } 28 public String getName() { 29 return name; 30 } 31 public void setName(String name) { 32 this.name = name; 33 } 34 public void setServletRequest(HttpServletRequest request) { 35 this.request = request; 36 } 37 38 }
tag2.jsp里:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s" %> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>tag2.jsp</title> 8 </head> 9 10 <body> 11 <s:iterator value="#request.ulist" var="user"> 12 <!-- 显示list集合里的所有数据 --> 13 <!-- 14 <s:property value="#user.id"/>---<s:property value="#user.uname"/><br> 15 --> 16 <!-- if条件语句 --> 17 <s:if test="#user.id<=2"> 18 <s:property value="#user.id"/>---<s:property value="#user.uname"/><br> 19 </s:if> 20 <s:elseif test="#user.id>2&&#user.id<=4"> 21 <s:property value="#user.id"/>+++<s:property value="#user.uname"/><br> 22 </s:elseif> 23 <s:else> 24 <s:property value="#user.id"/>***<s:property value="#user.uname"/><br> 25 </s:else> 26 </s:iterator> 27 28 <s:iterator begin="1" end="10" step="1" status="varStatus"> 29 <s:property value="#varStatus.index"/>--- 30 <s:property value="#varStatus.count"/>--- 31 <s:property value="#varStatus.odd"/>--- 32 <s:property value="#varStatus.even"/>--- 33 <s:property value="#varStatus.first"/>--- 34 <s:property value="#varStatus.last"/>--- 35 hello struts2<br> 36 </s:iterator> 37 <s:debug></s:debug> 38 </body> 39 </html>
最终页面上显示: