springmvc中以一个页面作为响应返回给ajax
2014-12-07 21:05
363 查看
springmvc配置这里不写了直接上代码
下面是jsp请求页面
下面是处理请求的contoller
下面是jsp请求页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <%-- ${pageContext.request.contextPath } --%> <script type="text/javascript" src="<%=request.getContextPath() %>/resource/js/jquery-1.11.1.js"></script> <title>Insert title here</title> <style type="text/css"> #div1{ border: 1px solid gray; text-align: center; height: 600px; width:400px; margin:0 auto; } </style> <script type="text/javascript"> function findUser() { $.ajax({ //这是$.ajax()方法 type:"POST", url:"/1206homework/find", data: $('#myform').serialize(),//ajax提交表单 dataType:"html",//接受响应的数据类型,我的响应是一个页面,所以这里用“html” success:function(data){ $('#mydiv').html(data);//我把响应的页面被放到一个div中显示 } }) } </script> </head> <body > <div id="div1"> <form id="myform" method="post" action="/1206homework/find"> <input type="text" name="username" id="username" /> <input type="button" value="查询" onclick="findUser()" /><br/><br/> </form> <hr/> <div id="mydiv"></div> </div> </body> </html>
下面是处理请求的contoller
package com.leo.controller; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.leo.entity.User; import com.leo.service.UserServiece; @Controller public class MyController { private UserServiece us; public void setUs(UserServiece us) { this.us = us; } @ResponseBody//作用是将返回的对象作为响应,发送给页面 @RequestMapping("/find") public ModelAndView findUsers(String username,ModelAndView m){ setUs(new UserServiece()); List<User> users = us.findUsers(username); m.addObject("users", users); m.setViewName("showUser"); return m; } }showUser是一个页面,显示数据,(我要把这个页面作为响应,返回给发送请求的那ajax页面)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <%-- ${pageContext.request.contextPath } --%> <script type="text/javascript" src="../resource/js/jquery-2.1.1.min.js"></script> <title>Insert title here</title> <style type="text/css"> table{ border: 1px solid gray; } td,th{ border:1px solid gray; width: 60px; } </style> </head> <body > <div > <table > <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>地址</th> </tr> <c:forEach items="${users }" var="user"> <tr> <td>${user.id }</td> <td>${user.username }</td> <td>${user.age }</td> <td>${user.address }</td> </tr> </c:forEach> </table> </div> </body> </html>
相关文章推荐
- springmvc中以一个页面作为响应返回给ajax
- Ajax请求返回一个页面,实现页面局部刷新
- 每次返回上一个页面让ajax重新请求的一句代码
- 爬虫 抓取数据后 显示到前台页面,jsoup包,ajax,设置一个返回ResponseResult结果集
- SpringMVC 方法里返回json或跳到一个页面
- 利用ajax返回一个页面
- 同一个页面,发出ajax请求,经过controller,返回ModelAndView,数据显示不了
- 关于springmvc的controller返回页面无法跳转,使用ajax
- jsp页面的ajax和form表单提交后接收数据的区别(SpringMVC控制器返回Map对象)
- 如何通过JQ,Ajax来实现返回到上一个页面并进行刷新
- Yii:如何在CGridView列表控件使用了Ajax方式更新数据的情况下返回前一个页面
- 本文使用springMVC和ajax,实现将JSON对象返回到页面
- SpringMVC响应Ajax请求(@Responsebody注解返回页面)
- SpringMVC学习系列(3) 之 URL请求到Action的映射规则 在系列(2)中我们展示了一个简单的get请求,并返回了一个简单的helloworld页面。本篇我们来学习如何来配置一个acti
- ajax页面跳转(后台返回的是一个url地址,或者自己传进去的是url地址)
- 返回到前一个页面时显示前一个页面中ajax获取的数据
- 使用ajax时,需要返回一个页面的写法
- 关于SpringMVC 中服务器端如何响应 AJAX 请求(即controller是如何返回json的)
- 一个异步ajax的返回结果作为另一个ajax的入参
- 利用ajax返回一个页面