当前位置:编程学习 > JAVA >>

高分请教如何从struts2的action中获取前台javascript的字符串

jsp页面的javascript代码: 
function change(parm){ 
 ..
 var  path ="你好";
}
这个页面通过struts2跳转,请问在action中如何获取path这个字符串?请给出思路及代码,谢谢! --------------------编程问答-------------------- 放在url里传过去 --------------------编程问答-------------------- var path ="你好";

1. url
document.forms[0].action = "xxx.action?path=" + encodeURI(encodeURI(path));
document.forms[0].submit();
action
request.getParameter("path");


2. set val into form hidden;
document.getElementById("path").value = path;
<input type="hidden" value="" id="path" />

action
private String path;

setter/getter; --------------------编程问答--------------------
String path = request.getParameter("path");
path = java.net.URLDecoder.decode(path, "UTF-8"); 
--------------------编程问答-------------------- 你这段javascript代码应该要在页面跳转之前执行
代码中把这变量设置到request
这样后台才能获取到数据 --------------------编程问答-------------------- 我继续解答吧,这次只能贴代码了
<s:file name="uploadPic" id="upload" onchange="loadPath()"/>
<s:hidden name="uploadPath" id="path"/>//用来传递上传文件本地路径


function loadPath()
{
    //通过ID获取文件域的文件名(包括路径)
    var filePath=document.getElementById("upload").value;

    //将文件名(包括路径)赋予隐藏域
    document.getElementById("path").value=filePath;
    alert(filePath);
}


以上代码不兼容FIREFOX --------------------编程问答-------------------- 上面的JS代码不兼容IE8和FIREFOX,重新发过可以兼容的JS代码

function getFilePath(obj)   
{   
  if(obj)   
   {   
    if (window.navigator.userAgent.indexOf("MSIE")>=1)   
     {   
       obj.select();   
       return document.selection.createRange().text;   
     }   
    else if(window.navigator.userAgent.indexOf("Firefox")>=1)   
     {   
      if(obj.files)   
       {   
        return obj.files.item(0).getAsDataURL();   
       }   
      return obj.value;   
     }   
    return obj.value;   
   }   
}    



function loadPath()
{
    //通过ID获取文件域的文件名(包括路径)
var obj=document.getElementById("upload");
    var filePath=getFilePath(obj);

    //将文件名(包括路径)赋予隐藏域
    document.getElementById("path").value=filePath;
    alert(filePath);
}

--------------------编程问答-------------------- 通过url传参或放大隐藏域中。 --------------------编程问答-------------------- javascript中:
var path = "yourPath";
location.href="yourAction.action?path="+path;

Action中:
String path = request.getParameter("path"); --------------------编程问答-------------------- 最简单url里传过去,后台这样取参数就行了。String param= request.getParameter("param"); --------------------编程问答-------------------- var path ="你好";

1. url
document.forms[0].action = "xxx.action?path=" + encodeURI(encodeURI(path));
document.forms[0].submit();
action
request.getParameter("path");


2. set val into form hidden;
document.getElementById("path").value = path;
<input type="hidden" value="" id="path" />

action
private String path;

setter/getter;、



答案了 --------------------编程问答-------------------- --------------------编程问答-------------------- 除了楼上说的,还可以使用异步提交。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,