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

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 添加

estController 添加

/**
   *  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

jquery-1.6.2.js下载:

http://pan.baidu.com/s/1o6nwWP0

时间: 2024-08-10 00:05:19

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

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

这是前辈们对于SpringMVC接收表单数据记录下来的总结经验: SpringMVC接收页面表单参数 springmvc请求参数获取的几种方法 下面是我自己在使用时发现的,前辈们没有记录的细节和注意点: 使用bean来接收form表单提交的参数时,pojo中必须含有默认的(即空的)构造函数,同时,需要设置到bean中的变量必须有setter方法. 注:以下代码均为示例代码,非本人实际运行代码,请自行补充. 例如:我有一个bean类是User,具有变量username和password.同时,表单

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

实际编码中我们常常写路径,写路径既能够写相对路径,也能够写绝对路径.我2年曾经我就养成了习惯.仅仅要是写路径我从来都是写绝对路径,由于万一将来我们的项目的文件夹发生变化.原来要是写相对路径的话就会有路径依赖关系.改的地方太多了.并且相对路径在某些情况下还有点特殊,有的是相对于原来的请求的文件夹,有的是相对于整个web应用,所以我强烈建议大家以后写路径统一用绝对路径(以"/"开头)来写,"/"表示网站的根路径. 写路径的情况无非以下4种情况,这里做一个整理. 1,超链

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

原生态Ajax提交表单:需要借助XMLHttpRequest对象的open,要收通过post发送请求还要setRequsetHeader,然后把数据发送给后端,代码如下 目录结构 index.py代码 1 #index.py 2 #!/usr/bin/env python 3 #-*- coding:utf-8 -*- 4 import tornado.web 5 import tornado.ioloop 6 class indexHandler(tornado.web.RequestHand

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

转载于:http://54im.com/php-2/php-max-input-vars-default-value-1000-fail-post-form.html 公司内一个php的后台管理系统,之前运行在apache上,后来我给转到nginx+php上后,其他功能运行正常,有一个修改功能提交表单后没有提交成功,查了代码没查出来什么问题,后来看了下php error日志,也没有什么线索,打印post请求后,也发现提交表单个数和正在表单个数对不上(当时怀疑过是不是某个插件是不是没装,字符集对不

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

有些时候我们的前端页面总没有<form></form>表单,但是具体的业务时,我们又必须用表单提交才能达到我们想要的结果,LZ最近做了一些关于导出的一些功能,需要调用浏览器默认的下载功能,如果用ajax请求,则无法调用.所以只能用表单提交的方式请求后台方可调用浏览器默认的下载功能.这个时候我们只能自己手动添加<form></form>元素.这里LZ提供我自己遇到的两种情况: 一:在原有的html结构包上<form></form>标签,

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

ajax的表单提交 $.ajax ({ url: "<%=basePath%>resource/addPortDetectOne.action", dataType: "json", method : 'post', data: $("#form_portDetect").serialize(),              //传送表单数据 success : function(data_) { //alert(data_); ale

jquery.form.js(ajax表单提交)

Form插件地址: 官方网站:http://malsup.com/jQuery/form/ 翻译地址:http://www.aqee.net/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started 一.准备工作 写一个表单: <form id="reg" action="123.php" method="post"> <p> <label for

ajax form表单提交 input file中的文件

ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了解决这个问题我走了不少弯路: 1.用原生的 input file , 不支持ajax上传文件,你肯定会说可以用 ajax form表单上传了呀?不过我后面还要调用上传成功后用js处理一些对话框,所以这种方法排除 2.用了 uploadify 上传插件,弄出来能上传东西,结果不理想:因为不能判断上传的

一则由表单提交引发的思考

地址:http://partner.jianyihui2011.com/grade/score_user?period_id=1&proj_id=8 一:发起post请求 二:http请求头-------------提交数据(Form Data) 三:提交请求头---------------------接口返回数据 四:思考 表单提交--------提交成功页跳页面&&表单不需要验证):使用传统的提交方式form元素里使用<input type="submit&qu

Ajax表单提交插件jquery form

jQuery Form插件是一个优秀的Ajax表单插件,我们可以非常容易的使用它处理表单控件的值,清空和复位表单控件,附件上传,以及完成Ajax表单提交. jQuery Form有两个核心方法ajaxForm()和ajaxSubmit(),本文我们重点介绍ajaxSubmit()的应用. HTML 首先我们载入jquery库和jquery.form.js插件.jquery.form.js插件的官网地址:http://www.malsup.com/jquery/form/ <script type