67,513
社区成员
发帖
与我相关
我的任务
分享
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(username);
LZ,试试!
package test.s2sh.action;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends BaseAction
{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String test() throws IOException
{
try {
String username2 = URLDecoder.decode(username, "UTF-8");
System.out.println(username2);
response.setCharacterEncoding("UTF-8");
response.getWriter().write("dddddd"+username);
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null; 或return SUCCESS;都一样
}
}
package test.s2sh.action;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
private static final long serialVersionUID = 7620009925942346125L;
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
//HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
HttpServletResponse response = ServletActionContext.getResponse();
Map session = context.getSession();
}
<package name="test" extends="struts-default">
<action name="test" class="testAction" method="test">
<result name="success">
/Save_success.jsp #不跳转,这个其实没用的
</result>
</action>
</package>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/jquery.js"></script>
<!--<script type="text/javascript" src="js/test.js"></script> -->
<script>
$(document).ready(function(){
$("#userBtt").click(function(){
var username = $("#username").val();
if(username=="")
{
alert("不能为空");
}
else
{
$.post("http://localhost/test/test?username="
+encodeURI(encodeURI(username)),function(data){
alert("Data Loaded: " + data);
});
}
});
}
);
</script>
</head>
<body>
<table>
<tr>
<td>请输入用户名:<input type="text" name="username" id="username"></td>
<td><input type="button" value="校验" id="userBtt"></td>
<td></td>
</tr>
</table>
</body>
</html>