实验是在前一篇文章的项目上做的;

数据写到页面

后台往前台传数据

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访问:

http://localhost:8080/springmvc-2/test/toPerson4.do 
输出信息正确;

另外一种方式:

/**
* 直接在方法的参数列表中来定义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 访问

springmvc返回值、数据写到页面、表单提交、ajax、重定向的更多相关文章

  1. SpringMVC中使用bean来接收form表单提交的参数时的注意点

    这是前辈们对于SpringMVC接收表单数据记录下来的总结经验: SpringMVC接收页面表单参数 springmvc请求参数获取的几种方法 下面是我自己在使用时发现的,前辈们没有记录的细节和注意点 ...

  2. Servlet--超链接,表单提交,重定向,转发4种情况的路径

    实际编码中我们经常写路径,写路径既可以写相对路径,也可以写绝对路径.我2年以前我就养成了习惯,只要是写路径我从来都是写绝对路径,因为万一将来我们的项目的目录发生变化,原来要是写相对路径的话就会有路径依 ...

  3. WEB-INF目录下登录表单提交的重定向

    问题描述 登陆表单提交跳转后刷新会重新提交表单,但是使用重定向时不走视图解析器,不能访问WEB-INF下的资源 解决方法 原方法 @RequestMapping(value = "/logi ...

  4. 向后台提交数据:通过form表单提交数据需刷新网页 但通过Ajax提交数据不用刷新网页可通过原生态Ajax或jqueryAjax。Ajax代码部分

    原生态Ajax提交表单:需要借助XMLHttpRequest对象的open,要收通过post发送请求还要setRequsetHeader,然后把数据发送给后端,代码如下 目录结构 index.py代码 ...

  5. php中max_input_vars默认值为1000导致多表单提交失败

    公司内一个php的后台管理系统,之前运行在apache上,后来我给转到nginx+php上后,其他功能运行正常,有一个修改功能提交表单后没有提交成功,查了代码没查出来什么问题,后来看了下php err ...

  6. 八 SpringMVC文件上传,必须设置表单提交为post

    1 修改Tomcat配置,本地目录映射 那么在server.xml中体现为: 测试一下是否设置成功: 2 引入jia包   3 配置多媒体解析器 3 jsp开启图片上传 4 Controller层设置 ...

  7. jQueryValidate的表单提交ajax刷新代码

    $("#form-member-add").validate({ rules:{ username:{ required:true, minlength:2, maxlength: ...

  8. 表单提交---前端页面模拟表单提交(form)

    有些时候我们的前端页面总没有<form></form>表单,但是具体的业务时,我们又必须用表单提交才能达到我们想要的结果,LZ最近做了一些关于导出的一些功能,需要调用浏览器默认 ...

  9. ajax的表单提交,与传送数据

    ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: ...

  10. input file 在开发中遇到的问题 类似ajax form表单提交 input file中的文件

    最近在做项目的过程中遇到个问题,在这里做个记录防止日后忘记 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为 ...

随机推荐

  1. 剪花布条[HDU2087]

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. CodeForces Round 199 Div2

    完了,这次做扯了,做的时候有点发烧,居然只做出来一道题,差点被绿. My submissions     # When Who Problem Lang Verdict Time Memory 443 ...

  3. JavaScript初学者应注意的七个细节

    每种语言都有它特别的地方,对于JavaScript来说,使用var就可以声明任意类型的变量,这门脚本语言看起来很简单,然而想要写出优雅的代码却是需要不断积累经验的.本文利列举了JavaScript初学 ...

  4. Hadoop分布式文件系统:架构和设计要点

    原文:http://hadoop.apache.org/core/docs/current/hdfs_design.html 一.前提和设计目标 1.硬件错误是常态,而非异常情况, HDFS可能是有成 ...

  5. winform学习2-datagridview数据绑定

    1.datagridview.clearSelection()清除默认的选中项 2.列数据显示,首先列必须是显示状态, 3.布局-单元格内文字内容居中显示,示例:外观-defaultCellStyle ...

  6. 【C语言】01-C语言概述

      说明:这个C语言专题,是学习iOS开发的前奏.也为了让有面向对象语言开发经验的程序员,能够快速上手C语言.如果你还没有编程经验,或者对C语言.iOS开发不感兴趣,请忽略 为什么iOS开发要先学C语 ...

  7. ThinkPHP之APP_DEBUG给我带来的问题

    1.刚开始学习Thinkphp,在模块分组之后,自己配置了模块分组后,发现打不开网页了,分组配置如图 2.问题现象如图 在处理空模块时写的函数也不能运行 这时我很困惑,一直检查自己的拼写和配置没发现错 ...

  8. Ubuntu 14.04 使用的一些笔记

    1. 今天下载了安卓的2.3.5的源代码,解压tar的时候,发现一个错误中断了解压过程: could not create the hard link file, 看了一下基本上都是每个主目录下的 . ...

  9. Html - 浮动的云朵

    http://www.17sucai.com/download/9006.html @-webkit-keyframes animate-cloud { from { background-posit ...

  10. Html - 404页面

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...