页面ajax交互,通过地址url先找到服务器,从数据库取值,去掉逗号,再传给页面

来源:互联网 发布:windows 组播 编辑:程序博客网 时间:2024/04/28 22:53

/**

* 页面ajax交互,通过地址url先找到服务器,从数据库取值,去掉逗号,再传给页面

*/


前端页面代码:

在<html>标签上面:

    <c:set var="ctx" value="${pageContext.request.contextPath}" />

JS脚本语言:

    当然,需要在某一个标签中定义这样一个函数,或者在页面一加载函数中定义这个函数。

    function showImage(){

$.ajax({

url:"${ctx }/app/inspection/sendImage?codeStr=${requestScope.basicInfo.code}",

type:"post",

data:null,

dataType:"text",

success:function(result){

/* alert(result); */

//业务逻辑代码

var image = [];
var maleCount = 0;
var feMaleCount = 0;
var sex = "${requestScope.basicInfo.gender}";
/* alert(sex); */
if(result != null){
image = result.split(",");
for(var i = 0; i<image.length; i++){
var aStr = image[i].substring(image[i].lastIndexOf("_"),image[i].lastIndexOf("."));
if(aStr == "_male"){
maleCount += 1;
}else if(aStr == "_female"){
feMaleCount += 1;
}
}

}

},

error:function(){

alert("系统异常,预览失败");

}

});

    }


服务器端代码:


@Controller

@RequestMapping("/app/inspection")

public class InspectionController{


@RequestMapping("/sendImage")

@ResponseBody

public String sendLisImageUrl(String codestr,HttpServletRequest request,HttpServletResponse response,Model model) throws UnsupportedEncodingException{

/**对页面传过来的"codestr"进行转码*/

String code = new String(codestr.getBytes("iso-8859-1"), "utf-8");

/**根据code查找image路径*/
String image;

try {

/**从数据库中取值*/

Clinical ci = clinicalSrv.selectByClinicalCode(code);//Spring+SpringMVC+MyBatis框架,后台的sql,JavaBean,Service那都简单
String str = ci.getImage();
logger.info("图片路径地址:..."+str);
/**去掉逗号*/

image = str.substring(0, str.length()-1);

/**向页面传递数据*/

model.addAttribute("image", image);
} catch (Exception e) {
throw new RuntimeException("查找image图片路径失败",e);
}
return image;

}

}

仅以此文章记录自己“数据从前端传到后台,经过处理,再从后台传到前端”的思路和代码,以后,遇到同样的功能时,可以照搬!

0 0