1. 程式人生 > >SSM框架下,Jsp頁面提交請求及action獲取值得問題

SSM框架下,Jsp頁面提交請求及action獲取值得問題

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);