最近在看struts2.0,这个基本就是webwork2.2。以前没有弄过webwork,所以有几个小问题想问问?
1、表单提交中文问题:
如果是<s:form表单提交,中文无法过滤,出现都是?号,我看了网上的文章试着把过滤器放在strust2.0 FilterDispatcher的前面,结果还是一样。2、表单验证问题。struts2.0的表单验证比较简单,只要写个xxx-validation.xml就可以了。我写的ProductsAction-save-validation.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
    <!-- Field-Validator Syntax -->
    <field name="product.gameNameCn">
        <field-validator type="requiredstring">
            <message key="product.gameNameCn.required"/>
        </field-validator>
    </field>
    <field name="product.gameNameEn">
        <field-validator type="requiredstring">
            <message key="product.gameNameEn.required"/>
        </field-validator>
    </field>
    <field name="product.gameCapacity">
        <field-validator type="requiredstring">
            <message key="product.gameCapacity.required"/>
        </field-validator>
    </field>
</validators>然后在struts_products.xml配置加上<interceptor-ref name="validation"/>。然后在s:form表单加上validate="true",验证就可以通过。
但这时出问题了,我的表单无法提交了,因为取不到表单元素属性的值。全部是空值。

解决方案 »

  1.   

    另外关于strust2.0的中文问题还要补充一句,如果是url传值,没有中文问题。只有表单提交就会出现中文问题,我的服务器是tomcat5.5
      

  2.   

    关注,我也正在学struts2 但是网上关于struts2的资料太少了,楼主可否发一份struts2的资料给我,万分感激!~
    E-mail:[email protected]
      

  3.   

    过滤器:public class chineseFilter implements Filter { 
    protected String encoding = null; 
    protected FilterConfig filterConfig = null; 
    protected boolean ignore = true; 
    public void destroy() { this.encoding = null; this.filterConfig = null; } 
    public void doFilter(ServletRequest request, ServletResponse response, 
    FilterChain chain) throws IOException, 
    ServletException { 
    // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) { request.setCharacterEncoding(encoding); 
    response.setCharacterEncoding("gb2312");
    } } // Pass control on to the next filter chain.doFilter(request, response); 

    public void init(FilterConfig filterConfig) throws ServletException { 
    this.filterConfig = filterConfig; 
    this.encoding = filterConfig.getInitParameter("encoding"); 
    String value = filterConfig.getInitParameter("ignore"); 
    if (value == null) { 
    this.ignore = true; 
    } else if (value.equalsIgnoreCase("true")) { 
    this.ignore = true; 
    } else if (value.equalsIgnoreCase("yes")) { 
    this.ignore = true; 
    } else { 
    this.ignore = false;
    } } protected String selectEncoding(ServletRequest request) { return (this.encoding); } }
      

  4.   

    第一个问题,Encode的Filter在Action的Filter之前肯定是可以的。
    第二个,似乎少了interceptor。
      

  5.   

    laoer(laoer.com):
    现在验证问题解决了,确实是少了一个interceptor。加一个<interceptor-ref name="params"/>就可以了。但现在中文问题还是没有解决。
      

  6.   

    laoer(laoer.com),我没猜错的话,应该是天乙论坛的作者。
      

  7.   

    看调试,提交表单的时候,调用了过滤器的request.setCharacterEncoding(encoding);和chain.doFilter(request, response);,而且encoding也是"gb2312"。但为什么一提交,到action中却是乱码了?
      

  8.   

    结贴,中文问题解决。在filter中<param-value>UTF-8</param-value>需要写utf-8,如果写gb2312就会是乱码。