遇到一个吐血小问题!!!!!!

eemean 2010-12-01 03:20:27
环境: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传值,也是无论如何得不到值。


求神人赐教!!

(考虑从头开始另搭建环境)
...全文
152 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshi1017 2010-12-02
  • 打赏
  • 举报
回复
LZ,搞定了就来说下是哪里的问题
magong 2010-12-01
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 eemean 的回复:]

用Firefox测和用IE测,反应一个样,啥东西都不弹出来

[/Quote]
虽然你已经结贴,好奇问一句,啥东西都弹不出来,包括
if(username=="")
{
alert("不能为空");
}
这个么?
eemean 2010-12-01
  • 打赏
  • 举报
回复
环境的初始化是令人又哭又笑的工作。
我会回来的!

结贴
eemean 2010-12-01
  • 打赏
  • 举报
回复
用Firefox测和用IE测,反应一个样,啥东西都不弹出来

改成这样也不行,但可以在Action中打印到后台:
<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>
服了
eemean 2010-12-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 magong 的回复:]
引用 3 楼 eemean 的回复:
package test.s2sh.action;
public class TestAction extends BaseAction


<action name="test" class="testAction" method="test">

struts.xml文件中和Action的定义不符。
[/Quote]
testAction是定义在spring里的bean,估计没关系
magong 2010-12-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 eemean 的回复:]
package test.s2sh.action;
public class TestAction extends BaseAction


<action name="test" class="testAction" method="test">
[/Quote]
struts.xml文件中和Action的定义不符。
tianshi1017 2010-12-01
  • 打赏
  • 举报
回复
9楼的也可以
tianshi1017 2010-12-01
  • 打赏
  • 举报
回复
$.post("http://localhost/test/test?username="
+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; }
数据流 2010-12-01
  • 打赏
  • 举报
回复
服务器端不用改,客户端改为:
$.post("http://localhost/test/test",{'username':username},function(data){
alert(......);
});

即可。
eemean 2010-12-01
  • 打赏
  • 举报
回复
jsp取值我之前试过了一天,都是null。想用一下struts2咋这么难呢

to 4楼哥们,我照做了,一样不行

(看来是得完全新搭环境了,这五年的程序员白当了)
mopishv0 2010-12-01
  • 打赏
  • 举报
回复
推荐LZ用 STRUTS2的 JSON返回类型 很方便
tianshi1017 2010-12-01
  • 打赏
  • 举报
回复
只看到你有ajax取值
tianshi1017 2010-12-01
  • 打赏
  • 举报
回复
没看出你页面上哪里直接与action取值了
屌丝团 2010-12-01
  • 打赏
  • 举报
回复

request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(username);

LZ,试试!
eemean 2010-12-01
  • 打赏
  • 举报
回复

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>


tianshi1017 2010-12-01
  • 打赏
  • 举报
回复
纯action向jsp传值,也是无论如何得不到值。
???
贴代码出来撒,你后台都拿到值了,前台拿不到,说明代码有问题

然后你的ajax取值,你的代码呢,也不贴,调用的方法有没错????
屌丝团 2010-12-01
  • 打赏
  • 举报
回复
贴action代码瞧瞧。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧