struts2标签
新建工程后首先在WEB-INF–>lib先导入了struts2的jar包
然后在jsp页面中引入以下指令
<%@ taglib uri="/struts-tags" prefix=“s” %>
struts标签
代码案例
a标签跳链接并且传值:
struts的通用标签
测一测4
TageAction 代码展示
public class TageAction implements ModelDriven{
private Cal cal1 = new Cal();
private Cal cal2;
private String num1;
private String result;
public String execute() {
return "demo3";
}
public Cal getCal2() {
return cal2;
}
public void setCal2(Cal cal2) {
this.cal2 = cal2;
}
public String getNum1() {
return num1;
}
public void setNum1(String num1) {
this.num1 = num1;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public Cal getModel() {
// TODO Auto-generated method stub
return cal1;
}
}
一、通用标签
1、 数据标签
<body>
<h3 style="color: red;">数据标签</h3>
<h3>set</h3>
<!-- 在ognl上下文的根对象中取result值,赋值给test1-->
<s:set var="test1" value="result"></s:set>
${test1},${requestScope.test1}<!--请求器 requestScope-->
<h3>property</h3> 【 property 用于取值,类似于c标签中的c:out标签】(注:struts标签中的value不是按字符串算,而是按表达式算))
<!-- 取栈顶 ,取cal1(model)-->
<s:property/>,
<!-- 取根对象最上面的对象属性-->
<s:property value="num1"/>,
<!-- 取根对象的cal2对象的num1属性 -->
<s:property value="#cal2.num1"/>
<!-- 取非根对象cal2对象的num1属性值 -->
<s:property value="#request.cal2.num1"/>
<h3>push</h3>
<!-- 执行push方法 就是将你需要的值放到栈顶,便于获取-->
<s:property/>
<s:push value="result">
<s:property/>
</s:push>
<s:property/>
<h3>action</h3><!--action标签用来请求后台,获取初始化数据的 -->
<!-- 用var="test2"设值都放在非根对象里面 -->
<s:action name="tageAction" namespace="/sy" var="test2"></s:action>
<s:property value="#test2.cal2"/>
<h3>url</h3>
<!-- url标签是为了生成地址所有,注意与a标签区分 不会访问后台-->
<s:url namespace="/sy" action="tageAction" value="test3"></s:url>
<s:property value="#test3"/>
<a href='<s:property value="#test3"/>'>xxx</a><!-- c标签的a标签 -->
<s:a href="%{test3}">aaa</s:a><!--strust的a标签 -->
<h3>param</h3> <!-- 作为子标签使用,方便携带参数-->
<s:url namespace="/sy" action="tageAction" var="test4">
<!-- 两种赋值方式:ognl表达式,字符串-->
<s:param name="test5">aaa</s:param><!-- 字符串 -->
<s:param name="test6" value="num1">aaa</s:param><!-- ognl表达式 -->
</s:url>
<s:property value="#test4"/>
<h3>date</h3><!--日期标签 -->
<%
request.setAttribute("currentDate", new Data());
request.setAttribute("score", new Integer(70));
request.setAttribute("names", new String[]{"男","ls","王五"});
%>
<s:date name="#request.currentDate" format="yyy-MM-dd"/>
<h3>debug</h3>
<s:debug/>
<h3 style="color: red;">控制标签</h3>
<h3>iterator/if/elseif/else</h3><!-- 迭代标签 -->
<ul>
<s:iterator var="v" value="#request.names">
<li>
<s:property value="#v"/>
</li>
</s:iterator>
</ul>
<s:if test="#request.score >80"><!-- if标签 -->
A
</s:if>
<s:elseif test="#request.socre > 60">B</s:elseif><!-- elseif标签 -->
<s:else>C</s:else><!-- else标签-->
二、 UI标签
2、 表单标签
代码展示
struts的表单标签
测一测5
<body>
crud详细讲解
<h3 style="color: red;">表单标签</h3>
<h3>form</h3>
<h3>textfield</h3>
<h3>radio</h3>
<h3>select</h3>
<h3>textarea</h3>
<h3>hidden</h3>
<h3>submit</h3>
<s:form namespace="" action="">
<s:textfield label="姓名" name="uname"></s:textfield>
<%-- <s:radio list="{‘男’,‘女’}" label=“性别” name=“sex” value="‘男’"/> --%>
<s:radio list="#{1:‘男’,2:‘女’}" label=“性别” name=“sex” value=“2”/>
<s:select label="—部门选择—" name=“deptName” list="#{1:‘财务部门’,2:‘公关部’,3:‘人事部’}"/>
<s:checkbox label=“是否已婚” name=“married”/>
<s:checkboxlist label=“爱好” list="#{1:‘吃饭’,2:‘睡觉’,3:‘大狗狗’}" name=“hobby”/>
<s:textarea label=“个人简介” name=“remark” />
<s:hidden name=“xxx” value=""/>
</s:form>
-
标签的属性类型说明
String 字符串
Boolean true|false
Object
Object/String 传过去字符串,但会被认为是一个OGNL表达式进行计算%{str}:str会被强制转换成OGNL表达式计算
<s:a href="%{#url1}">bbb</s:a> -
标签的公共属性
var 将值保存到上下文(ActionContext)中的一个key -
action/param
部门查询 /a/deptAction!list.action
deptAction
list员工资料新增
员工姓名:
所属部门:重点总结:
1、value表达式讲解(与c标签作比较)
2、set中var没有放到值栈中
3、action 再发一次请求 是为了调后台模块代码,拿数据
4、url 没有发请求,只为了生成url链接地址
%{str}:str会被强制转换成OGNL表达式计算
以上最最重要的是通过debug标签去拿值