URL标签。
典型用法:
<a href="<s:url action='hello'/>">Hello World</a>
该链接会直接交给一个叫做hello的action处理。
带参数的用法:
TextField标签
<s:textfield>
典型用法:
通过资源文件读取:
<s:textfield key="personBean.firstName" />
对应资源文件如下:
personBean.firstName=First name
struts会把资源文件当中的key作为textfield的name属性的值,把value作为textfield的text值
struts2中资源文件的加载
value就是资源文件的名字。
可以用形如
<s:text name="contact" />
的方式去访问资源文件中定义的键值对。
还有一个,就是包级别的资源文件必须定义为Package.properties。名字不能更改。
struts实现国际化也是基于资源文件。例如,我们现在用的是简体中文环境,那么struts会首先搜索myResource_zh_CN.properties找不到然后搜索myResource_zh.properties,然后再找myResource.properties。
在页面中引用国际化资源的方法:
用 1.<s:text name="propertiesName"/> 2.直接使用key属性 <s:textfield key="personBean.firstName" /> 3.用ognl表达式<s:textfield name="age" label="%{getText('age')}"/>
Struts 2 will look for a property key of contact in all the property files starting with the property file that matches the Action class, then in the property files that are in the package hierarchy of the Action class, and then in any property files specified in struts.xml.
上面是struts2.2.1官方文档中的一段话。意思就是说,struts首先会去和被访问action相同的包底下去找资源文件,而后在去他的上一级包里找,最后去struts.xml里找全局properties文件,再找不到就没有了。
所以全局资源文件需要在struts.xml中声明的。具体声明的方法为:
<constant name="struts.custom.i18n.resources" value="global" />
<s:textfield name="personBean.firstName" label="First name" />
<s:url action="hello" var="helloLink">
<s:param name="userName">Bruce Phillips</s:param>
</s:url>
<p><a href="${helloLink}">Hello Bruce Phillips</a></p>