当前位置: 代码迷 >> Java Web开发 >> 50问struts2.0(webwork2.2)的几个问题
  详细解决方案

50问struts2.0(webwork2.2)的几个问题

热度:436   发布时间:2016-04-17 15:13:21.0
50问struts2.0(webwork2.2)的几个小问题
最近在看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 ",验证就可以通过。
但这时出问题了,我的表单无法提交了,因为取不到表单元素属性的值。全部是空值。



------解决方案--------------------
过滤器:

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 ");
  相关解决方案