一、struts2主题样式
就是对于struts2 ,当应用其自身的标签时,例如:
<s:form> <s:textfield name="a"></s:textfield> </s:form
当查看源代码时为:
<form> <table> <tr><td><input type="text" name=a/></td></tr> </table> </form>
这是由于struts2中加入了主题(theme)的概念。通过设置不同的主题,可以让struts的代码输出不同的html代码,你也就相应得到不同的布局效果。struts2提供了三种主题,ajax, simple, xhtml,它默认的是xhtml主题,当然你可以写任意个你自己的主题,我们称之为自定义主题。
可以通过设置解决以上问题
有两种方法可以解决.
1.简单的方法(也很实用,针对所有struts2标签)
在Struts.xml中,加上下一行代码就可以了
<constant name="struts.ui.theme" value="simple" />
代表所有的页面采用的都是 simple主题了,这时它输出的页面,不回添加任何多余的代码,比如 table tr td 等,我们就可以像其他编辑页面的方式编辑页面的风格。
2.针对某个标签
在 <s:form 中添加 theme
<s:form theme="simple"></s:form>
struts2每个标签都有theme属性.
二、struts2表达式:
注意:
struts2标签中不可以写EL的表达式,struts2使用OGNL表达式应该。
错误的写法:
<s:textfield name="user.username" label="用户名" value="${user.username}"/>
正确写法:
<s:textfield name="user.username" label="用户名" value="%{user.username}"/>
另外:
<s:textfield name="user.username" label="用户名" value="%{user.username}"/>
等同于:
<s:textfield name="user.username" label="用户名"/>
因为:
textfield中的value会自动寻找到name属性做为它的值,若为为空则value也为空。
三、关于OGNL与EL表达式
由于OGNL中有#、%和$这三个符号,同样在JSP2.1 中#也 被用作了JSP EL (表达式语言)的特殊记好,所以对OGNL 的使用可能导致问题,一个简单的方法是禁用JSP2.1 的EL 特性,这需要修改web.xml 文件:
<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-ignored>true</el-ignored> </jsp-property-group> </jsp-config>