1.访问路径中带中文,乱码问题
我们在使用重定向访问页面时,往往带着参数,而有时参数是中文的。比如在修改用户时,需要把姓名传参过去。则此时,需要在action中:
this.username=URLEncoder.encode("周小伟","UTF-8");
package com.hongwei.action;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.opensymphony.xwork2.ActionSupport;
public class EmployeeUpdateAction extends ActionSupport{
private String username;
public String getUsername() {
return username;
}
public String execute() throws UnsupportedEncodingException{
//this.username="zhouxiaowei";
this.username=URLEncoder.encode("周小伟","UTF-8");
return "success";
}
}
struts.xml:
<action name="employeeupdate" class="com.hongwei.action.EmployeeUpdateAction" method="execute">
<result name="success" type="redirect">/employeeUpdate.jsp?username=${username}</result>
</action>
employeeUpdate.jsp:
<%@ page language="java" import="java.util.*,java.net.URLDecoder" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
employeeUpdate page<br>
${param.username}<!-- 访问路径中的参数 -->
123
<br>
<!-- 要是乱码,则使用下面这句 -->
<%= URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8") %>
</body>
</html>