Spring MVC -3 将服务器中数据体现在view ajax 重定向
将服务器返回值体现在view中
通过如下方式:
1,返回一个ModeAndView(String url,Map map)
2,在html读取MAV的数据。
@RequestMapping("/to4.do")
public ModelAndView toPersion4()
{
Persion p = new Persion();
p.setName("ZHANGXU");
p.setAdress("ma a mi");
p.setAge(20);
Map<String,Object> map = new HashMap();
map.put("p", p);
ModelAndView mav= new ModelAndView("jsp1/in",map);
return mav;
}
也可以将函数定义为,实际上视图解析器 会把 map map和string 自动组装成一个MAV返回,也不建议使用...
public String toPersion5(Map<String,Object>)
{
}
建议使用这种
@RequestMapping("/to5.do")
public String toPersion6(Model m)
{
Persion p = new Persion();
p.setName("ZHANGXU");
p.setAdress("ma a mi");
p.setAge(20);
m.addAttribute("p",p);
return "jsp1/in";
}
html中:
<body>
<>
<h1>${p.name} </h1>
<h1>${p.age} </h1>
<h1>${p.adress} </h1>
hello springmvc654465
</body>
访问ajax的内容
/**
*
* desc:直接在参数的列表上定义PrintWriter,out.write(result);把结果写到页面,建议使用的
* author:任亮
* mail:renliangjava@163.com
* qq群:2636378537
* 班级:0922
*/
@RequestMapping("/ajax1.do")
public void ajax1(String name, PrintWriter out){
String result = "hello " +name;
out.write(result);
}
@RequestMapping("/toAjax.do")
public String toAjax(){
return "ajax";
}
*
* desc:直接在参数的列表上定义PrintWriter,out.write(result);把结果写到页面,建议使用的
* author:任亮
* mail:renliangjava@163.com
* qq群:2636378537
* 班级:0922
*/
@RequestMapping("/ajax1.do")
public void ajax1(String name, PrintWriter out){
String result = "hello " +name;
out.write(result);
}
@RequestMapping("/toAjax.do")
public String toAjax(){
return "ajax";
}
ajax.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
//这里
<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
<script type="text/javascript">
$(function(){
$("#mybutton").click(function(){
$.ajax({
url:"test/ajax1.do",
type:"post",
dataType:"text",
data:{
name:"zhangsan"
},
success:function(responseText){
alert(responseText);
},
error:function(){
alert("system error");
}
});
});
});
</script>
<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
<script type="text/javascript">
$(function(){
$("#mybutton").click(function(){
$.ajax({
url:"test/ajax1.do",
type:"post",
dataType:"text",
data:{
name:"zhangsan"
},
success:function(responseText){
alert(responseText);
},
error:function(){
alert("system error");
}
});
});
});
</script>
//这里
</head>
<body>
</head>
<body>
//定义的按钮功能
<input id="mybutton" type="button" value="click">
</body>
</html>
<input id="mybutton" type="button" value="click">
</body>
</html>
表单提交
首先添加标记
一个是提交界面,一个是提交成功的返回界面
@RequestMapping("/form.do")
public String toPersion4(Persion persion)
{
System.out.println(persion);
return "jsp1/form";
}
@RequestMapping(value="/pers0.do",method=RequestMethod.POST) //限制为post方法
public String toPersion3(Persion persion)
{
System.out.println(persion);
return "jsp1/index";
}
接下来是jsp
<body>
<form action="test/pers0.do" method="post">
name:<input name="name" type="text"> <br>
age:<input name="age" type="text"> <br>
address:<input name="adress" type="text"> <br>
<input name="name" type="submit" value="submit"><br>
</form>
</body>
action是接受界面,name=“name” 是与Persion.setName匹配的
重定向
将url重定向
1,在controller内部中
@RequestMapping("/redirect.do")
public String redirect()
{
return "redirect:form.do";
}
2,不同controller中
return "redirect:/test2/form.do";