三大标签:
1.JSP:脚本,为了替代servlet,已过时
2.JSTL:标准标签库(core、format、sql、xml),还未淘汰的只有core库
3.Struts2标签库:由Struts2开发团队开发,功能更加强大
struts2标签分为:普通标签、UI标签
普通标签分:控制标签,数据标签
UI标签分:表单标签,非表单标签
准备一些数据:
List<String> list = new ArrayList<>(); list.add("tom"); list.add("jerry"); list.add("jack"); list.add("rose"); ActionContext.getContext().put("list", list);
普通标签示例:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 遍历标签 iterator --> <!-- ------------------------------------- --> <s:iterator value="#list" > <s:property /><br> </s:iterator> <!-- ------------------------------------- --><hr> <s:iterator value="#list" var="name" > <s:property value="#name" /><br> </s:iterator> <!-- ------------------------------------- --><hr> <s:iterator begin="1" end="100" step="1" > <s:property />| </s:iterator> <!-- ------------------if else elseif标签------------------- --><hr> <s:if test="#list.size()==4"> list长度为4! </s:if> <s:elseif test="#list.size()==3"> list长度为3! </s:elseif> <s:else> list长度不为3也不为4! </s:else> <!-- ------------------property 配合ognl表达式页面取值 ------------------- --><hr> <s:property value="#list.size()" /> <s:property value="#session.user.name" /> </body> </html>
表单标签(不常用):
<!-- struts2表单标签 --> <!-- 好处1: 内置了一套样式. --> <!-- 好处2: 自动回显,根据栈中的属性 --> <!-- theme:指定表单的主题 xhtml:默认 simple:没有主题 --> <s:form action="Demo3Action" namespace="/" theme="xhtml" > <s:textfield name="name" label="用户名" ></s:textfield> <s:password name="password" label="密码" ></s:password> <s:radio list="{‘男‘,‘女‘}" name="gender" label="性别" ></s:radio> <s:radio list="#{1:‘男‘,0:‘女‘}" name="gender" label="性别" ></s:radio> <s:checkboxlist list="#{2:‘抽烟‘,1:‘喝酒‘,0:‘打游戏‘}" name="habits" label="爱好" ></s:checkboxlist> <s:select list="#{2:‘大专‘,1:‘本科‘,0:‘硕士‘}" headerKey="" headerValue="---请选择---" name="edu" label="学历" > </s:select> <s:file name="photo" label="近照" ></s:file> <s:textarea name="desc" label="个人简介" ></s:textarea> <s:submit value="提交" ></s:submit> </s:form> <s:actionerror/>
Action:
package tag; import com.opensymphony.xwork2.ActionSupport; public class Demo3Action extends ActionSupport { private String name; public String execute() throws Exception { System.out.println(name); this.addActionError("错误"); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }