在JSP中使用Struts2标签,需要先指明标签的引入:
<%@ taglib prefix=”s” uri=”/struts-tags”%>
非UI标签、UI标签(基于表单的UI标签和其他UI标签)
1、 非UI标签
1)、if\elseif和else 执行基本的条件流转
例子:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>if</h1>
<s:set name="age" value="' 22'" />
<s:if test="#age>60">l</s:if>
<s:elseif test="#age>40">z</s:elseif>
<s:elseif test="#age>20">q</s:elseif>
<s:else>s
</s:else>
</body>
</html>
Iterator
2)、iterator
用于遍历集合(java.util.Collection)或枚举值(java.util.Iterator)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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%>">
<title>My JSP 'index.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body> <h1>iterator</h1>
<s:iterator value="{'java','html','hibernate','struts2'}" id="skill" status="status">
<s:property value="#status.odd"/><br/>
<s:if test="#status.odd">
<s:property value="skill"/><br>
</s:if>
<s:property value="#status.index"/><br/>
<!-- 页面显示用作序号 -->
<s:property value="#status.count"/><br/>
</s:iterator>
</body>
</html>
3)include
包含一个servlet的输出(servlet或jsp的页面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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%>">
<title>My JSP 'index.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>include(少用)</h1>
<s:include value="include.jsp">
<s:param name="name" value="'csdn'" />
<s:param name="age" value="22" />
</s:include>
</body>
</html>
include.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
${param.name}
<br /> ${param.age}
</body>
</html>
4).param
为其他标签提供参数,比如include标签和bean标签. 参数的name属性是可选的.
当使用param标签时, 参数的值可以通过value属性给出,也可以在标签体中给出(开始标签和结束标签之间的文本)。这二种用法有一些区别。我们看下面的例子:
<param name=“color”>blue</param> <!-- (1) -->
<param name=“color” value=“blue” /> <!-- (2) -->
在第(1)种情形中,参数值将作为java.lang.String对象(即字符串)被放入栈中;
在第(2)种情形中,参数值将作为表达式进行计算,如果blue不存在,则color参数的值为null。
在第(1)种情形中,要为color指定字符串值,可以写为:
<param name=“color” value=“ ‘ blue ‘ ”/>或者<param name=“color” value=“%{‘ blue ‘ }“/>
使用单引号包围字符串,表明这是一个字符串常量
5).set
set标签赋予变量一个特定范围内的值。当希望给一个变量赋一个复杂的表达式,每次访问该变量而不是复杂的表达式时用到。其在两种情况下非常有用: 复杂的表达式很耗时 (性能提升) 或者很难理解 (代码可读性提高)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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%>">
<title>My JSP 'index.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>set</h1>
<s:set name="xxx" value="user.name" />
<s:property value="#xxx" />
<s:property value="#request.xxx" />
<s:set name="xxx" value="user.name" scope="session" />
<s:property value="#session.xxx" />
<s:set name="xxx" value="user.name" scope="application" />
<s:property value="#application.xxx" />
</body>
</html>
6).property标签
property标签用于输出值栈中的对象的属性(property)值,使用value属性来指定要输出的对象属性,如果没有指定value属性,那么默认输出栈顶对象。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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%>">
<title>My JSP 'index.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:label value="message" />
<br />
<!-- message -->
<s:label value="'message'" />
<br />
<!--'message'-->
<s:label value="%{message}" />
<br />
<!-- Hello -->
<s:label name="message" />
<br />
<!-- Hello -->
<s:property value="message" />
<br />
<!-- Hello -->
<s:property value="'message'" />
<br />
<!-- message -->
<s:property value="%{message}" />
<br />
<!-- Hello -->
</body>
</html>
7).自定义标签
主题和模板
Struts2 的校验,错误输出是在输入框的上方,希望错误信息是显示在输入框的右方。
1. 找到你的struts2-core-2.XXX.jar这个核心包,用软件解压;
2. 在\struts2-core-2. XXX\template\simple目录下找到fielderror.ftl;
3. 上面的fielderror.ftl放到WEB-INF/classes/template/simple目录下
4. 修改fielderror.ftl配置文件,把<ul></ul><li></li>标签删除(仅仅是删除标签而不删除标签里面的内容),简便的方法就是用记事本打开这个文件并用空格替换上面的每个标签(一个一个来哦)
5. <s:fielderror cssStyle="color: red" >
<s:param>XXX (标签的 name 属性,象 user.username ) </s:param>
</s:fielderror>
自定义模板
需要提供自定义的模板文件
创建select.ftl 定义模板文件
创建theme.properties 继承现有模板
在jsp测试 引用测试
select.ftl
<H1>自定义标签</H1>
<#include "/${parameters.templateDir}/xhtml/controlheader.ftl" />
<#include "/${parameters.templateDir}/simple/select.ftl" />
<#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" />
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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%>">
<title>My JSP 'login.jsp' starting 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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="/hello/login" theme="simple">
<s:textfield name="user.name" label="用户名"/><font color="red"><s:fielderror>
<s:param>name</s:param>
</s:fielderror></font>
<s:textfield name="user.age" label="年龄"/>
<s:submit value="提交"/>
</s:form>
<s:select list="{'java','html','struts2'}" theme="csdn">
</s:select>
</body>
</html>
8).debug标签 :
debug标签主要用于辅助测试,它在页面上生成一个超链接,通过该链接可以查看ValueStack和Stack Context 中的所有值信息。
使用debug标签只有一个id属性,这个属性仅仅是该元素一个引用id。
在页面上增加<s:debug/>标签,通过debug标签,可以看的系统中ValueStack离得全部信息,并可以看到Stack Context中的属性。 示范代码:
<s:debug></s:debug>
//直接在jsp页面上面添加这样的标签,就可以生产一个查看debug信息的链接
2、 UI标签
UI标志又可以分为表单UI和非表单UI两部分。
表单标签:
两种:form标签本身和单个表单元素的标签,form标签本身的行为不同于表单元素标签
3、 Struts2标签语法
(1)所有的字符串属性类型都会解析“%{…}”这样的语法。
(2)所有的非字符属性类型都不会被解析,而是直接被看作一个OGNL表达式进行求值
(3)对于第二个规则的例外情况是,如果非字符串属性使用了“%{…}”语法,那么%{…}将被忽略,花括号中的内容将作为表达式计算