编码转化了,还是乱码,大家帮帮忙,在线等待!
jsp页面里传过去一个值例如:<html:link page="/XmList.do?xmLx=基建项目库">,在控制台输出request编码是gb2312,但是得到的值还是乱码,为什么?
大家帮帮忙,小弟谢过了~~~很郁闷~
相关文件如下(编译成功):
filter文件:
-------------------------------------
package 包;
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 {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
System.out.println("filter2 is doing !");
System.out.println("encoding:"+encoding);
System.out.println("ignore:"+ignore);
request.setCharacterEncoding(encoding);
}
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);
}
}
-----------------------------------------------------------
-----------------------------------------------------------
web.xml文件:
-----------------------------------------------------------
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>包</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
--------------------------------------------------------
--------------------------------------------------------
action文件:
--------------------------------------------------------
package 包.Actions;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import 包.GetXmList;
import 包.PageForm;
public class XmListAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
PageForm f = (PageForm)form;
//Get the parameter from Form
String xmLx = f.getXmLx();
//调试
System.out.println("request CharacterEncoding :" + request.getCharacterEncoding());
System.out.println("response CharacterEncoding :" + response.getCharacterEncoding());
System.out.println("xmLx in action is:" + xmLx);
//调试结束
int countPerPage = f.getCountPerPage();
int page = f.getPage();
if(page <= 0 ) f.setPage(1);
GetXmList gal = new GetXmList();
if(xmLx == null || xmLx.equals("") || xmLx.equals("*"))
{
gal.getList("*",countPerPage,page);
}
else
{
gal.getList(xmLx,countPerPage,page);
}
if(page > gal.getPageTotal())
{
f.setPage(gal.getPageTotal());
}
request.setAttribute("coll",gal.getColl());
request.setAttribute("xmTotal",String.valueOf(gal.getXmTotal()));
request.setAttribute("pageTotal",String.valueOf(gal.getPageTotal()));
request.setAttribute("page",String.valueOf(gal.getPage())); //获得当前页
request.setAttribute("pageForm",f);
return mapping.findForward("showXmList");
}
}
---------------------------------------------------------------------
---------------------------------------------------------------------
PageForm文件:
-------------------------------------------------------------
package 包.FormBeans;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import java.util.HashMap;
public class PageForm
extends ActionForm
{
private String xmLx = "*";
private int countPerPage = 20;
private int page = 1;
private HashMap nextLinkMap = new HashMap(); //该变量用于存放上下页链接所用的Map,下一页
private HashMap lastLinkMap = new HashMap(); //该变量用于存放上下页链接所用的Map,上一页
//上一页
public HashMap getLastLinkMap()
{
this.lastLinkMap.put("xmLx",this.xmLx);
this.lastLinkMap.put("page",String.valueOf(this.page-1));
return this.lastLinkMap;
}
//下一页
public HashMap getNextLinkMap()
{
this.nextLinkMap.put("xmLx",this.xmLx);
this.nextLinkMap.put("page",String.valueOf(this.page+1));
return this.nextLinkMap;
}
public String getXmLx()
{
return xmLx;
}
public void setXmLx(String xmLx)
{
this.xmLx = xmLx;
}
public int getCountPerPage()
{
return countPerPage;
}
public void setCountPerPage(int countPerPage)
{
this.countPerPage = countPerPage;
}
public int getPage()
{
return page;
}
public void setPage(int page)
{
this.page = page;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
/**@todo: finish this method, this is just the skeleton.*/
return null;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
xmLx = "*";
countPerPage = 20;
page = 1;
}
}
-------------------------------------------------------
------------------------------------------------------