关于struts2URL传中文乱码的解决

好久没有碰struts2了,今天刚试着一上手,就遇到了一个问题。URL传中文传值乱码、大致是这样的:

比如index.jsp页面写了一句:<a href="wo?username=我&password=1">点击</a>。

然后在struts.xml配置文件:

<package name="font2" extends="struts-default">
<action name="wo" class="com.guang.action.tags" method="tags" >
<result>
/tags.jsp
</result>
</action>
</package>

那么在tags类里面,来接收参数的:

public class tags extends ActionSupport{
private String password;
private String username;
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) throws UnsupportedEncodingException {
this.username =new String(username.getBytes("ISO-8859-1"),"gbk");
}
public String tags(){
System.out.println("username="+username);
return SUCCESS;
}
}

注意红线粗体字,这个就是重点了、之后在tags.jsp中显示也是中文,后台也是中文了

希望对需要的朋友有用!

你可能感兴趣的:(struts2)