將數據以json字符串格式傳到前台請求頁面


1.前台ajax方法(這個是在FlowDocAction的add.jsp頁面)

  //序列號
  var preFileNo = factoryCode+deptCode+"-"+modelSeriesCode+"-"+workProcessCode;

  var yz=$.ajax({
  type:'get',
  url:'FlowDocVersion!getSort.shtml?preFileNo='+preFileNo,
  cache:false,
  dataType:'json',
  success:function(data){
    if(data){

    //將json字符串轉換為json對象,因為要通過點的方法來拿它的屬性  eval('('+str+')');
    var obj = eval('(' + data + ')');
    alert(obj.preFileNo);
    }
    },
  error:function(data){
  alert("出錯了");
  }
});

2.后台頁面:因為此處是ssh的action,方法返回String類型默認去找對應頁面,而請求的頁面是另一個action的result對應的頁面,所以此處的

(這里是在FlowDocVersinoAction.java類里)

  //文件編碼
  public String getSort() throws Exception {
  try {
  //查詢所有類似的文件
  String sql="select FileNo from FlowDocVersion where Status <>0 and FileNo like '"+preFileNo+"%'";
  System.out.println("getSort.sql:"+sql);
  List<FlowDocVersion> fileNoList = new FlowDocVersionFacade().find(sql,"FlowDocVersion.FileNo");
  int max = 0 ;
  if(fileNoList.size()==0){//沒有與當前新增文件編碼相同的文件編碼
  preFileNo = preFileNo+"01";

  //將數據以json字符串形式響應到請求頁面start
  HttpServletResponse response=ServletActionContext.getResponse();
  response.setContentType("text/html");
  PrintWriter out;
  out = response.getWriter();
  //將要被返回到客戶端的對象
  JSONObject json=new JSONObject();
  json.accumulate("preFileNo", preFileNo);
  out.println(json.toString());
  out.flush();
  out.close();
  //將數據以json字符串形式傳到請求頁面end
  System.out.println(preFileNo);
  return "";
  }else {
  for(FlowDocVersion e:fileNoList){
  //拿到文件編碼后邊的序號
  String sort = e.getFileNo().substring(preFileNo.length(), e.getFileNo().length());
  if(sort.length()>0){
  //解決 NumberFormatException
  //把最大序號賦給max
  try{
  int i = Integer.valueOf(sort);
  if(max<i){
  max = i;
  }
  }catch (NumberFormatException ep) {
  preFileNo = preFileNo+"01";
  System.out.println(preFileNo);
  }
  }
  }
  max=max+1;//最大序號+1作為新增文件的序號
  if(max<10){//小於10個位用0填補
  preFileNo = preFileNo+"0"+max;
  }else{
  preFileNo = preFileNo+max;
  }
  }
  return null;
  }catch(Exception e) {
  this.setMsg("生成文檔編碼出錯");
  Logger.getLogger(this.getClass()).error("FlowDocVersionAction getSort() Exception", e);
  return ERROR;
  }
  }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM