环境:struts2_spring_hiber + jquery
用:
$.post("http://localhost/test/test?username="
+encodeURI(encodeURI(username)),function(data){
alert("Data Loaded: " + data);
}); 能把username传到Action中,能在后台打印显示,但用:
response.setCharacterEncoding("UTF-8");
response.getWriter().write(username);死活不在页面上有alert怀疑我的环境搭建得有问题,之前使用纯action向jsp传值,也是无论如何得不到值。
求神人赐教!!(考虑从头开始另搭建环境)
用:
$.post("http://localhost/test/test?username="
+encodeURI(encodeURI(username)),function(data){
alert("Data Loaded: " + data);
}); 能把username传到Action中,能在后台打印显示,但用:
response.setCharacterEncoding("UTF-8");
response.getWriter().write(username);死活不在页面上有alert怀疑我的环境搭建得有问题,之前使用纯action向jsp传值,也是无论如何得不到值。
求神人赐教!!(考虑从头开始另搭建环境)
???
贴代码出来撒,你后台都拿到值了,前台拿不到,说明代码有问题然后你的ajax取值,你的代码呢,也不贴,调用的方法有没错????
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();
} struts.xml <package name="test" extends="struts-default">
<action name="test" class="testAction" method="test">
<result name="success">
/Save_success.jsp #不跳转,这个其实没用的
</result>
</action>
</package>
index.jsp<%@ 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>
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(username);LZ,试试!
$.post("http://localhost/test/test",{'username':username},function(data){
alert(......);
});即可。
+encodeURI(encodeURI(username)),function(data){
alert("Data Loaded: " + data);
});
试试这样看:var url ="http://localhost/test/test?username="
+encodeURI(encodeURI(username);
$.ajax( {
type : "post",
url : url,
dataType : "json",
success :function(data){
alert("Data Loaded: " + data);
,
async : false
});
然后:
public String test() throws IOException
{
String username2 = URLDecoder.decode(username, "UTF-8");
System.out.println(username2);
return SUCCESS; }
<script>
$(document).ready(function(){
$("#userBtt").click(function(){
var username = $("#username").val();
var url="http://localhost/test/test?username="+username;
$.ajax( {
type : "post",
url : url,
success :function(data){
alert("Data Loaded: " + data);
},
async : false
});
});
}
);
</script>
服了
if(username=="")
{
alert("不能为空");
}
这个么?