2007-08-21
T5 技巧 1:解决Form的提交乱码问题。
关键字: tapestry
现在正在使用T5开发一个小项目。 因为现在T5还正处于发展中。 而且没有像T4一样有许多的文档。 和例子(Workbench, Vlib)。 所以我会把在这个开发中遇到的一些问题记录下来。
tapestry5.0.5现在默认支持UTF-8编码。 但是发现在form提交数据后。 数据变成了乱码。
解决方案是增加一个Filter。
然后contribute给RequestHandler。
以上代码都是放在AppModule.java中。。
参考:http://wiki.apache.org/tapestry/Tapestry5Utf8Encoding
tapestry5.0.5现在默认支持UTF-8编码。 但是发现在form提交数据后。 数据变成了乱码。
解决方案是增加一个Filter。
public RequestFilter buildUtf8Filter(
@InjectService("RequestGlobals") final RequestGlobals requestGlobals)
{
return new RequestFilter()
{
public boolean service(Request request, Response response, RequestHandler handler)
throws IOException
{
requestGlobals.getHTTPServletRequest().setCharacterEncoding("UTF-8");
return handler.service(request, response);
}
};
}
然后contribute给RequestHandler。
public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
@InjectService("TimingFilter") final RequestFilter timingFilter,
@InjectService("Utf8Filter") final RequestFilter utf8Filter)
{
configuration.add("Utf8Filter", utf8Filter); // handle UTF-8
configuration.add("Timing", timingFilter);
}
以上代码都是放在AppModule.java中。。
参考:http://wiki.apache.org/tapestry/Tapestry5Utf8Encoding
评论
dengyin2000
2007-12-28
谁会用get去穿中文内容。 一般get都是用来传ID的。
liyong_2003_cn
2007-12-28
iorigod123 写道
小弟也碰到过这种问题,有upload组件就会提交成乱码,小弟的解决方法有点笨,就是搞个转换,居然可以成功转换,代码如下:
public static String getUTF8(String temp){
try{
return new String(temp.getBytes("iso-8859-1"),"utf-8");
}catch(Exception e){
return "";
}
}
象这种转换是最保险的,用filter的情况,对post提交是有效的,但是对get提交是无效的,这个跟post和get提交的机制有关系。以前项目遇到过这个问题,现在基本上都采用转换了,不用filter了,效率没仔细比较过,估计差不太多。
public static String getUTF8(String temp){
try{
return new String(temp.getBytes("iso-8859-1"),"utf-8");
}catch(Exception e){
return "";
}
}
如果是get方式提交,应该设置server.xml中RUIEncoding="UTF-8"
iorigod123
2007-12-28
小弟也碰到过这种问题,有upload组件就会提交成乱码,小弟的解决方法有点笨,就是搞个转换,居然可以成功转换,代码如下:
public static String getUTF8(String temp){
try{
return new String(temp.getBytes("iso-8859-1"),"utf-8");
}catch(Exception e){
return "";
}
}
public static String getUTF8(String temp){
try{
return new String(temp.getBytes("iso-8859-1"),"utf-8");
}catch(Exception e){
return "";
}
}
kris_xu
2007-09-05
如果是get方式提交,应该设置server.xml中RUIEncoding="UTF-8"
kris_xu
2007-09-05
和form的提交方式有关吗?get?post?
xo_tobacoo
2007-09-05
要解决乱码,首先要了解你的环境中那些位置涉及字符编码:
jsp页面要设置
开发工具要设置
连接数据库要设置
java包
最好的办法是使用楼主使用的方案,过滤器
在tomcat的示例里有个过滤器,拷贝过去,改下就可以了
jsp页面要设置
开发工具要设置
连接数据库要设置
java包
最好的办法是使用楼主使用的方案,过滤器
在tomcat的示例里有个过滤器,拷贝过去,改下就可以了
match927
2007-09-05
这个文件AppModule.java在那里?谢谢
javersion
2007-08-28
你的表单提交方式是用的post还是get,如果是get,就会发生乱码!
Linuxboy
2007-08-28
将这个问题提交到jira吧
koda
2007-08-28
我终于找到了问题发生的原因了!!!但是没有解决方案
真正的问题是:如果form里包含有上传文件的field,则其他textfield提交的中文乱码;或者,如果显式地在<form>标签中加入属性 enctype="multipart/form-data"则提交的textfield中文值乱码。
真正的问题是:如果form里包含有上传文件的field,则其他textfield提交的中文乱码;或者,如果显式地在<form>标签中加入属性 enctype="multipart/form-data"则提交的textfield中文值乱码。
dengyin2000
2007-08-24
我贴下我的吧。。 跟你的也差不多。。
java 代码
- package com.javaeye.dengyin2000.gtts.services;
- import java.io.IOException;
- import java.math.BigDecimal;
- import org.apache.commons.logging.Log;
- import org.apache.tapestry.Translator;
- import org.apache.tapestry.ioc.MappedConfiguration;
- import org.apache.tapestry.ioc.OrderedConfiguration;
- import org.apache.tapestry.ioc.ServiceBinder;
- import org.apache.tapestry.ioc.annotations.InjectService;
- import org.apache.tapestry.services.Request;
- import org.apache.tapestry.services.RequestFilter;
- import org.apache.tapestry.services.RequestGlobals;
- import org.apache.tapestry.services.RequestHandler;
- import org.apache.tapestry.services.Response;
- import com.javaeye.dengyin2000.gtts.tapestry.BigDecimalTranslator;
- /**
- * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to
- * configure and extend Tapestry, or to place your own service definitions.
- */
- public class AppModule
- {
- public static void bind(ServiceBinder binder)
- {
- // binder.bind(MyServiceInterface.class, MyServiceImpl.class);
- // Make bind() calls on the binder object to define most IoC services.
- // Use service builder methods (example below) when the implementation
- // is provided inline, or requires more initialization than simply
- // invoking the constructor.
- }
- public static void contributeApplicationDefaults(
- MappedConfiguration<String, String> configuration)
- {
- // Contributions to ApplicationDefaults will override any contributions to
- // FactoryDefaults (with the same key). Here we're restricting the supported
- // locales to just "en" (English). As you add localised message catalogs and other assets,
- // you can extend this list of locales (it's a comma seperated series of locale names;
- // the first locale name is the default when there's no reasonable match).
- configuration.add("tapestry.supported-locales", "en");
- }
- /**
- * This is a service definition, the service will be named "TimingFilter". The interface,
- * RequestFilter, is used within the RequestHandler service pipeline, which is built from the
- * RequestHandler service configuration. Tapestry IoC is responsible for passing in an
- * appropriate Log instance. Requests for static resources are handled at a higher level, so
- * this filter will only be invoked for Tapestry related requests.
- *
- * <p>
- * Service builder methods are useful when the implementation is inline as an inner class
- * (as here) or require some other kind of special initialization. In most cases,
- * use the static bind() method instead.
- *
- * <p>
- * If this method was named "build", then the service id would be taken from the
- * service interface and would be "RequestFilter". Since Tapestry already defines
- * a service named "RequestFilter" we use an explicit service id that we can reference
- * inside the contribution method.
- */
- public RequestFilter buildTimingFilter(final Log log)
- {
- return new RequestFilter()
- {
- public boolean service(Request request, Response response, RequestHandler handler)
- throws IOException
- {
- long startTime = System.currentTimeMillis();
- try
- {
- // The reponsibility of a filter is to invoke the corresponding method
- // in the handler. When you chain multiple filters together, each filter
- // received a handler that is a bridge to the next filter.
- return handler.service(request, response);
- }
- finally
- {
- long elapsed = System.currentTimeMillis() - startTime;
- log.info(String.format("Request time: %d ms", elapsed));
- }
- }
- };
- }
- public RequestFilter buildUtf8Filter(
- @InjectService("RequestGlobals") final RequestGlobals requestGlobals)
- {
- return new RequestFilter()
- {
- public boolean service(Request request, Response response, RequestHandler handler)
- throws IOException
- {
- requestGlobals.getHTTPServletRequest().setCharacterEncoding("UTF-8");
- return handler.service(request, response);
- }
- };
- }
- /**
- * This is a contribution to the RequestHandler service configuration. This is how we extend
- * Tapestry using the timing filter. A common use for this kind of filter is transaction
- * management or security.
- */
- public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
- @InjectService("TimingFilter")
- RequestFilter filter, @InjectService("Utf8Filter") RequestFilter utf8Filter)
- {
- // Each contribution to an ordered configuration has a name, When necessary, you may
- // set constraints to precisely control the invocation order of the contributed filter
- // within the pipeline.
- configuration.add("Utf8Filter", utf8Filter);
- configuration.add("Timing", filter);
- }
- public static void contributeTranslatorDefaultSource(
- MappedConfiguration<Class, Translator> configuration)
- {
- configuration.add(BigDecimal.class, new BigDecimalTranslator());
- }
- public static void contributeTranslatorSource(
- MappedConfiguration<String, Translator> configuration)
- {
- configuration.add("bigdecimal", new BigDecimalTranslator());
- }
- }
koda
2007-08-23
代码也贴出来
package org.opend.bogo.services;
public class AppModule {
public void contributeRequestHandler(
OrderedConfiguration<RequestFilter> configuration,
@InjectService("TimingFilter")
final RequestFilter timingFilter, @InjectService("Utf8Filter")
final RequestFilter utf8Filter) {
configuration.add("Utf8Filter", utf8Filter); // handle UTF-8
}
public RequestFilter buildUtf8Filter(
@InjectService("RequestGlobals") final RequestGlobals requestGlobals)
{
return new RequestFilter()
{
public boolean service(Request request, Response response, RequestHandler handler)
throws IOException
{
requestGlobals.getHTTPServletRequest().setCharacterEncoding("utf-8");
return handler.service(request, response);
}
};
}
}
koda
2007-08-23
我在onSuccess里面插入数据库之前打印就是乱码
另外我创建数据库的时候用下面的语句
CREATE DATABASE bogo CHARACTER SET UTF8 COLLATE utf8_general_ci;
另外我创建数据库的时候用下面的语句
CREATE DATABASE bogo CHARACTER SET UTF8 COLLATE utf8_general_ci;
dengyin2000
2007-08-23
koda 写道
引用
要记得清除开发环境和浏览器的缓存。
都清除了,也换成了jetty5.1.14. 问题依旧,绝望了:(
很有可能你的mysql数据库不是用的utf-8.
你可以调试下 在你onSuccess里面看看 提交后的java class的数据是不是乱码。
Linuxboy
2007-08-23
那就奇怪了。我的开发环境是:XPsp2+mysql5(utf8)+eclipse3.3+jetty6.1.3+tapestry5.0.5
在form中输入中文提交后,中文显示正常。
在form中输入中文提交后,中文显示正常。
koda
2007-08-23
引用
要记得清除开发环境和浏览器的缓存。
都清除了,也换成了jetty5.1.14. 问题依旧,绝望了:(
Linuxboy
2007-08-22
要记得清除开发环境和浏览器的缓存。
dengyin2000
2007-08-22
koda 写道
引用
确定你的html 和 java 文件的编码用的是utf-8? eclipse的话 在文件上右键 ---》 然后properties。
.java, .html采用UTF-8
数据库utf-8
环境: WindowsXP + mysql5.0.18 + jetty5.1.12
乱码依旧:(
jetty5.1.14. mysql5. 在ubuntu 和windows xp下就没问题。。 呵呵 RP有问题。
koda
2007-08-22
引用
确定你的html 和 java 文件的编码用的是utf-8? eclipse的话 在文件上右键 ---》 然后properties。
.java, .html采用UTF-8
数据库utf-8
环境: WindowsXP + mysql5.0.18 + jetty5.1.12
乱码依旧:(
dengyin2000
2007-08-22
koda 写道
我的环境也是jetty,但是就是乱码 :(
确定你的html 和 java 文件的编码用的是utf-8? eclipse的话 在文件上右键 ---》 然后properties。
- 浏览: 220493 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
VB-seamless
共 13 张
共 13 张
最近加入圈子
最新评论
-
使用Terracotta和Tomcat建 ...
renavatior 写道"运行start.bat 9081 这样我们就启动了目 ...
-- by rainsf -
使用Terracotta和Tomcat建 ...
"运行start.bat 9081 这样我们就启动了目录9081中的tomcat ...
-- by renavatior -
广州3年多经验 5500的 ...
fucku 写道广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多, ...
-- by yongfan_420 -
广州3年多经验 5500的 ...
广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多,导致机会也没有这 ...
-- by fucku -
广州3年多经验 5500的 ...
想高工资就去厂家咯,老在集成商里面混能有多大个奔头
-- by fucku






评论排行榜