struts(tags)

Struts2-tags

通用标签:

property

默认的value 的类型为object,只要是object  类型都会被解析成ognl表达式,但是如果只想传递一个普通的字符串需要加‘‘ ;example

<s:property value="username" />会被解析成ognl表达式

<s:proerty value="‘username‘" />此时的username就是一个普通的字符串

设定默认值:

<s:property value="admin" default="管理员" />当值栈中没有 admin 属性时,取不到值,就采用默认值,如果值栈中有 admin对应的值,则采用取到的值

设定html :

<s:property value="‘</hr>‘" escape="false" />默认的escape的值为 true,为 true 时struts2会把字符串解析成普通的字符串,为假时Struts2 会把它解析成html标签

set

value的类型为object

name和id均以废弃统一使用var代替

var定义一个变量,scope指定变量的范围(可取:page,session,application,request,action)

value指定变量的值, 在没有指定scope的情况下:默认的范围为request和ActionContext

<s:set var="name" value="username" />存储在request和ActionContext中,name  的值就是 username 中保存的值

从request 中取出:<s:property value="#request.name" />

从 ActionContext中取值 :<s:property var="#name" />

设定范围:<s:set var="pass" value="password" scope="page" />

从相应范围取值:<%=pageContext.getAttribute("pass")%>

bean

生成一个对象:

<s:bean name="类路径" var="对象名">

<s:param name="属性名" value="值" ></s:param>

</s:bean>

当未给对象起名时: 生成的对象会放在值栈区,访问对象的属性可以用访问值栈的方式访问:<s:property value="属性名" />

但是不可以在bean标签外访问该对象,当bean标签结束后,对象就被移除了值栈区

为对象起名后,对象就会放在stackContext中,可以在标签外访问该对象<s:property value="#对象.属性" />

include

value的标签为String 类型

include将一个 html页面包含到当前页面

<s:include value="html的url" />

当value 的值存的是string类型的, ${}可以将其强制转换成ognl表达式

不推荐用,中文经常出现乱码

param

debug

控制标签

if elseif else

ifelse控制标签使用test获取参数的值,取法和property标签相同

<s:if test="#name==null">请填写姓名</s:if>

<s:set var="len" value="name.length()" />

<s:if test="#len<0">wrong</s:if>

<s:elseif test="#len>10">to long</s:elseif>

<s:else >OK</s:else>

iterator

遍历集合:

<s:iterator value="{1,2,3,4}">  定义一个集合

<s:property />   通过property遍历集合

</s:iterator>

自定义变量遍历集合

<s:iterator value="{‘aaa‘,‘bbb‘,‘ccc‘}" var="x">   定义的变量x相当于for each中的循环变量,就是集合中元素

<s:property value="#x.toUpperCase()" />  获取集合中的元素

</s:iterator>

使用status: status记录着集合遍历过程的状态

<s:iterator value="{‘aaa‘,‘bbb,‘ccc‘}" status="status">

<s:property />

<s:property value="#status.count" />

<s:property value="#status.index" />

<s:property value="#status.odd" />判断是否是奇数

<s:property value="#status.even" />判断是否是偶数

<s:property value="#status.first" />判断是否是第一元素

<s:property value="#status.last" />

</s:iterator>

遍历map集合

<s:iterator value="#{1:‘a‘,2:‘b‘,3:‘c‘}">定义map 集合必须加 #

<s:property value="key" /> 获取map集合的key

<s:property value="value" />获取map集合的value

</s:iterator>

自定以变量遍历map 集合

<s:iterator value="#{1:‘a‘,2:‘b‘,3:‘c‘}" var="x">

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

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

</s:iterator>

subset

ui

声明式异常处理:

如果希望struts处理异常,在后台的代码中,需将异常显示的抛出,

在对应的struts.xml 配置异常的捕捉:

第一种方式在每个action中配置自己的异常处理:

<exception-mapping result="result名" exception="Java异常包" />

<result name="">/xx.jsp</result>

第二种统一配置处理:

将异常处理放在一个包中, 其它包只要继承该包即可使用

example:

<package name="xxxx" extends="struts-default">

<global-results>

<result name="xxx">/xx.jsp</result>

</global-results>

<global-exception-mappings>

<exception-mapping result="result名" exception="Java异常包" />

</global-exception-mappings>

</package>

I18n国际标准:(很少用,用到时查文档)

拦截器:

时间: 2024-08-03 19:22:33

struts(tags)的相关文章

This is usually caused by using Struts tags without the associated filter.

1.错误描述 严重: Servlet.service() for servlet [jsp] in context with path [/SendMail] threw exception [An exception occurred processing JSP page /pages/fileupload/fileUpload.jsp at line 42 39: <table> 40: <tr> 41: <td> 42: <s:fielderror css

The struts dispatcher cannot be found

1.错误描述 严重:Servlet.service() for servlet jsp threw exception The struts dispatcher cannot be found.This is usually called by using struts tags without the associated filters.Struts tags are only usable when the request has passed through its servlet f

struts的异常处理

异常信息: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

struts2错误:The Struts dispatcher cannot be found.

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 t

The Struts dispatcher cannot be found. This is usually caused by using Struts

对于struts2中的问题: org.apache.jasper.JasperException: 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, wh

The Struts dispatcher cannot be found异常的解决方法

系统错误:HTTP Status 500 异常信息:The Struts dispatcher cannot be found.  This is usually caused by using Struts tagswithout the associated filter. Struts tags are only usable when the request haspassed through its servlet filter, which initializes the Strut

struts2学习笔记--使用Validator校验数据

我们在进行一些操作是需要对用户的输入数据进行验证,比如网站的注册,需要对各个数据项进行数据校验,Struts2提供了一些默认的校验器,比如数字的检测,邮箱的检测,字符串长度的检测等等. 常用的Validator 校验器 作用  required  必填校验器,要求字段必须有值  requiredstring 必填字符串校验器,要求必须有值且长度大于0,即不能是空字符串.默认会去掉字符串前后空格 参数fieldName:该参数指定校验的字段名称,如果是字段校验,则不用指定该参数 参数trim:该参

Struts2学习之文件上传/下载&amp;ValueStack(三)

简介 今天是学习Struts2第三天,也算struts2准备前奏告一段落,提升部分只能在后期深入了解,多看源码,多参阅资料.今天所学的知识点文件上传.下载/ValueStack&OGNL/Struts2标签 文件上传/下载 文件上传简介 a). 企业常用文件上传技术 : jspSmartUpload(主要应用 JSP model1 时代) . fileupload (Apache commons项目中一个组件). Servlet3.0 集成文件上传 Part类 b). Struts2 对文件上传

struts2国际化全例 错误解决

在struts2中需要做国际化的有: jsp页面的国际化,action错误信息的国际化,转换错误信息的国际化,校验错误信息的国际化 在之前的例子中已经做过和国际化相关的例子了,在struts.xml中配置过 view plaincopy to clipboardprint? <constant name="struts.custom.i18n.resources" value="message"></constant> 其中的message就