struts2教程--标签库详解

时间:2021-08-27 04:42:36

struts2 标签库

tag-reference.html 就是 struts2标签规范

一、 通用标签库 的学习

<s:property> 解析ognl表达式,设置默认值,设置内容是否HTML转义


<s:set> 向四个数据范围保存数据


<s:iterator> 遍历值栈中数据


<s:if> <s:elseif> <s:else> 进行条件判断 -------- elseif可以有多个


<s:url> 进行URL重写(追踪Session) ,结合s:param进行参数编码


<s:url action="download" namespace="/" var="myurl">

     <s:param name="filename" value="%{'MIME协议简介.txt'}"></s:param>

</s:url>


<s:property value="#myurl"/>


<s:a> 对一个链接 进行参数编码


<s:a action="download" namespace="/" >下载MIME协议简介.txt

   <s:param name="filename" value="%{'MIME协议简介.txt'}"></s:param>

</s:a>

 

OGNL 了解部分 : 支持赋值操作和表达式串联 、 操作集合对象

 1) 在值栈中保存一个对象

        <s:property value="price=1000,name='冰箱',getPrice()"/>  自动查找值栈中pricename属性 为其赋值  

 2ognl操作集合

        <s:property value="products[0].name"/> 访问集合第一个元素name属性

        <s:property value="map['name']"/> 访问mapkeyname的值


{} 直接构造List元素、#{}直接构造Map元素

<s:iterator value="{'aaa','bbb'}" var="s">

     <s:property value="#s"/>

</s:iterator>


<s:iterator value="#{'ccc':'111','ddd':'222' }" var="entry">

     <s:property value="#entry.key"/>

</s:iterator>

    

UI标签库的学习 (Form标签)

    使用struts2 form标签 好处 : 支持数据回显 , 布局排班(基于Freemarker模板定义 )


1、struts2 表单标签 value属性。 必须写%{}进行设值

使用struts2表单标签前, 必须配置StrutsPrepareAndExecuteFilter

  The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag

  

<s:form> 表单标签

<s:form action="regist" namespace="/" method="post" theme="xhtml">  ---   theme="xhtml"默认布局样式

<s:textfield> 生成 <input type="text" >

<s:password > 生成 <input type="password" >

<s:submit type="submit" value="注册"/>生成<input type="submit" >

<s:reset type="reset" value="重置" />生成 <input type="reset" >

<s:textarea> 生成 <textarea>多行文本框

<s:checkboxlist> 生成一组checkbox


2、使用ognl构造Map(看到值和提交值 不同时)

    <s:checkboxlist list="#{'sport':'体育','read':'读书','music':'音乐' }" name="hobby"></s:checkboxlist>

    <s:radio> 生成一组radio

3、使用 ognl构造List  (看到内容和提交值 相同时)

   <s:radio list="{'',''}" name="gender"></s:radio>

   <s:select> 生成一个<select>

   <s:select list="{'北京','上海','南京','广州'}" name="city"></s:select>

4、struts2 开发 密码框 默认不回显

      <s:password name="password" id="password" showPassword="true"/>

 

5、 页面元素主题设置

    default.properties  ----  struts.ui.theme=xhtml 设置struts2 页面元素使用默认主题

                          struts.ui.templateSuffix=ftl 默认模板引擎 Freemarker

修改主题

    方式一 <s:textfield name="username"  label="用户名“theme="simple"></s:textfield>只对当前元素有效

  方式二 <s:form  action="" method="post" namespace="/ui“    theme="simple">form中所有元素有效

   方式三 struts.xml

     <constant name="struts.ui.theme" value="simple"></constant>  修改默认主题样式,页面所有元素都有效

     优先级 方式一 >方式二  >方式三

 

三、防止表单重复提交原理

表单防止重复提交

表单重复提交 危害:刷票、重复注册、带来服务器访问压力(拒绝服务)

 

1、 在jsp通过<s:token />生成令牌号

生成表单隐藏域

将令牌号保存到Session

 

2、 通过struts2提供tokenIntercetor拦截器 完成请求中令牌号 和session中令牌号 比较

<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>

<action name="token" class="com.sihai.struts2.TokenAction">

<result>/index.jsp</result>

<!-- 重新定义拦截器 -->

<interceptor-ref name="defaultStack"></interceptor-ref>

<interceptor-ref name="token"></interceptor-ref>

</action>


 

3、 当表单重复提交时,token拦截器自动跳转result name="invalid.token"

 通过 <s:actionError/> 显示错误信息

 

覆盖重复提交信息  struts.messages.invalid.token=您已经重复提交表单,请刷新后重试

 

 

 

四、Struts2 内置json插件

知识点 struts2Ajax开发

Ajax开发客户端 和 服务器交互数据格式  --------------- json

   json 是最轻量级,体积最小

 

服务器将程序处理结果,转换为json格式发送给 客户端

   json-lib flexjson  工具类库

 

  struts2-json-plugin-2.3.7.jar

 

案例一: 输入用户名,鼠标点击密码(触发用户名元素离焦事件),使用Ajax 将用户名发送到服务器 判断是否存在

jquery 1.4 1.6新特性比较多  (企业主流1.4

 

使用struts2 json插件

   要点1 <package> extends 继承json-default

   要点2 <result> type 类型写json  

 

 struts2 json插件 ,默认将值栈root顶端对象 所有属性返回(get方法)

 

不想将company属性返回 ,在get方法上@JSON(serialize=false)

 

案例二 :服务器将商品对象 List列表返回

如果Action 实现ModelDrivenmodel对象就是值栈栈顶对象,struts2 json插件默认 将model返回

 

通过设置root属性,修改插件返回 根对象

   * <param name="root">action</param> Action作为根对象返回

 

只想要每个商品的 name 属性

   方案一: pnumpriceget方法上 添加@JSON(serialize=false)  =========只要@JSON注解,属性将永远不能参与json返回

   方案二: 设置 includeProperties属性

 <param name="includeProperties">products\[\d+\]\.name</param>