您的位置:首页 > Web前端 > JavaScript

SSM框架下,Jsp页面提交请求及action获取值得问题

2016-05-27 09:01 513 查看
JSP代码:

<form id="formid" action="poiAction!insertMsg.do" method="post">

     <input type="file" id="filepath" name="filepath"/>

     <!-- <input type="hidden" id="filename" name="text"/> -->

</form>

    <input type="submit" value="上传" onclick="insert()"/>

    <input type="button" value="下载">

<script type="text/javascript">

 function insert(){

  //获取选择文件的路径

  //var txt=document.getElementById("filename");

  //txt.value=document.getElementById("filepath").value;

  //alert(txt.value);

  //发送请求

  if(document.getElementById("filepath")!=null){

    document.forms[0].submit();

  }

 };

  </script>

一开始我想的是利用document.getElementById().value来获取路径,alert之后发觉是能获取到的。这里先不考虑不同浏览器的问题。

后来之所以没这样是因为,如果你用的是struts2的话,你这样获取再在action用request.getParameter("filepath");就像是Servlet的用法,根本没体现到Struts2的特点。

所以,直接一个form表单控件把input file标签套住,用document.forms[0].submit();提交

Action类如何获取?

只需要声明filepath(与你input标签的name对应),写个get/set方法就可以直接获取

代码如下:

private String filepath;

 

 public String getFilepath() {

  return filepath;

 }

 public void setFilepath(String filepath) {

  this.filepath = filepath;

 }
//filepath=ServletActionContext.getRequest().getParameter("filename");

  System.out.println(filepath);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: