<html:link page="/action.do" paramName="name" paramProperty="property" paramId="id">
表示request参数paramId = paramName.paramProperty;
即id = name.getProperty();/**
*struts-config.xml
*/
<action parameter="id"
type="MyAction"
path="/action.do">
<forward name="success" path="/pages/index.jsp" />
</action>------------------------------
/**
*MyAction.java
*/
...
excute(){
...
String id = request.getParameter("id");
//如果遇到中文问题,尝试
//String id = new String(request.getParameter("id").getBytes("ISO8859_1"));
...
}
表示request参数paramId = paramName.paramProperty;
即id = name.getProperty();/**
*struts-config.xml
*/
<action parameter="id"
type="MyAction"
path="/action.do">
<forward name="success" path="/pages/index.jsp" />
</action>------------------------------
/**
*MyAction.java
*/
...
excute(){
...
String id = request.getParameter("id");
//如果遇到中文问题,尝试
//String id = new String(request.getParameter("id").getBytes("ISO8859_1"));
...
}
下面是一段说明这个问题的话:
如果通过 get/post 方式从 browser 返回的参数值中包含汉字信息, servlet 将无法得到正确的值。SUN的 J2SDK 中,HttpUtils.parseName 在解析参数时根本没有考虑 browser 的语言设置,而是将得到的值按 byte 方式解析。这是网上讨论得最多的 encoding 问题。因为这是设计缺陷,只能以 bin 方式重新解析得到的字符串;或者以 hack HttpUtils 类的方式解决。参考文章 2 均有介绍,不过最好将其中的中文 encoding GB2312、 CP1381 都改为 GBK,否则遇到 GBK 汉字时,还是会有问题。
Servlet API 2.3 提供一个新的函数 HttpServeletRequest.setCharacterEncoding 用于在调用 request.getParameter(“param_name”) 前指定应用程序希望的 encoding,这将有助于彻底解决这个问题。
在WEB-INF/web.xml中设置character-encoding来解决:
<web-app character-encoding="GBK">
...
</web-app>