SPRINGMVC return value, data write to page, form submission, Ajax, redirect

Source: Internet
Author: User

The experiment was done on the project of the previous article;

Data written to page background forward data

TestController add

   The return value of the/** * method is Modelandview, new Modelandview ("index", map), and is equivalent to putting the   result data in the request   * @return   * @ Throws Exception   *  /@RequestMapping ("/toperson4.do") public  Modelandview ToPerson4 () throws exception{ Person person    = new person ();    Person.setname ("Jerome");    Person.setage (a);    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);  }

Page Received: index.jsp

<body>     

  

Introduction of FMT Tag Library in JSP

* The article contains the URL that is disabled and cannot be saved and published.

Too pit, this link is also blocked ~
To restart Tomcat access:

Http://localhost:8080/springmvc-2/test/toPerson4.do
The output information is correct;
Another way:
/**   * Define the map directly in the parameter list of the method, this map even if the map inside the Modelandview is   handled uniformly by the view parser, the Unified Walk Modelandview interface   * is not recommended to use   * @ 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 (a);    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";  }

  

To restart Tomcat access:
Http://localhost:8080/springmvc-2/test/toPerson5.do
The output is correct, the recommended way to use:
/**   * Define Model,model.addattribute directly in the parameter list ("P", person);   * To put the parameter values in the request class, it is recommended to use   * @param map   * @return   * @throws Exception *  /@RequestMapping ("/ Toperson6.do ") Public  String ToPerson6 (model model) throws Exception {person person    = new person ();    Person.setname ("Jerome");    Person.setage (a);    Person.setaddress ("Nanan");    SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");    Date date = Format.parse ("2012-12-21");    Person.setbirthday (date);    Put the parameter value into the request class to    Model.addattribute ("P", person);    return "Jsp/index";  }

  

To restart Tomcat access:
Http://localhost:8080/springmvc-2/test/toPerson6.do
Output the correct data; no page jumps: Ajax backend methods:

In TestController Plus

/**   * Ajax request return value type should be void, parameter list directly define HttpServletResponse,   * Get Prontwriter class, finally can write the results to the page   * Not recommended to use   * @ 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 ()}  }

Foreground call New ajax.jsp
<input id= "MyButton" type= "button" value= "click" >
Webroot New JS folder to copy jquery in;
Introduce jquery and write JS scripts:

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

Write a forward background:

@RequestMapping ("/toajax.do") public  String Toajax () {    return "Jsp/ajax";  }

  

Restart Tomcat Access
Http://localhost:8080/springmvc-2/test/toAjax.do
Click, pop-up Hello Zhangsan, success; The above method is not recommended, recommended use:
/**   * Define PRINTWRITER,OUT.WROTE (Result) directly on the list of parameters;   * To write the results to the page, it is recommended to use   * @param name   * @param out *  /@RequestMapping ("/ajax1.do") public  void Ajax1 ( String name, PrintWriter out) {    string result= "Hello1" +name;    Out.write (result);  }

  

Modify the Ajax.jap page, JS script, jump URL is
URL: "Test/ajax1.do",
Restart Tomcat Access
Http://localhost:8080/springmvc-2/test/toAjax.do
Click
Pophello1 Zhangsan;form:

Copy an index, named 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";  }

To restart Tomcat access:
Http://localhost:8080/springmvc-2/test/toForm.do
Submit Jump to
Http://localhost:8080/springmvc-2/test/toPerson7.do
Console output
person [Name=aa, ADDRESS=ASDF, birthday=tue June 00:00:00 CST, age=22]

Specify the request mode:

Background can specify the method of submission, if the foreground is not used the same way of submission will be error;

/**   * @RequestMapping (value= "/toperson7.do", Method=requestmethod.post)   * Can specify the request method, the front desk must be in its established way to access, Otherwise there will be a 405 error   * @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 modified to get and post test; Redirect: Same controller
/**   * Redirect: Controller internal Redirect, redirect: plus the value of requesmapping in the same controller * @return */  @ Requestmapping ("/redirecttoform.do") public  String Redirecttoform () {    return ' redirect:toForm.do ';  }

  

To restart Tomcat access:
Http://localhost:8080/springmvc-2/test/redirectToForm.do
Redirect to
Redirection between Http://localhost:8080/springmvc-2/test/toForm.do controllers:

Copy a TestController to TestController1
Leave this.

@Controller//is used to annotate the @requestmapping ("/test1")//controller unique identity of the control layer of the current class is SPRINGMVC, or the namespace public class TestController1 {    @RequestMapping ("/toform.do") public  String Toform () {    return "Jsp/form";  }}

TestController add

/**   *  Controller redirection: You must specify the namespace of the controller and then   *  Specify the value of requestmapping, redirect: Must be added/, starting from the root directory,   *  Otherwise, from the day of test found   * @return *  /@RequestMapping ("/redirecttoform1.do") public  String RedirectToForm1 () {    return "redirect:/test1/toform.do";  }

Restart Tomcat Access

SPRINGMVC return value, data write to page, form submission, Ajax, redirect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.