web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.multipart.maxSize" value="20971520000"></constant>
<package name="default" namespace="/" extends="json-default">
<action name="testAction" class="com.huawei.s2.action.TestAction" method="queryList">
<result name="success">/s_action.jsp</result>
</action>
</package>
</struts>
action:
package com.huawei.s2.action;
import java.util.ArrayList;
import java.util.List;
public class TestAction {
private List<String> list;
public String queryList(){
list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
return "success";
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
po:
package com.huawei.s2.po;
public class Person {
private Integer pid;
private String pname;
public Person(Integer pid, String pname) {
super();
this.pid = pid;
this.pname = pname;
}
public Person() {
super();
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
jsp:
s_set.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>This is my JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:set var="s" value="{00,11,22,33,44,55,66,77,88,99}" ></s:set>
<s:set var="testMap" value="#{'uname':'aaa','age':12}" ></s:set>
<s:set var="s2" value="'aaa'" scope="request"></s:set>
<hr/>
<s:property value="#s" />
<hr/>
<s:property value="#testMap" />
<hr/>
<s:property value="#request.s2" />
</body>
</html>
s_action.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>This is my JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:action var="a" name="testAction" namespace="/" executeResult="false"/>
<s:iterator value="#a.list" var="s">
<s:property value="s"/>
</s:iterator>
</body>
</html>
s_include.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<h1>此乃s_include.jsp</h1>
<s:include value="s_date.jsp">
<s:param name="aaa" value="zhangsan"/>
</s:include>
s_date.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
request.setAttribute("d", new Date());
%>
<s:date name="#request.d" format="yyyy-MM-dd" nice="true"/><!-- 增加nice,format属性不起作用 -->
<hr/>
<s:date name="#request.d" format="yyyy-MM-dd" var="c"/><!--使用var,时间不会打印出,而是保存在c变量中 -->
<hr/>
<s:property value="#c"/>
<hr/>
${param.aaa }
s_if.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
int num1=(int)(Math.random()*6)+1;
int num2=(int)(Math.random()*6)+1;
int num3=(int)(Math.random()*6)+1;
int sum=num1+num2+num3;
request.setAttribute("sum", sum);
%>
<s:if test="#request.sum>=12">
运气不错====><s:property value="#request.sum"/>
</s:if>
<s:elseif test="#request.sum>=8">
运气一般====><s:property value="#request.sum"/>
</s:elseif>
<s:else>
运气很差====><s:property value="#request.sum"/>
</s:else>
s_iterator.jsp:
<%@page import="com.huawei.s2.po.Person"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
List<String> list = new ArrayList<String>();
for(int i=1;i<=20;i++){
list.add("aaa==="+i);
}
request.setAttribute("list", list);
List<Person> personList = new ArrayList<Person>();
personList.add(new Person(1,"zhangsan"));
personList.add(new Person(2,"lisi"));
request.setAttribute("personList", personList);
%>
<!--begin end 指的的下标
status: 状态类变量 记录了结果的索引 或者 计数等信息 常用方法如下:
int getCount() 返回迭代次数
int getIndex() 返回当前迭代的索引值
boolean isEven() 当前迭代的索引值是否偶数
boolean isFirst() 当前迭代的索引值是否第一个元素
boolean isLast() 当前迭代的索引值是否最后一个元素
boolean isOdd() 当前迭代的索引值是否奇数 -->
<s:iterator value="#request.list" var="str" begin="5" end="15" step="2" status="indx">
<s:property value="str"/>=====><s:property value="#indx.count"/>====><s:property value="#indx.index"/><hr>
</s:iterator>
<s:iterator value="#request.personList" var="person">
<s:property value="#person.pname"/>
<hr>
</s:iterator>
s_form.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>This is my JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="test" name="regFrm">
<s:textfield lable="用户名" name="uname"></s:textfield>
<s:password lable="密码" name="pwd"></s:password>
<s:textarea lable="邮箱" name="email"></s:textarea>
<s:submit value="注册"></s:submit>
</s:form>
</body>
</html>