springmvc中以一个页面作为响应返回给ajax

springmvc配置这里不写了直接上代码

下面是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="查询" οnclick="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>

今天重新写了一下这个代码,搭建框架时候,遇到几点问题

1、配置的spring-servlet.xml中忘记加入   

<mvc:resources mapping="/js/*" location="/js/"/>  
造成访问不到js 文件 报404

2、忘记加注解扫描驱动

<mvc:annotation-driven />
导致访问不到controller 中的代码 报404


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值