struts标签

struts2标签

终于把struts2标签看的差不多了,不过还有好多还不是很熟悉,我是结合Max的struts2教程和struts自带的reference文档学习的!笔记中有好多都是从Max的博客中搬来的。不过也没有办法,咱水平还不行,也只能站在人家的肩膀上学习一下了!!

if,elseif, else标签

?<%@ 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 'tags.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">
</head>

<body>
<!--
小技巧:#parameters.country[1]可以这样取值!原来Struts2是将URL传的参数封装 成一个数组的,
也就是说你可以在一个URL传两个同名的参数(即?name=a&name=b);
-->
<%request.setCharacterEncoding("utf-8");%>
(request获取方式)country=<%=request.getParameter("country") %><br>
<s:if test="#parameters.country[1] == 'www'">中国</s:if>
<s:elseif test="#parameters.country[0] == '美国'">美国</s:elseif>
<s:else >其他国籍的人</s:else>
<br>

<!-- 为某个属性赋值 -->
<s:set name="country" value="#parameters.country[1]"></s:set>
country=<s:property value="#country" /> <br>
<s:if test="#country == 'www'">中国</s:if>
<s:elseif test="#country == '美国'">美国</s:elseif>
<s:else >其他国籍的人</s:else>
<br>
</body>
</html>

  

这里面有个小常识,就是通过url传递参数的时候:Struts2是将URL传的同名参数封装成一个数组,也就是说我们可以在一个URL传两个同名的参数(即?name=a&name=b);获取的时候直接#parameters.name[0], parameters.name[1]就可以分别取出两个参数的值。

include标签

  被包含页面:

?<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
this is the include page!!!

  包含页面:

?<%@ 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 'includea.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" mce_href="styles.css">
-->
</head>
<body>
This is my JSP1 page.
<br>
<s:include value="include.jsp">
<!-- 提交表单时才会获得value的值 -->
<s:param name="value1">test1</s:param>
<s:param name="value2" value="user"></s:param>
</s:include>
</body>
</html>

当然struts的include标签静态动态页面都能包含的!

i18n标签
描述:加载资源包到值堆栈。它可以允许text标志访问任何资源包的信息,而不只当前action相关联的资源包。

?<%@ 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 'tags3.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" mce_href="styles.css">
-->
</head>

<body>
<!-- <s:text>标签没弄明白 -->
<s:i18n name="I18N">
The i18n value is <s:text name="hi!!"></s:text>
</s:i18n>
<br>
<s:debug></s:debug>
</body>
</html>

  

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 'tags2.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" mce_href="styles.css">
-->
</head>
<%
List<String> strs = new ArrayList<String>();
strs.add("a");
strs.add("b");
strs.add("c");
strs.add("d");
strs.add("e");
request.setAttribute("strs", strs);
%>
<body>
<s:iterator value="#request.strs" var="strs">
<s:property value="#strs"/>
</s:iterator>
<s:debug></s:debug>
</body>
</html>

  

这个标签挺容易理解的。

param标签

struts2的<s: param>标签问我觉得比较复杂的。struts2的s:param标签主要有两个属性name与value, 若想在value属性中输入直接量,则可以这样写:<s:param name="some" value="%{'user'}"/>, 也可以这样写:<s:param name="some">user</s:param>。 但如果直接赋值,这个值不是由Action动态生成的,而是自己指定的一个字符串,则只能用后者。

param页面代码:

?<%@ 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 'param.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" mce_href="styles.css">
-->
</head>

<body>
<!-- struts2的s:param标签主要有两个属性name与value,
若想在value属性中输入直接量,则可以这样写:<s:param name="some" value="%{'user'}"/>,
也可以这样写:<s:param name="some">user</s:param>。
但如果直接赋值,这个值不是由Action动态生成的,而是自己指定的一个字符串,则只能用后者。 -->
<s:url value="paramAction.jsp" id="href">
<s:param name="value1">hello!!</s:param>
<s:param name="valu2" value="%{'HELLO!'}"></s:param>
</s:url>
<s:a href="%{href}" mce_href="%{href}">paramAction</s:a>
<s:debug></s:debug>
</body>
</html>

  

paramAction页面:

?<%@ 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 'paramAction.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" mce_href="styles.css">
-->
</head>

<body>
value1的值:<%=request.getParameter("value1") %><br>
value1的值:<s:property value="#parameters.value1" /><br>
value2的值:<%=request.getParameter("value2") %><br/><!-- 获取不到值 -->
value2的值:${#param.value2 }<!-- 获取不到值 -->
<s:debug></s:debug>
</body>
</html>

  

经常用到的UI标签

?<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<html>
<head>
<title>UI Tags Example</title>
<s:head />
<sx:head parseContent="true"/>
</head>
<body>
<s:actionerror/>
<s:actionmessage/>
<s:fielderror />
<s:form action="exampleSubmit" method="post" enctype="multipart/form-data" tooltipConfig="#{'jsTooltipEnabled':'true'}">
<s:textfield
label="Name"
name="name"
tooltip="Enter your Name here" />
<s:date name="Select Your Birthday"/>
<!-- 在struts2.2.1中<sx:datetimepicker/>的标签使用有所变化,需要引入struts2-dojo-plugin-2.2.1.jar
这个包。
-->
<sx:datetimepicker
tooltip="Select Your Birthday"
label="Birthday"
name="birthday" />
<s:textarea
tooltip="Enter your Biography"
label="Biograph"
name="bio"
cols="20"
rows="3"/>
<s:select
tooltip="Choose Your Favourite Color"
label="Favorite Color"
list="{'Red', 'Blue', 'Green'}"
name="favoriteColor"
emptyOption="true"
headerKey="None"
headerValue="None"/>
<s:select
tooltip="Choose Your Favourite Language"
label="Favourite Language"
list="#{'CN':'中文','EN':'英文','FR':'外文'}"
name="favouriteLanguage"
emptyOption="true"
headerKey="None"
headerValue="None"/>
<s:checkboxlist
tooltip="Choose your Friends"
label="Friends"
list="{'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}"
name="friends"/>
<s:checkbox
tooltip="Confirmed that your are Over 18"
label="Age 18+"
name="legalAge"/>
<s:doubleselect
tooltip="Choose Your State"
label="State"
name="region" list="{'North', 'South'}"
value="'South'"
doubleValue="'Florida'"
doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}"
doubleName="state"
headerKey="-1"
headerValue="---------- Please Select ----------"
emptyOption="true" />
<s:file
tooltip="Upload Your Picture"
label="Picture"
name="picture" />

<s:optiontransferselect
tooltip="Select Your Favourite Cartoon Characters"
label="Favourite Cartoons Characters"
name="leftSideCartoonCharacters"
leftTitle="Left Title"
rightTitle="Right Title"
list="{'Popeye', 'He-Man', 'Spiderman'}"
multiple="true"
headerKey="headerKey"
headerValue="--- Please Select ---"
emptyOption="true"
doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}"
doubleName="rightSideCartoonCharacters"
doubleHeaderKey="doubleHeaderKey"
doubleHeaderValue="--- Please Select ---"
doubleEmptyOption="true"
doubleMultiple="true" />

<s:textarea
label="Your Thougths"
name="thoughts"
tooltip="Enter your thoughts here" />

<s:submit οnclick="alert('aaaa');" />
<s:reset οnclick="alert('bbbb');" />
</s:form>

</body>
</html>

  

这些代码是直接从Max的博文中copy过来的,本来这是一个完整的例子我只是把前台展示代码考过来了。在这里有一个要注意的地方,不过在代码中也有体现,这是struts2比较高级的版本和低版本之间的区别,具体从哪个 版本改的我也不是很清楚。这个不同的用法是关于<s: datetimepicker>的。在使用<s: datetimepicker>时,较高的版本中还必须引入struts2-dojo-plugin-2.2.1.jar,还得指明<%@ taglib prefix="sx" uri="/struts-dojo-tags" %> ,如果做实验不成功的可以考虑下是不是这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值