最近在看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、表单提交中文问题:
如果是<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",验证就可以通过。
但这时出问题了,我的表单无法提交了,因为取不到表单元素属性的值。全部是空值。
解决方案 »
- jsp cookies 值取不到?
- JSP 消息提示问题
- [不知道是否该发这里] DIV+CSS 不识别iterator
- 【求助】axis2 怎样获取航班信息?
- 求poolman2.0.4。有的话发给我行吗。我的email是[email protected]
- 关于Tomcat的网站路径问题
- servlet编译后的CLASS文件是和bean的放在一起??
- 高分求巨难菜单源码!!!
- web页面传参数,防止重复插入数据的问题
- struts2抛的异常:java.lang.NoSuchFieldException: resourceEntries
- 在action中如何得到pageContext中的对象
- 超级无敌 Applet菜鸟问题
E-mail:[email protected]
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); } }
第二个,似乎少了interceptor。
现在验证问题解决了,确实是少了一个interceptor。加一个<interceptor-ref name="params"/>就可以了。但现在中文问题还是没有解决。