liuquanqi

java

常用链接

统计

最新评论

webwork 中文乱码问题 表单字段验证 标签的用法

1.WebWork中文问题解决
a)在webwork.properties文件中,添加: 
webwork.i18n.encoding = UTF-8 
它主要是用来设置WebWork UI标签库的编码, 
要保持你的页面和编码与你在encoding设置的一致,否则重复编码出现乱码. 
如果不设置它将通过System.getProperty("file.encoding"来获取默认字符编码。
 
PS:2.1.6版本中存在BUG,不能够使用定义的encoding. 

b)写一个Filter,将编码设置为UTF-8。 
例如SetCharacterEncodingFilter。它解决Action数据传递时的编码,但是不能解决属性在Action或是Model赋值的编码. 
c)velocity.properties文件中,添加: 
input.encoding=UTF-8 
output.encoding=UTF-8 
default.contentType=text/html; charset=UTF-8 
它是用来设置.vm页面的编码方式. 

d)使用freemarker作为view层,可以再web.xml中指定字符属性;

<servlet>
        <servlet-name>freemarker</servlet-name>
        <servlet-class>com.opensymphony.webwork.views.freemarker.FreemarkerServlet</servlet-class>
        <!-- FreemarkerServlet settings: -->
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>/templates/</param-value>
        </init-param>
        <init-param>
            <param-name>NoCache</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>ContentType</param-name>
            <param-value>text/html</param-value>
        </init-param>
       <!--        指定编码         -->
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>

        <init-param>
            <param-name>number_format</param-name>
            <param-value>0.##########</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

2.WebWork表单字段验证
a)Action必须继承ActionSurport; 
b)在xwork.xml文件必须定义 
<action name="hello" class="test.HelloAction"> 
   <result name="success">/result.jsp</result>    
   <result name="input"> 
        <param value="/location">/index.jsp</param> 
   </result>   
   <interceptor-ref name="validationWorkflowStack"/> 
</action> 
其中input 是必须定义的,另外要使用 
<param value="location">/index.jsp</param> 
才可以在验证错误后回到输入页面。
 
c)在Action类名-validation.xml文件必须与Action类在同一目录下
d)定义的<message/>元素,当提示消息为中文时候必须在文件头定义: 
<?xml version="1.0" encoding="GBK"?> 

3.WebWork的标签的用法
a)需要提交的表单项name属性值均为常量,因此均以单引号包围,以与Model中的变量相匹配,

<ww:form namespace="'/test'" action="'ftltest'" method="'POST'">
               <ww:textfield name="'msg'" label="消息"></ww:textfield>
               <ww:submit value="/提交" />
    </ww:form>
b)<ww:radio/>和<ww:select/>标签中的list的属性可以使用Map类型,以key和value对应listKey,listValue属性.

第一种写法:

<ww:select label="'Months'" 
        name="'months'" 
            list="#{'01':'Jan', '02':'Feb', []}" 
        value="/01"
        required="true" 
/>

第二种写法:

<ww:select label="'Pets'" 
        name="'petIds'" 
        list="petDao.pets" 
        listKey="id" 
        listValue="name" 
        multiple="true" 
        size="3" 
        required="true" 
/>

<!--petDao.pets为普通对象,使用其id为KEY,name为value-->
<!--如果petDao.pets为Map类型,则可省略listKey和listValue属性-->

c)WebWork2中的UI标签的模板定制,可以在webwork.properties文件中指定相关属性,并且定制模板文件;

webwork.ui.theme=xhtml     //可用的theme还有simple;
webwork.ui.templateDir=template    //模板路径;
#sets the default template type. Either vm or jsp
webwork.ui.templateSuffix=vm   //模板类型,还支持JSP模板,开发可以使用FreeMarker定制模板;


4.WebWork的国际化使用: 
a)<ww:i18 name="'message'"> 
        <ww:text name="'key'"/> 
  </ww:i18> 
使用message_zh_CN.properties中包含key=****; 

b)在Action中的getText()方法资源来自Action的类名.properties

c)在Action中可以使用getTexts()获得具体的ResourceBundle;

d)在view层一样可以使用getText()方法;


        <ww:i18n name="'messages'">
            <!--在messages.properties文件中有key->index_msg-->        
                <ww property value="getText('index_msg')"/>
        </ww:i18n>

posted on 2007-03-19 11:18 ☆℡齐 阅读(2173) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航: