我有一个问题,
使用spring mvc,我想使用ajax调用,试图调用控制器方法,但我不能,有一个jsp页面和控制器类
我的Jsp页面,
<form role="form" >
<div class="form-group">
<label for="Ders">Ders</label>
<form:select path="derslist" class="form-control" id="ddlders">
<form:options items="${derslerlistesi}" itemValue="dersid"
itemLabel="ders" />
</form:select>
</div>
<div class="form-group">
<label for="KonuAd">Konu Ad</label> <input type="text"
class="form-control" id="KonuAd" placeholder="Konu İsmi Giriniz!">
</div>
<div class="form-group">
<button type="submit" id="btnekle" class="btn btn-default">Ekle</button>
</div>
和我的控制器类
package publisher.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import publisher.dao.Dao;
import publisher.entity.Dersler;
import publisher.entity.Konular;
@Controller
public class IslemlerController {
@Autowired
private Dao dao;
@Autowired
private Konular _konu;
@RequestMapping(value = "/islemler", method = RequestMethod.GET)
public String Home(Model model,Dersler dersler) {
model.addAttribute("derslist",dersler);
return "islemler";
}
@ModelAttribute("derslerlistesi")
public List<Dersler> getdersler ()
{
return dao.getAll(Dersler.class);
}
@ModelAttribute("konulistesi")
public List<Konular> getKonular ()
{
return dao.getAll(Konular.class);
}
@ResponseBody
@RequestMapping(value="/konukaydet",method=RequestMethod.POST)
private Konular KonuKaydet (@RequestParam String konu,@RequestParam int dersid)
{
_konu.setKonu(konu);
_konu.setDersid(dersid);
_konu.setSinavid(1);
_konu = dao.save(_konu);
System.out.println("kaydet e girdi !");
return _konu;
}
}
和我的脚本代码
$(document).ready(function() {
$("#btnekle").click(function() {
var input = {
"konu": $("#KonuAd").val(),
"dersid": $("#ddlders").val()
};
var inputStr = JSON.stringify(input);
alert(inputStr);
$.ajax({
url: "/islemler/konukaydet",
type: "POST",
data: inputStr,
dataType: "json",
success: function(output) { // callback method for further manipulations
var str = JSON.stringify(output);
alert("success ");
$("#output").text(data);
},
error: function(data) { // if error occured
alert("err");
$("#error").text(data);
}
});
});
});
感谢您的帮助!
答案 0 :(得分:0)
我认为你在脚本中有问题,你需要将数据作为查询参数传递,因为你在控制器类中使用了@RequestParam
。所以在你的ajax调用你的网址应该如下。
url: "/islemler/konukaydet?konu="+input.konu+"&dersid="+input.dersid;
注意 - 您未在控制器中使用islemler
网址。 @RequestMapping(value="/islemler/konukaydet",method=RequestMethod.POST)
它可以帮助你,如果没有,请回复我。
如果您想要按原样使用脚本,则需要更改控制器并使用@RequestBody
并使用模型
答案 1 :(得分:0)
要添加@ Sam的观点,根据您在js上的网址,您需要在控制器级别添加@RequestMapping。所以你的控制器应该是这样的:
@RequestMapping(value = "/islemler")
@Controller public class IslemlerController {
因此,通过组合 controller 级别的请求映射和 KonuKaydet 方法级别,您现在可以完成所需的请求映射。
答案 2 :(得分:0)
我假设您已在 dispatcher-servlet.xml中配置控制器
如果不是see this
如果您在本地计算机上运行它,则需要以
调用该调用 $.ajax({
//other options. .
url: http://localhost:<port>/<your-url>
//other options. .
});
。
此外@RequestMapping
应在控制器级别给出。
答案 3 :(得分:0)
在你的@RequestMapping中你应该映射完整的URL在控制器方法或在控制器级添加@RequestMapping像:
@Controller
@RequestMapping("/islemler/")
public class IslemlerController {
@Autowired
private Dao dao;
@Autowired
private Konular _konu;
@RequestMapping(value = "home", method = RequestMethod.GET)
public String Home(Model model,Dersler dersler) {
model.addAttribute("derslist",dersler);
return "islemler";
}
@ModelAttribute("derslerlistesi")
public List<Dersler> getdersler ()
{
return dao.getAll(Dersler.class);
}
@ModelAttribute("konulistesi")
public List<Konular> getKonular ()
{
return dao.getAll(Konular.class);
}
@ResponseBody
@RequestMapping(value="konukaydet",method=RequestMethod.POST)
private Konular KonuKaydet (@RequestParam String konu,@RequestParam int dersid)
{
_konu.setKonu(konu);
_konu.setDersid(dersid);
_konu.setSinavid(1);
_konu = dao.save(_konu);
System.out.println("kaydet e girdi !");
return _konu;
}
}
你的ajax电话应该是这样的:
$(document).ready(function() {
$("#btnekle").click(function() {
var input = {
"konu": $("#KonuAd").val(),
"dersid": $("#ddlders").val()
};
var inputStr = JSON.stringify(input);
alert(inputStr);
$.ajax({
url: "../islemler/konukaydet",
type: "POST",
data: inputStr,
dataType: "json",
success: function(output) { // callback method for further manipulations
var str = JSON.stringify(output);
alert("success ");
$("#output").text(data);
},
error: function(data) { // if error occured
alert("err");
$("#error").text(data);
}
});
});
});
对于您的家庭方法,您的网址就像“../islemler/home”
我希望它可以帮到你。
答案 4 :(得分:0)
解决问题,
脚本代码
function madeAjaxCall(){
var data = {}
data["konu"] = $("#konuad").val()
data["dersid"] = $("#ddlders").val()
$.ajax({
type: "post",
// http://localhost:8080
url: "/islemler/konukaydet",
cache: false,
// data:'konu=' + $("#konuad").val() + "&dersid;=" + $("#ddlders").val(),
data:data,
success: function(response){
$('#result').html("");
var obj = JSON.parse(response);
$('#result').html("First Name:- " + obj.konu +"</br>Last Name:- " + obj.dersid );
},
error: function(){
alert('Error while request..');
}
});
}
和控制器代码
@RequestMapping(value="islemler/konukaydet",method=RequestMethod.POST)
private @ResponseBody Konular KonuKaydet (HttpServletRequest request, HttpServletResponse response)
{
_konu.setKonu(request.getParameter("konu"));
_konu.setDersid(Integer.parseInt(request.getParameter("dersid")));
_konu.setSinavid(1);
_konu = dao.save(_konu);
System.out.println("kaydet e girdi !");
return _konu;
}
非常感谢... :)