Spring Json View项目为Spring MVC扩展了Ajax功能
1.将spring-json.jar和sojo.jar文件放入类路径;
2.为spring mvc 配置支持Ajax的视图解析器:
<!-- View Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver" >
<property name="order" value="1"/>
</bean>
为什么是两个?一个给jsp用,另一个给需要将返回的model转换成Json字符串的Ajax视图用.
3.像为jsp编写控制器那样,该怎么写还怎么写:
public class SimpleJsonGetController implements Controller {
private ProfileService profileService;
public void setProfileService(ProfileService profileService) {
this.profileService = profileService;
}
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
String roleId=(arg0.getParameter("roleId")!= null)?arg0.getParameter("roleId"):"1";
List select2=profileService.getRemainedTopicsByRole(Integer.parseInt(roleId));
List select3=profileService.getTopicsByRole(Integer.parseInt(roleId));
HashMap map=new HashMap();
map.put("select2", select2);
map.put("select3", select3);
//map->json的转换是自动的
return new ModelAndView("hello",map);
}
4.jsp 文件中引用的js文件如下:
$(document).ready(function(){
//jQuery test
$("a").click(function(event){
alert("as you can see, the link no longer took you to jquery.com");
event.preventDefault();
});
//button test
$('#getName').click(function(){
$.getJSON('hello.htm', function(data) {
$('#username').html( data.username );
$('#selectfrom').empty();
var users = data.users;
$.each(users,function(index,value){
$("#selectfrom")[0].options.add(new Option(value.username,value.userId,false,false));
});
});
});
$('#select1').change(function() {
var rid=$('#select1').val();
$.getJSON('hello.htm?roleId='+rid, function(data) {
$('#select2').empty();
$('#select3').empty();
var select2 = data.select2;
var select3 = data.select3;
$.each(select2,function(index,value){
$("#select2")[0].options.add(new Option(value.topicName,value.topicId,false,false));
});
$.each(select3,function(index,value){
$("#select3")[0].options.add(new Option(value.topicName,value.topicId,false,false));
});
});
});
//button click event
$('#authorize').click(function(){
$.post("profile.htm", $("#form1").serialize());
alert("OK");
});
});
Js程序调用hello.htm返回结果如下(控制器所返回的Json格式的模型map):
{"username":"张三","users":[{"username":"root","password":"spk321","userId":"43","roleId":"33"},{"username":"Spongebob","password":"hmbb123","userId":"44","roleId":"33"}]}
分享到:
相关推荐
Spring MVC与Ajax交互源码,详细解读参考 http://blog.csdn.net/quincylk/article/details/51423297
Spring + Hibernate + Spring mvc +Ajax 整合 框架,一个简单的添加删除。
NULL 博文链接:https://xkorey.iteye.com/blog/726146
一个实用了spring mvc和ajax异步请求的例子
文件上传demo:前台ajax,后台spring mvc,效率好于传统表单上传。
十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-driven /> 到底做了什么工作 二十、...
十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-driven /> 到底做了什么工作
十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八、spring mvc 多视图控制器 十九、 <mvc:annotation-driven /> 到底做了什么工作 二十...
ajax导出xml调用浏览器下载功能; ajax导入文件说明及注意事项
基于spring mvc注解开发的ajax项目实例,通过对注解@ResponseBody的应用,实现ajax局部刷新功能,java代码端返回Map,Integer,User,List等对象,在jsp页面通过$.each(data,function(index,entity) { });...
7-Spring-mvc-ajax支持1
Ajax With Spring 3. Eclipse 工程,包含Web所需要的 所有jar包。 1> ajax 请求。 2> spring 3注解使用 3> mvc:annotation
系统前台与后台的交互全部使用 Ajax 异步请求 06. 自定义 SecureValid 注解实现权限的控制 07. 通过自定义 ExcludeLog 注解来实现对日志的记录进行排除 08. Spring mvc 返回数据格式采用统一的对象(JSONReturn...
用户登录增删改查的小项目,但包括前台界面设计,js,jQuery,ajax调后台,获取返回结果在前台界面用JS代码中处理,轻量实用适合新手入门项目开发。
spring example 用ajax来做得spring 添加账号功能,在spring官网上有
SpringMVC+Ajax异步文件上传+短视频背景+a标签绑定文件域
三期答辩课程设计,人事管理系统Mybatis+Spring+Spring MVC框架整合AJAX+JQuery实现登录、注册以及验证码和验证注册用户信息,包括数据库备份,课设说明,答辩PPT,Windows下sts(eclipse) 。
Table of Contents Configuring a Spring Development Environment Spring ...AJAX Resolving and Implementing Views Testing Spring MVC Applications Spring Web Flow Building Applications with Spring Web Flow ...
spring使用ajax的方法 还有mvc的上传下载
如果你使用Spring进行开发,那可能想知道(或者有人曾问过你)Spring MVC与JAX-RS有何异同点?更进一步,如果你手头有一个Spring MVC应用,使用了控制类继承(SimpleFormController等),你可能还意识不到现在的...