[英]how to call java controller method from jsp using ajax
i have implemented different solution of this problem but i am unable to access the controller method from jsp some solution that i have used are following 我已经实现了这个问题的不同解决方案,但是我无法从jsp访问控制器方法,下面是我使用过的一些解决方案
<script type="text/javascript">
var entryId = arguments[0];
function callMetod()
{
<%!public void clickBtn() {
ControllerJSP controller = new ControllerJSP();
controller.likePicture();
}%>
}
</script>
solution 2 is 解决方案2是
<script type="text/javascript">
var entryId = arguments[0];
function callMetod()
{
var entryId = arguments[0]
$.post( "../controllerJSP/LikePicture", {eid: 232 })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
</script>
Solution three that i can implemented is 我可以实施的解决方案三是
<script type="text/javascript">
function callMetod()
{
`
var f={};
var response = "4292";
f.url = "../controllerJSP/LikePicture";
f.type = "POST";
f.dataType = "json";
f.data={eId:entryId};
f.contentType = "application/json";
f.success = function (entryId) {
console.log(json)
alert("success");
};
f.error = function (){
alert("failed");
};
$.ajax(f);
alert("run")
}
</script>
please help me to resolve this issue 请帮助我解决此问题
i will find a solution to call the method inside jsp from controller put an annotation requestmapping and assign a name as shown below and call this annotation in your ajax url like shown below 我将找到一种从控制器调用jsp内方法的解决方案,将其放入注释requestmapping并分配一个名称,如下所示,并在您的ajax url中调用此注释,如下所示
@RequestMapping("likePicture")
@ResponseBody
public void likePicture(@RequestParam("eId") String eId, Map<String, Object>map)
{
List<LikeCount> likeCounts = new ArrayList<>();
likeCounts = dal.getLikeCount(eId);
SimpleResponseModel srm = new SimpleResponseModel();
srm = dal.likePicture(eId, "n@n.com");
dal.updateLikeCount(srm.getMessage(), eId);
System.out.println(srm.getMessage());
}
ajax method is shown here 这里显示了ajax方法
function likeImg(id, votes)
{
var btn = document.getElementById("likeBtn");
debugger
$.ajax({
url : "../likePicture",
type : "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
data : {eId:id, vote:votes},
success : function(responce) {
alert("ok")
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.