-
高手请进,JS获取action里request.setAttribute("sendMeg", message) 设置的值3
我用Ajax异步提交后;再获取action的值,具体如下页面 //异步条件下发请求 function _sendPara(){ (function($) { $(function() { $.ajax({ type:"post", url:"sendPara_send.do?Ips="+Ips+"&TableNames="+TableNames, success:function(res){ alert("${sendMeg}"); } }); }); })(jQuery); }
Action里的代码:request.setAttribute("sendMeg", message);
我在页面上的处理方式没能获取到值,请问各位我该如何获取呢
问题补充:andy_javahome 写道你的res里面是什么数据,你异步请求返回的数据类型是什么还是没有返回数据只是一个操作。
一个正常的解决思路是你用Ajax的json处理,这个你要是会的话我就不多说了,要是不会我几句话也说不明白。
另一个就是比较笨的办法,就是你在你的action请求给个结果页面,这个页面里的内容就是一个hidden类型的input,这个input的value就是你的${sendMeg},然后再你Ajax的回调函数里的res就是这个就是的那个小页面(其实里面可能就一个input),接下来你可以随意处理,你可以把这个res放到某个div里 反正是隐藏的,然后你在得到这个input的值。
这个方法貌似很笨但是是万能的。
你好andy_javahome! 你说的Ajax的JSON处理方式是怎么样的呢
问题补充:andy_javahome 写道如果你会Ajax不会json你还真的学学,我简单给你说说 。
1、首先你得引入一个jar 我的版本是json-lib-2.1.jar在网上随便都能下到。
2、在后台写个方法:
public void writerJSON(Object object,HttpServletResponse reponse) {
JSONObject jsonObject = JSONObject.fromObject(object);
try {
Writer writer = reponse.getWriter();
System.out.println("Json Data :" + jsonObject.toString());
writer.write(jsonObject.toString());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我们通常把这个方法卸载BaseAction里,这个是准备工作,那个object可以是你的任何对象和属性。
3、一般将你的action 的返回值设置为void 在action方法里的最后返回你的数据
this.writerJSON(message, reponse);这两个参数是上面方法里的,如果你的只是个字符串就简单了,通常都是传出个对象然后对象点你的属性
4.页面应该:
$(function() {
$.ajax({
type:"post",
url:"sendPara_send.do?Ips="+Ips+"&TableNames="+TableNames,
dataType : "json",
success:function(res){
//这个地方就看你的数据类型了如果只是个String的
alert(res);
//如果你把那个字符串放入对象里可以这样写
alert(res.sendMeg);
}
});
});
就说这些了有什么不理解可以一起讨论,还有上面那哥们怀疑你那是不是el表达式,我确定 你用的${sendMeg}就是el表达式,而且在js里用时没问题的,但在回调函数里的去数据,是json最擅长的。
JSONObject jsonObject = JSONObject.fromObject(object); 里的fromObject(object)方法是从哪里来的呢,我引入包,并没有发现这个方法
问题补充:andy_javahome 写道
3、一般将你的action 的返回值设置为void 在action方法里的最后返回你的数据
this.writerJSON(message, reponse);这两个参数是上面方法里的,如果你的只是个字符串就简单了,通常都是传出个对象然后对象点你的属性
刚刚没方向方法是我大意了,我现在是这样写action的:public void send(){ ....... ActionContext context = ActionContext.getContext(); HttpServletResponse reponse=(HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE); ...... message=sendText.toString(); this.writerJSON(message, reponse); }
这样写页面没有数据
问题补充:ethen 写道to andy_javahome:
你确定js里面用EL没问题?
我没有用EL ,后台的代码如:public void send(){ ... HttpServletResponse reponse=ServletActionContext.getResponse(); ... message=sendText.toString(); this.writerJSON(message, reponse); } public void writerJSON(Object object, HttpServletResponse reponse) { reponse=ServletActionContext.getResponse(); JSONObject jsonObject = JSONObject.fromObject(object); try { Writer writer = reponse.getWriter(); System.out.println("Json Data :" + jsonObject.toString()); writer.write(jsonObject.toString()); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
页面的代码:$(function() { $.ajax({ type:"post", url:"sendPara_send.do?Ips="+Ips+"&TableNames="+TableNames, dataType : "json", success:function(res){ alert(res); } }); });
2010年11月29日 10:14
8个答案 按时间排序 按投票排序
-
采纳的答案
哥们你这个${sendMeg}就是el表达式 ${};我经常在js里用,没出过问题,但也没用过复杂的在js里。
给你解决问题***************************************
json返回的可以 对象 数组 数字 串 true false null,这个你可以在你引入的net.sf.json.JSONObject;把JSONObject去掉你点一下看都有什么类
JSONObject jsonObject = JSONObject.fromObject(object);
这个就是对象 要求应该是键值型,在不改变writerJSON方法的前提下你可以将你要传的值这样写:Map map =new HashMap();
String message=sendText.toString();
map.put("urlm", message);
this.writerJSON(map, reponse);
简单解释一下,你要是传的对象fromObject处理后就是个map,因为你的对象一定是有属性名和属性值。在页面绝对得到2010年11月29日 15:22
-
aciont里面应该用response.getOutputStream().write("");
之所以你获取不到值,是因为你的页面没有再次编译。2010年11月29日 16:42
-
如果你会Ajax不会json你还真的学学,我简单给你说说 。
1、首先你得引入一个jar 我的版本是json-lib-2.1.jar在网上随便都能下到。
2、在后台写个方法:
public void writerJSON(Object object,HttpServletResponse reponse) {
JSONObject jsonObject = JSONObject.fromObject(object);
try {
Writer writer = reponse.getWriter();
System.out.println("Json Data :" + jsonObject.toString());
writer.write(jsonObject.toString());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我们通常把这个方法卸载BaseAction里,这个是准备工作,那个object可以是你的任何对象和属性。
3、一般将你的action 的返回值设置为void 在action方法里的最后返回你的数据
this.writerJSON(message, reponse);这两个参数是上面方法里的,如果你的只是个字符串就简单了,通常都是传出个对象然后对象点你的属性
4.页面应该:
$(function() {
$.ajax({
type:"post",
url:"sendPara_send.do?Ips="+Ips+"&TableNames="+TableNames,
dataType : "json",
success:function(res){
//这个地方就看你的数据类型了如果只是个String的
alert(res);
//如果你把那个字符串放入对象里可以这样写
alert(res.sendMeg);
}
});
});
就说这些了有什么不理解可以一起讨论,还有上面那哥们怀疑你那是不是el表达式,我确定 你用的${sendMeg}就是el表达式,而且在js里用时没问题的,但在回调函数里的去数据,是json最擅长的。2010年11月29日 13:23
-
看错了,把${}看成EL了,可能是下面有句request.setAttribute("sendMeg", message); 让我以为是从request中拿数据了。不过如果你想要在ajax中拿到返回的数据,直接用ls的response.getWriter().write(text)就行了。当然也可以写在request中,不过你要在页面上有一个控件id为sendMeg,然后把该控件的值设置为request.getAttribute(“sendMeg”)。只有这样才能通过${sendMeg}拿到值。
2010年11月29日 12:58
-
你的res里面是什么数据,你异步请求返回的数据类型是什么还是没有返回数据只是一个操作。
一个正常的解决思路是你用Ajax的json处理,这个你要是会的话我就不多说了,要是不会我几句话也说不明白。
另一个就是比较笨的办法,就是你在你的action请求给个结果页面,这个页面里的内容就是一个hidden类型的input,这个input的value就是你的${sendMeg},然后再你Ajax的回调函数里的res就是这个就是的那个小页面(其实里面可能就一个input),接下来你可以随意处理,你可以把这个res放到某个div里 反正是隐藏的,然后你在得到这个input的值。
这个方法貌似很笨但是是万能的。2010年11月29日 10:31
相关推荐
- **发生位置**:y2ssh.sg.chp1.action.AddAction.execute(AddAction.java:18) - **相关代码**:y2ssh.sg.chp1.framework.Controller.doGet(Controller.java:25), y2ssh.sg.chp1.framework.Controller.doPost...
javascript request.setAttribute()详解 request.setAttribute()怎么用的? JSP1代码 String [] test=new String[2]; test[0]=1; test[1]=2; request.setAttribute(test,test) ; response.sendRedirect(jsp2.jsp); ...
`request.setAttribute()`和`request.getAttribute()`是用于在请求范围内设置和获取属性的方法。`request.setAttribute()`允许开发者将任意对象存储在请求范围中,而`request.getAttribute()`则用于从请求范围中检索...
request.setAttribute() 方法用来设置服务器端的变量,而 request.getAttribute() 方法用来获取服务器端的变量。 request.setAttribute() 方法可以将变量设置到 request 范围内,例如: request.setAttribute(...
例如,JSP中设置一个隐藏字段,然后在JavaScript中读取该字段的值。 ```jsp // JSP ${username}" /> // JavaScript var username = document.getElementById("hiddenUsername").value; ``` 总结来说,理解和熟练...
request.setAttribute("ulist",userList); request.getRequestDispatcher("/admin/show.jsp").forward(request,response); } public void logout(HttpServletRequest request, HttpServletResponse response) ...
request.setAttribute("toolbar", toolbar); // 条件过滤数据,传送到页面 request.setAttribute("xm", xm); request.setAttribute("rxsj", rxsj); request.setAttribute("yx", yx); request.setAttribute("zy", zy);...
标题中的“访问或添加三个范围的属性与获取Request-Session-ServletContext-Response对象”指的是在Java Web开发中,如何操作四个重要的请求处理对象:HttpServletRequest(Request)、HttpSession(Session)、...
我们可以使用 `request.setAttribute()` 方法将数据设置到 Request 对象中,然后在 JSP 页面中使用 `request.getAttribute()` 方法来获取这些数据。 在下面的示例代码中,我们创建了一个 Servlet 类,使用 `request...
request.setAttribute("msg", "路径有误请重试"); request.getRequestDispatcher("/404.jsp").forward(request, response); } } public void addCategory(HttpServletRequest request, ...
request.getSession().setAttribute("UserAccessToken",token); } request.setAttribute("openid", token.getOpenid()); request.setAttribute("accessToken", token.getAccessToken()); request....
// session.setAttribute("redirectUrl", request.getRequestURL().append("?").append(request.getQueryString()).toString()); String url = request.getParameter("url"); URLEncoder.encode(URLDecoder....
HttpServletRequest request=ServletActionContext.getRequest(); HttpSession session=request.getSession(); session.setAttribute("bookName", this.bookName); session.setAttribute("bookPrice", this....
最近在做一个文件下载的功能,这里把做的过程中用... elt.setAttribute('href', url); elt.setAttribute('download', 'file.png'); elt.style.display = 'none'; document.body.appendChild(elt); elt.click(); d
此时,在`2.jsp`页面可以通过`request.getAttribute("test")`获取到之前设置的数据。 #### 4. RequestDispatcher.forward()详解 `RequestDispatcher.forward()`方法用于将当前请求转发到一个新的资源(如Servlet或...
request.setAttribute("pageControlNumberType1", pageControl.pageControl()); request.setAttribute("pageControlNumberType2", pageControl2.pageControl()); request.setAttribute("pageControlNumberType3", ...
request.setAttribute("pageControlNumberType1", pageControl.pageControl()); request.setAttribute("pageControlNumberType2", pageControl2.pageControl()); request.setAttribute("pageControlNumberType...
List<Message> msgList = mDao.findAll(); if(null == u.getUname()){ response.sendRedirect("jspPages/login.jsp"); }else{ HttpSession session = request.getSession(); session.setAttribute("userName",...
为了提高效率,我们可以通过直接指定搜索域的方式来优化性能,例如使用`${requestScope.a}`来明确地从请求作用域中获取变量`a`。 #### 三、EL表达式在不同数据结构中的应用 ##### 1. 对象的取值 - **后台设置**:...