请教:struts2中 传参乱码问题

sun_abc 2008-11-05 03:50:02
在struts2中,
通过<s:url value="item.action"><s:param name="steps" value="steps"/></s:url>
得到的参数是乱码,
注:steps 在action中是输出正常的中文数组(String[] steps)

并且硬编码写上, 如:item.action?steps=你好 提交到action中后也是乱码。

请熟悉struts2的大哥大姐们指教!
...全文
444 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jincheng2008 2008-11-11
  • 打赏
  • 举报
回复
又是乱码...
Tomcat的安装目录webapps\examples\WEB-INF\classes\filters
有SetCharacterEncodingFilter.java!
整理后的代码:



package filters;


import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;




public class SetCharacterEncodingFilter 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);
}

// 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);

}


}

要学会看帮助文档....
pobaby 2008-11-10
  • 打赏
  • 举报
回复
=%E6%88%91 这种格式需要解码才行:

eg:
String newPar = URLDecoder.decode(request.getParameter("oldPar"),
"UTF-8");


[Quote=引用 4 楼 sun_abc 的回复:]
我试了都不行,现在解决了一个问题
jsp页面中: <a href="helloWorld.action?name= <%=java.net.URLEncoder.encode("你好")%>">TEST </a>
action中:System.out.println(new String(name.getBytes("iso-8859-1")));
可以正常输出提交的参数。

现在的问题是:
actin中:steps 在action中是输出正常的中文数组(String[] steps)

jsp页面中这样写:
<s:url id="helloWorld" value="helloWorld.action" > <s:param…
[/Quote]
时光瞄 2008-11-10
  • 打赏
  • 举报
回复
写一个CharacterEncodingFilter...
sun_abc 2008-11-10
  • 打赏
  • 举报
回复
也就是用UTF-8编码的参数,提交到action里后
并不是原样的UTF-8编码.
sun_abc 2008-11-10
  • 打赏
  • 举报
回复
那个所谓的乱码是UTf-8编码,
用String newPar = URLDecoder.decode(request.getParameter("oldPar"), "UTF-8");
能把那些编码解码成正常的汉字字符串.

可是又出现一个问题:
<div dojoType="struts:BindDiv"
id="middle" label="项目实例" href="helloWorld.action?steps=%E6%88%91&steps=%E9%A5%BF" showError="true"> </div>
提交到action里的内容,并不是?后的uf-8编码 (%E6%88%91&steps=%E9%A5%BF)
而是一大堆的问号(?????)
所以解码后的字符串也还是乱码.
ZA_cameron 2008-11-07
  • 打赏
  • 举报
回复
这个不叫乱码,是UTF-8编码
jumpheightway 2008-11-07
  • 打赏
  • 举报
回复
set方法里面这样就行了
new String(name.getBytes("iso-8859-1"),“gbk”);
sun_abc 2008-11-06
  • 打赏
  • 举报
回复
我试了都不行,现在解决了一个问题
jsp页面中:<a href="helloWorld.action?name=<%=java.net.URLEncoder.encode("你好")%>">TEST</a>
action中:System.out.println(new String(name.getBytes("iso-8859-1")));
可以正常输出提交的参数。

现在的问题是:
actin中:steps 在action中是输出正常的中文数组(String[] steps)

jsp页面中这样写:
<s:url id="helloWorld" value="helloWorld.action" ><s:param name="steps" value="steps"/></s:url>
<s:div id="middle" label="项目实例" theme="ajax" href="%{helloWorld}"></s:div>

提交到action后,在jsp页面中得到的steps参数是乱码:
<div dojoType="struts:BindDiv"
id="middle" label="项目实例" href="helloWorld.action?steps=%E6%88%91&steps=%E9%A5%BF" showError="true"></div>


orz_ORZ_orz 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ZA_cameron 的回复:]
<constant name="struts.i18n.encoding" value="UTF-8" />
[/Quote]
同意,一种方式,在struts.xml中配置
ZA_cameron 2008-11-05
  • 打赏
  • 举报
回复
<constant name="struts.i18n.encoding" value="UTF-8" />
orz_ORZ_orz 2008-11-05
  • 打赏
  • 举报
回复
什么意思?是action返回的jsp中是乱码还是什么地方是乱码?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧