学习笔记_springmvc返回值、数据写到页面、表单提交、ajax、重定向
数据写到页面
后台往前台传数据
TestController添加
/**
* 方法的返回值采用ModelAndView, new ModelAndView("index", map);,
* 相当于把结果数据放到request里面
* @return
* @throws Exception
*/
@RequestMapping("/toPerson4.do")
public ModelAndView toPerson4() throws Exception{
Person person = new Person();
person.setName("jerome");
person.setAge(22);
person.setAddress("nanan");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-12-21");
person.setBirthday(date); Map<String,Object> map = new HashMap<String, Object>();
map.put("p", person);
return new ModelAndView("jsp/index",map);
}
页面接收:index.jsp
<body>
<h5>${p.name }</h5>
<h5>${p.age }</h5>
<h5>${p.address }</h5>
<h5><fmt:formatDate value="${p.birthday }" pattern="yyyy-MM-dd"/></h5>
</body>
在jsp引入fmt标签库
* 文章包含被禁用的url,无法保存和发布。
太坑了,这个链接也屏蔽~
重启tomcat访问:
输出信息正确;
另外一种方式:
/**
* 直接在方法的参数列表中来定义Map,这个Map即使ModelAndView里面的Map
* 由视图解析器统一处理,统一走ModelAndView的接口
* 也不建议使用
* @param map
* @return
* @throws Exception
*/
@RequestMapping("/toPerson5.do")
public String toPerson5(Map<String,Object> map) throws Exception{
Person person = new Person();
person.setName("jerome");
person.setAge(22);
person.setAddress("nanan");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-12-21");
person.setBirthday(date); map.put("p", person);
return "jsp/index";
}
重启tomcat访问:
http://localhost:8080/springmvc-2/test/toPerson5.do
输出正确;
建议使用方式:
/**
*在参数列表中直接定义Model,model.addAttribute("p", person);
*把参数值放到request类里面去,建议使用
* @param map
* @return
* @throws Exception
*/
@RequestMapping("/toPerson6.do")
public String toPerson6(Model model) throws Exception {
Person person = new Person();
person.setName("jerome");
person.setAge(22);
person.setAddress("nanan");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-12-21");
person.setBirthday(date);
//把参数值放到request类里面去
model.addAttribute("p", person);
return "jsp/index";
}
重启tomcat访问:
http://localhost:8080/springmvc-2/test/toPerson6.do
输出正确数据;
不需要页面跳转:ajax
在TestController加
/**
* ajax的请求返回值类型应该是void,参数列表里直接定义HttpServletResponse,
* 获得ProntWriter的类,最后可把结果写到页面
* 不建议使用
* @param name
* @param response
*/
@RequestMapping("/ajax.do")
public void ajax(String name, HttpServletResponse response) {
String result = "hello " + name;
try {
response.getWriter().write(result);
} catch (IOException e) {
e.printStackTrace();
}
}
前台调用 新建ajax.jsp
<input id="myButton" type="button" value="click">
Webroot新建js文件夹将jquery拷贝进来;
引进来jquery 和写js脚本:
<script type="text/javascript">
$(function(){
$("#myButton").click(function(){
$.ajax({
url:"test/ajax.do",
type:"post",
dataType:"text",
data:{
name:"zhangsan"
},
success:function(responseText){
alert(responseText);
},
error:function(){
alert("system error");
}
});
});
});
</script>
后台写一个转发:
@RequestMapping("/toAjax.do")
public String toAjax() {
return "jsp/ajax";
}
重启tomcat 访问
http://localhost:8080/springmvc-2/test/toAjax.do
Click,弹出hello zhangsan,成功;
以上方法不建议使用,建议使用:
/**
* 直接在参数的列表上定义PrintWriter,out.wrote(result);
* 把结果写到页面,建议使用
* @param name
* @param out
*/
@RequestMapping("/ajax1.do")
public void ajax1(String name, PrintWriter out) {
String result="hello1 "+name;
out.write(result);
}
修改ajax.jap页面的,js脚本,跳转的url是
url:"test/ajax1.do",
重启tomcat 访问
http://localhost:8080/springmvc-2/test/toAjax.do
Click
弹出 hello1 zhangsan;
表单:
拷贝一份index,起名form.jsp
<body>
<form action="test/toPerson7.do" method="post">
name:<input name="name" type="text"><br/>
age:<input name="age" type="text"><br/>
address:<input name="address" type="text"><br/>
birthday:<input name="birthday" type="text"><br/>
<input type="submit"><br/>
</form>
TestController
@RequestMapping("/toPerson7.do")
public String toPerson7(Person person) {
System.out.println(person);
return "jsp/index";
}
重启tomcat 访问:
http://localhost:8080/springmvc-2/test/toForm.do
提交跳转到
http://localhost:8080/springmvc-2/test/toPerson7.do
控制台输出
Person [name=aa, address=asdf, birthday=Tue Jun 03 00:00:00 CST 2014, age=22]
请求方式的指定:
后台可以指定提交方法,如果前台不是用的同一种提交方式 将报错;
/**
* @RequestMapping(value="/toPerson7.do",method=RequestMethod.POST)
* 可以指定请求方式,前台就必须要以它制定好的方式来访问,不然会出现405错误
* @param person
* @return
*/
@RequestMapping(value="/toPerson7.do",method=RequestMethod.POST)
public String toPerson7(Person person) {
System.out.println(person);
return "jsp/index";
}
Form.jap的method修改为get和post测试;
重定向:
同一个 controller
/**
* 重定向:controller内部重定向,redirect:加上同一个controller中
* 的requesMapping的值
* @return
*/
@RequestMapping("/redirectToForm.do")
public String redirectToForm() {
return "redirect:toForm.do";
}
重启tomcat 访问:
http://localhost:8080/springmvc-2/test/redirectToForm.do
重定向到
http://localhost:8080/springmvc-2/test/toForm.do
Controller之间的重定向:
拷贝一份TestController改成TestController1
留这个
@Controller
//用来标注当前类是springmvc的控制层的类
@RequestMapping("/test1")
//controller的唯一标识或者命名空间
public class TestController1 { @RequestMapping("/toForm.do")
public String toForm() {
return "jsp/form";
} }
TestController 添加
/**
* controller之间的重定向:必须要指定好controller的命名空间再
* 指定requestMapping的值,redirect:后必须要加/,是从根目录开始,
* 否则就从当天test找了
* @return
*/
@RequestMapping("/redirectToForm1.do")
public String redirectToForm1() {
return "redirect:/test1/toForm.do";
}
重启tomcat 访问
http://localhost:8080/springmvc-2/test/redirectToForm1.do http://localhost:8080/springmvc-2/test/redirectToForm1.do
重定向到
http://localhost:8080/springmvc-2/test/toForm.do
学习笔记_springmvc返回值、数据写到页面、表单提交、ajax、重定向的更多相关文章
- HTML+CSS学习笔记(5)- 与浏览者交互,表单标签
HTML+CSS学习笔记(5)- 与浏览者交互,表单标签 1.使用表单标签,与用户交互 网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务 ...
- Netty学习笔记(一):接收nodejs模拟表单上传的文件
好久不写博客了,也好久不写代码了,这两天临时遇上一个事情,觉得不难,加上觉得手有些生,就动手做了一下,结果遇上了不少坑,有新坑,有老坑,痛苦无比,现在总算差不多了,赶紧记录下来,希望以后不再重复这种痛 ...
- python基础学习笔记——方法返回值
字符串中(需要有变量接收) 判断是不是阿拉伯数字,返回的是布尔值 1 2 3 4 name = 'alexdasx' new_name = name.isdigit() print(new_name) ...
- java学习笔记4——返回值
这个简单,返回值就是计算结果. 打个比方:个表格中我只要结果,不要经过,这个返回值就是结果.这个过程就是函数. 另外还有一个函数套用一个函数,被套用的函数的结果作为一个返回值给套用的外层函使用.比如: ...
- HTML5 学习笔记(二)——HTML5新增属性与表单元素
目录 一.HTML5新增属性 1.1.contextmenu 1.2.contentEditable 1.3.hidden 1.4.draggable 1.5.data-* 1.6.placehold ...
- 向后台提交数据:通过form表单提交数据需刷新网页 但通过Ajax提交数据不用刷新网页可通过原生态Ajax或jqueryAjax。Ajax代码部分
原生态Ajax提交表单:需要借助XMLHttpRequest对象的open,要收通过post发送请求还要setRequsetHeader,然后把数据发送给后端,代码如下 目录结构 index.py代码 ...
- php中max_input_vars默认值为1000导致多表单提交失败
公司内一个php的后台管理系统,之前运行在apache上,后来我给转到nginx+php上后,其他功能运行正常,有一个修改功能提交表单后没有提交成功,查了代码没查出来什么问题,后来看了下php err ...
- ajax的表单提交,与传送数据
ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: ...
- springmvc返回值、数据写到页面、表单提交、ajax、重定向
实验是在前一篇文章的项目上做的: 数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView(" ...
随机推荐
- ReactiveCocoa源码拆分解析(二)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 上面抽丝剥茧的把最主要的信号机制给分离开了.但在RA ...
- 非常简单的升级phpnow自带的php版本到5.3的方法
下载PHP5.3 下载地址百度一下“PHP5.3下载”就有很多网站提供下载的,也可以去官网下载最新的,记得下载ZIP压缩的噢 2 解压与命名 将下载的PHP5.3压缩包解压至phpnow的安装目录,命 ...
- maven项目打包成可执行的jar
编写功能类: package com.hpay.FileToZkUtil; import java.io.File; import java.io.FileInputStream; import ja ...
- Mysqle 常用 函数
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. select 可以用作输出 ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL ...
- (2016弱校联盟十一专场10.3) A.Best Matched Pair
题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<stack> ...
- SpringMVC与Ajax交互
1 springmvc和ajax的交互 1.1 请求字符串响应json 客户端发送的数据:key=value&key1=value1 响应回来:json 1.1.1json的支持jar包 1 ...
- socket通信之eofexception
方案一: 用read()将不会抛出异常.因为read是block方式的. readInt()抛出异常的原因(我遇到的)是对方在该端口没有消息发送. 方案二: socket的端口被阻塞了 ,也就是我们需 ...
- android用户界面之Gallery3D学习资料汇总
一.Gallery之根蒂根基教程1.Android Gallery与衍生BaseAdapter容器 http://www.apkbus.com/android-6249-1-1.html 2.Andr ...
- 浅析python 中__name__ = '__main__' 的作用
引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- linux shell 常用指令
1. man 对你熟悉或不熟悉的命令提供帮助解释 eg:man ls 就可以查看ls相关的用法 注:按q键或者ctrl+c退出,在linux下可以使用ctrl+c终止当前程序运行. 2. ls 查看目 ...