mvc中ajax的跳转,springmvc ajax 跳转记录

springmvc ajax  老是忘了

今天花下时间记录下

老是找重复的资料 。。坑。。

第一种

返回值是  Map

控制器写法

/**

* 纠错 问题

* @author admin

*

*/

@RequestMapping(value="/checkQuestion.php")

@ResponseBody

public Map checkQuestion(Long titleId){

Map map = new HashMap();

map.put("ceshi", "后台传回来的纠错");

System.out.println(map.toString());

return map;

}

页面写法一

$('.other1').click(function(){

$.ajax({

url:'/vote/checkQuestion.php',

data:$('#form1').serialize(),// 你的formid

success:function(data){

if(data!=null){

alert("纠错"+data.ceshi);

}

}

});

})

第二种

返回值是  json 的 字符串

/**

* 收藏 问题

* @author admin

*

*/

@RequestMapping(value = "/collectionQuestion.php",produces = "application/json; charset=utf-8")

@ResponseBody

public String collectionQuestion(){

JSONArray array=new JSONArray();

JSONObject json= new JSONObject();

json.put("ceshi", "后台传回来的收藏");

array.add(json);

System.out.println(array.toString());

return array.toString();

}

页面写法二

$('.other2').click(function(){

$.ajax({

url:'/vote/collectionQuestion.php?titleId='+${question.id},

success:function(data){

if(data!=null){

alert("收藏"+data[0].ceshi);

}

}

});

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring MVC,使用return语句可以实现页面跳转。具体来说,可以使用以下两种方式: 1. 返回逻辑视图名 在Controller方法,可以使用return语句返回一个逻辑视图名,Spring MVC会根据这个视图名来查找对应的视图文件,并将其渲染后返回给浏览器。例如: ``` @RequestMapping("/hello") public String hello() { return "hello"; // 返回逻辑视图名 } ``` 在这个例子,当用户访问/hello路径时,Spring MVC会查找名为hello的视图文件,并将其渲染后返回给浏览器。 2. 返回RedirectView或RedirectAttributes 除了返回逻辑视图名外,还可以使用RedirectView或RedirectAttributes来实现页面跳转。RedirectView是一个Spring MVC提供的视图类,可以将请求重定向到另一个URL。例如: ``` @RequestMapping("/redirect") public RedirectView redirect() { RedirectView redirectView = new RedirectView(); redirectView.setUrl("/hello"); // 设置重定向URL return redirectView; } ``` 在这个例子,当用户访问/redirect路径时,Spring MVC会将请求重定向到/hello路径。 另外,还可以使用RedirectAttributes来传递重定向时需要的参数。例如: ``` @RequestMapping("/redirectWithParam") public RedirectView redirectWithParam(RedirectAttributes attributes) { attributes.addAttribute("name", "Tom"); // 设置重定向参数 RedirectView redirectView = new RedirectView(); redirectView.setUrl("/hello"); // 设置重定向URL return redirectView; } ``` 在这个例子,当用户访问/redirectWithParam路径时,Spring MVC会将请求重定向到/hello路径,并将name参数设置为Tom。在/hello路径对应的Controller方法,可以使用@RequestParam注解来获取这个参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值