view plaincopy to clipboardprint?
<table align="center">
<tr>
<td>
<s:textfield label="%{getText('username')}" name="username" size="20" cssClass="input01" theme="simple"/>
</td>
</tr>
<tr>
<td>
<s:textfield label="%{getText('userpass')}" name="userpass" size="20" cssClass="input01"/>
</td>
</tr>
</table>
这样的话,struts2框架会自动生成label以及一些提示信息,生成的html代码如下:
view plaincopy to clipboardprint?
<table align="center">
<tr>
<td>
<input type="text" name="username" size="20" value="" id="_username" class="input01"/>
</td>
</tr>
<tr>
<td>
<tr>
<td class="tdLabel"><label for="_userpass" class="label">密码:</label></td>
<td>
<input type="text" name="userpass" size="20" value="" id="_userpass" class="input01"/>
</td>
</tr>
</td>
</tr>
</table>
我们会发现有生成许多代价,有些时候还会生成错误提示信息的代码,可我不想要它啊,我想把它放到别的地方去,或者干脆说,我只想用Struts的action处理机制,不想用struts的默认布局,又或者你想做。。。。。。总之就不要默认的,怎么办?
这就涉及到struts2的theme(主题)了,换不同的主题,可以让struts的代码输出不同的html代码,
问题一:Struts2有哪些主题?
Struts2提供了三种主题,ajax, simple, xhtml,它默认的是xhtml主题,当然你可以写任意个你自己的主题,我们称之为自定义主题
问题二:Struts2怎么改?
比如现在你想用simple主题,在Struts.xml中,加上下一行代码就可以了
<constant name="struts.ui.theme" value="simple" />
或者:在你的struts.propertise内加入以下:
struts.ui.theme=simple
struts2—from标签中 theme="simple" 用法
在struts-core-2.06.jar中包含了一些默认的模版文件($ {struts-core-2.06.jar}/template),其中有ajax/simple/xhtml等.
查看struts.properties或$ {struts-core-2.06.jar}/org/apache/struts2/default.properties文件,其中有如下配置:
struts.ui.theme=xhtml
struts.ui.templateDir=template
struts.ui.templateSuffix=ftl
这一段就是关于struts2模版的配置信息,我们可以修改struts.properties文件,将其改成
struts.ui.theme=simple
struts.ui.templateDir=template
struts.ui.templateSuffix=ftl
如果没有struts.properties文件,可以修改struts.xml文件,在其中加入如下行
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.ui.templateDir" value="template" />
<constant name="struts.ui.templateSuffix" value="ftl" />
---
demo:
----
<s:form action="Login">
<table align="center">
<tr>
<td> <s:textfield name="username" label="名 称"/> </td>
</tr>
<tr>
<td> <s:textfield name="password" label="密 码"/> </td>
</tr>
<tr>
<td>
<s:submit value="登录" /> <s:reset value="重填" />
</td>
</tr>
</table>
</s:form>
在页面上显示就变成两行显示,而且右对齐,难道默认就是这样?
解释:仔细看你生成的html代码,会发现多了很多多余的tr,td等,因为struts2表单标签本来就会自动生成的
你又再次添加,就多余了,
下面这段代码可行的:
HTML code
<s:form action="Login">
<table align="center">
<s:textfield name="username" label="名 称"/>
<s:textfield name="password" label="密 码"/>
<tr>
<td colspan="2" align="center">
<s:submit value="login" theme="simple" />
<s:reset value="reset" theme = "simple"/>
</td>
</tr>
</table>
</s:form>