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>
* 文章包含被禁用的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";
}
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";
}
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";
}
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);
}
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";
}
重定向:
同一个 controller
/**
* 重定向:controller内部重定向,redirect:加上同一个controller中
* 的requesMapping的值
* @return
*/
@RequestMapping("/redirectToForm.do")
public String redirectToForm() {
return "redirect:toForm.do";
}
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 访问
springmvc返回值、数据写到页面、表单提交、ajax、重定向的更多相关文章
- SpringMVC中使用bean来接收form表单提交的参数时的注意点
这是前辈们对于SpringMVC接收表单数据记录下来的总结经验: SpringMVC接收页面表单参数 springmvc请求参数获取的几种方法 下面是我自己在使用时发现的,前辈们没有记录的细节和注意点 ...
- Servlet--超链接,表单提交,重定向,转发4种情况的路径
实际编码中我们经常写路径,写路径既可以写相对路径,也可以写绝对路径.我2年以前我就养成了习惯,只要是写路径我从来都是写绝对路径,因为万一将来我们的项目的目录发生变化,原来要是写相对路径的话就会有路径依 ...
- WEB-INF目录下登录表单提交的重定向
问题描述 登陆表单提交跳转后刷新会重新提交表单,但是使用重定向时不走视图解析器,不能访问WEB-INF下的资源 解决方法 原方法 @RequestMapping(value = "/logi ...
- 向后台提交数据:通过form表单提交数据需刷新网页 但通过Ajax提交数据不用刷新网页可通过原生态Ajax或jqueryAjax。Ajax代码部分
原生态Ajax提交表单:需要借助XMLHttpRequest对象的open,要收通过post发送请求还要setRequsetHeader,然后把数据发送给后端,代码如下 目录结构 index.py代码 ...
- php中max_input_vars默认值为1000导致多表单提交失败
公司内一个php的后台管理系统,之前运行在apache上,后来我给转到nginx+php上后,其他功能运行正常,有一个修改功能提交表单后没有提交成功,查了代码没查出来什么问题,后来看了下php err ...
- 八 SpringMVC文件上传,必须设置表单提交为post
1 修改Tomcat配置,本地目录映射 那么在server.xml中体现为: 测试一下是否设置成功: 2 引入jia包 3 配置多媒体解析器 3 jsp开启图片上传 4 Controller层设置 ...
- jQueryValidate的表单提交ajax刷新代码
$("#form-member-add").validate({ rules:{ username:{ required:true, minlength:2, maxlength: ...
- 表单提交---前端页面模拟表单提交(form)
有些时候我们的前端页面总没有<form></form>表单,但是具体的业务时,我们又必须用表单提交才能达到我们想要的结果,LZ最近做了一些关于导出的一些功能,需要调用浏览器默认 ...
- ajax的表单提交,与传送数据
ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: ...
- input file 在开发中遇到的问题 类似ajax form表单提交 input file中的文件
最近在做项目的过程中遇到个问题,在这里做个记录防止日后忘记 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为 ...
随机推荐
- POJ2486 Apple Tree(树形DP)
题目大概是一棵树,每个结点都有若干个苹果,求从结点1出发最多走k步最多能得到多少个苹果. 考虑到结点可以重复走,容易想到这么个状态: dp[u][k][0]表示在以结点u为根的子树中走k步且必须返回u ...
- LightOJ1201 A Perfect Murder(树形DP)
一道经典的树型DP入门题.dp[u][0/1]表示u点不选或选时以u为根的子树最多能选择的点数. 题目给的有向有环图可以看作森林,注意不是树,因为题目没有说图是连通的! #include<cst ...
- Period[HDU1358]
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- CentOS6.4 配置DNS服务器
1.安装bind yum install -y bind bind-chroot bind-utis 2.配置named.conf [root@dns /]# vi /etc/named.conf 注 ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- TYVJ P1016 装箱问题 Label:01背包 DP
做题记录:2016-08-15 23:07:04 背景 太原成成中学第2次模拟赛 第三道 描述 有一个箱子容量为v(正整数,o≤v≤20000),同时有n个物品(o≤n≤30),每个物品有一个体积 ( ...
- MySql Replication配置
一.前言 Mysql Replication作为读写分离的廉价解决方案,支持一主多备的方式进行数据存储,采用二进制日志传送,目前存在着广泛应用,网上相关概念也比较多,不再重复介绍.引用一张官方提供的R ...
- java+easyui实例
1.首先引入easyui包 在jsp页面上引用以下文件: <link rel="stylesheet" type="text/css" href=&quo ...
- QCheckBox 的按钮响应
Qt中QCheckBox的按键响应如下,其中checkbox为对象名: void YourClass::on_checkbox_toggled(bool state) { // Do somethin ...
- app.config中的connectionstring
<connectionStrings> <add name="wz" connectionString="server=www.junjv.com ...