小屌丝公务员,用从1.9开始使用,用jfinal做过几个小项目,很感谢作者。最近在给单位弄一个在线文档管理系统,在使用restful风格时,中文传参后台取值问题,具体代码如下:
$('#commsearch').on('click',function(event){
//解决url传参中文乱码
window.location = url + '/' + encodeURI($('#str').val(),"UTF-8") + '-' + $('input:radio[name="pid"]:checked').val();
//window.location = url + '?str=' + $('#str').val() + '&pid=' + $('input:radio[name="pid"]:checked').val();
});这是前台js按钮监听,然后#str的val是中文,使用传统的?name1=value2&name2=value2后台通过getPara("name1")中文正常,定位到core.Controller中知此方法是直接调用request.getParameter("name1");使用v0-v1-v2 这种restful风格后台getPara(0)..这样取值则中文直接后台syso出来的是utf-8编码,系统编码utf-8,自带jetty和tomcat配置 URIEndcoding="utf-8",情况一样,查看getPara(int index)方法知道是从urlParaArrary中获取值,我也不知道这里有没有做转码处理,即URLDecoder.decode(urlParaArrary[i],"UTF-8");
JFinal action report -------- 2017-11-18 21:52:48 ------------------------------ Url : GET /ft/activity/all/%E8%A5%BF-0 Controller : gov.xichangxiang.www.controller.ActivityController.(ActivityController.java:1) Method : all UrlPara : %E8%A5%BF-0 Interceptor : gov.xichangxiang.www.interceptor.FtInterceptor.(FtInterceptor.java:1)
这是v0-v1传参get方式提交,后台getPara(0)出来直接是utf-8,还需要手动转换一遍.
JFinal action report -------- 2017-11-18 21:55:59 ------------------------------ Url : GET /ft/activity/all Controller : gov.xichangxiang.www.controller.ActivityController.(ActivityController.java:1) Method : all Interceptor : gov.xichangxiang.www.interceptor.FtInterceptor.(FtInterceptor.java:1) Parameter : str=西 pid=0 --------------------------------------------------------------------------------
这是传统?name1=value2&name2=value2传参get方式提交,后台getPara("name1")获取正常,然后我就加了两个方法:
/**
* Get para from url. The index of first url para is 0.
*/
public String getParaWithUTF8(int index) {
if (index < 0)
return getPara();
if (urlParaArray == null) {
if (urlPara == null || "".equals(urlPara)) // urlPara maybe is "" see ActionMapping.getAction(String)
urlParaArray = NULL_URL_PARA_ARRAY;
else
urlParaArray = urlPara.split(URL_PARA_SEPARATOR);
for (int i=0; i<urlParaArray.length; i++)
if ("".equals(urlParaArray[i]))
urlParaArray[i] = null;
}
return urlParaArray.length > index ? URLDecoder.decode(urlParaArray[index],"UTF-8"): null;
}
/**
* Get para from url. The index of first url para is 0.
*/
public String getParaWithDecoder(int index, String code) {
if (index < 0)
return getPara();
if (urlParaArray == null) {
if (urlPara == null || "".equals(urlPara)) // urlPara maybe is "" see ActionMapping.getAction(String)
urlParaArray = NULL_URL_PARA_ARRAY;
else
urlParaArray = urlPara.split(URL_PARA_SEPARATOR);
for (int i=0; i<urlParaArray.length; i++)
if ("".equals(urlParaArray[i]))
urlParaArray[i] = null;
}
return urlParaArray.length > index ? URLDecoder.decode(urlParaArray[index],code): null;
}恳请大神@詹波帮我编译下,得到Controller.class我的邮箱zuixin02170321@qq.com
项目:JFinal