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

数据写到页面

后台往前台传数据

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. Bridge 桥模式

    之前一直以为桥是简单地沟通几个不同接口,使之能够按照一定流程工作.但重新查了一下解释,才有新解. 对于同一个产品的不同影响因子,如果使用继承的话,则这些影响因子则会按照M1*M2* ... Mn的数量 ...

  2. windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放

    http://blog.csdn.net/wangrenzhu2011/article/details/7732907 (转) 实现方法: 对Manipulation进行抽象化 使不同容器可共用多点缩 ...

  3. 【BZOJ】1003: [ZJOI2006]物流运输trans(SPFA+DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 这题一开始看是不会的额,,,还是看题解了..一开始我觉得不能用最短路啥的,,看了题解发现这是d ...

  4. COJ969 WZJ的数据结构(负三十一)

    WZJ的数据结构(负三十一) 难度级别:D: 运行时间限制:3000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 A国有两个主基站,供给全国的资源.定义一个主基站 ...

  5. 网站地图sitemap.xml的格式

    URL列表—XML格式及规范说明: 标签名称  属性  标签说明  标签类型  标签限制  可选/必选  urlset  / urlset用来标记整个文档的开头  /  /  必选  url  / u ...

  6. imread() not working in OpenCV 2.4.11 Debug mode

    The OpenCV function imread() not working in OpenCV 2.4.11 Debug mode of VS2010 under Win32, the way ...

  7. elasticsearch插件大全

    Elasticsearch扩展性非常好,有很多官方和第三方开发的插件,下面以分词.同步.数据传输.脚本支持.站点.其它这几个类别进行划分. 分词插件 Combo Analysis Plugin (作者 ...

  8. 在HTML中如何把块的边框做成圆角

    adius,就是半径的意思.用这个属性可以很容易做出圆角效果,当然,也可以做出圆形效果.原理很简单,“正方形的内切圆的半径等于正方形边长的一半”.下面就做一个红色的圆. 工具/原料 Adobe Dre ...

  9. Yii源码阅读笔记(十三)

    Model类,集中整个应用的数据和业务逻辑: namespace yii\base; use Yii; use ArrayAccess; use ArrayObject; use ArrayItera ...

  10. C# Xml文件操作,解释见注释

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...