}
}
(5)Control代码 以下代码我们看到
方法ResultMsg addNsrxx(@RequestBody NsrxxBo nsrxxbo)对应ajax的请求
通过@RequestBody NsrxxBo将提交的json转成对应的bo类,我们可以直接方便的使用bo进行操作
package com.zkxx.reportforms.demo.action;
import java.util.HashMap; import java.util.List; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.zkxx.base.SpringContextUtil; import com.zkxx.base.ResultMsg;
import com.zkxx.reportforms.demo.bo.MxBo; import com.zkxx.reportforms.demo.bo.NsrxxBo;
import com.zkxx.reportforms.demo.service.DjnsrxxService;
@Controller
public class DjNsrxxAction {
private DjnsrxxService djnsrxxService;
public DjnsrxxService getDjnsrxxService() { }
@Autowired//自动注入service
public void setDjnsrxxService(DjnsrxxService djnsrxxService) { }
this.djnsrxxService=djnsrxxService;
return this.djnsrxxService;
}
@RequestMapping(\//对应ajax请求的url public @ResponseBody
ResultMsg addNsrxx(@RequestBody NsrxxBo nsrxxbo) throws Exception { }
@RequestMapping(\public @ResponseBody
ResultMsg queryNsrxxByNsrsbh(@RequestParam(\ System.out.println(nsrsbh); }
System.out.println(\
ResultMsg result=getDjnsrxxService().getInfoBysbh(nsrsbh); System.out.println(result.getRows().toString()); return result; ResultMsg ret; try{
ret=getDjnsrxxService().saveInfo(nsrxxbo);
}catch(Exception e){ }
return ret;
ret=new ResultMsg(); ret.setFhjg(\ret.setFhsm(\保存失败\e.printStackTrace();
(6)Service接口
package com.zkxx.reportforms.demo.service;
import com.zkxx.base.ResultMsg;
import com.zkxx.reportforms.demo.bo.NsrxxBo; public interface DjnsrxxService { }
ResultMsg saveInfo(NsrxxBo bo) throws Exception; ResultMsg getInfoBysbh(String sbh);
(7)Service接口实现
主要对数据库的操作不再累述
package com.zkxx.reportforms.demo.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.zkxx.base.ResultMsg; import com.zkxx.db.DbExecuter;
import com.zkxx.reportforms.demo.bo.MxBo;
import com.zkxx.reportforms.demo.bo.NsrxxBo;
import com.zkxx.reportforms.demo.service.DjnsrxxService; @Service @Transactional
public class DjnsrxxServiceImpl implements DjnsrxxService{
public ResultMsg getInfoBysbh(String sbh) { }
@Transactional(rollbackFor = Exception.class)//事务回滚 public ResultMsg saveInfo(NsrxxBo bo) throws Exception{
// TODO Auto-generated method stub ResultMsg result=new ResultMsg();
int ret=DbExecuter.getInstance().addPojo2Db(bo,\if(ret<=0){ }
List
mx.setNsrsbh(bo.getNsrsbh()); mx.setNsrmc(bo.getNsrmc());
ret=DbExecuter.getInstance().addPojo2Db(mx,\if(ret<=0){ }
result.setFhjg(\result.setFhsm(\保存失败\return result; result.setFhjg(\result.setFhsm(\保存失败\return result;
// TODO Auto-generated method stub
String sql=\Object[] arr={sbh};
List
ResultMsg result=new ResultMsg(); result.setRows(list); return result;
String nsrsbh=bo.getNsrsbh(); Object[] arrmx={nsrsbh};
sql=\
List
}
}
}
return result;
至此一个简单的保存查询功能完成。
5.文件上传
(1)页面元素
对应的js
function ajaxFileUpload(){ }
fnm=$('#myfile').val();
fnm=fnm.substring(12,fnm.length); $.ajaxFileUpload({ });
//处理文件上传操作的服务器端地址
url:'/TestWeb/uploadfile/upload.do',
secureuri:false, //是否启用安全提交,默认为false fileElementId:'myfile', //文件选择框的id属性
dataType:'text', //服务器返回的格式,可以是json或xml等 success:function(data){ //服务器响应成功时的处理函数 },
error:function(data,e){ //服务器响应失败时的处理函数 }
alert('上传失败,请重试!!'+e); alert(data);
(2)对应的服务类
package com.zkxx.reportforms.demo.action;
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import com.zkxx.base.User;
/**
* 功能:文件上传 * @author ydzhz * */
@Controller
publicclass UploadFilesAction {
@RequestMapping(\)
public String upLoadFiles(@RequestParam MultipartFile[] files,
//设置响应给前台内容的数据格式
response.setContentType(\); response.setCharacterEncoding(\); //设置响应给前台内容的PrintWriter对象 PrintWriter out = response.getWriter();
HttpServletRequest request, HttpServletResponse response) throws IOException{
for(MultipartFile myfile : files){
if(myfile.isEmpty()){
System.out.println(\文件未上传\); out.println(\文件未上传\);
}else{
System.out.println(\文件长度: \ + myfile.getSize());
System.out.println(\文件类型: \ + myfile.getContentType()); System.out.println(\文件名称: \ + myfile.getName());
System.out.println(\文件原名: \ + myfile.getOriginalFilename()); String realPath = \; try{
//不需进度直接存储
//FileUtils.copyInputStreamToFile(myfile.getInputStream(), new
File(realPath, myfile.getOriginalFilename()));
//若需知道进度
OutputStream os = new
FileOutputStream(realPath+\+myfile.getOriginalFilename());
//方式可自定义,这里把进度保存到用户session里,可以通过ajax访问获得 InputStream is=myfile.getInputStream(); int bytesWritten = 0; int byteCount = -1;
byte[] bytes = newbyte[4096];
while((byteCount=is.read(bytes))!=-1){
os.write(bytes,0,byteCount); bytesWritten += byteCount;
User user=(User)request.getSession().getAttribute(\); String
jd=String.valueOf(Math.ceil((double)bytesWritten*100/(double)myfile.getSize()));
if(jd.equals(\))
jd=\;
request.getSession().setAttribute(user.getYhdm()+\+myfile.getOriginalFilename(), jd); }
}
returnnull; }
}
}
is.close();
os.close();
}catch(Exception e) { e.printStackTrace();
out.print(\文件上传失败,请重试!!\);
}
out.println(realPath+\+myfile.getOriginalFilename());//返回保存路径 out.flush();