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 * equivalent to putting the result data into request * @return * @throws Exception */@RequestMapping ("/toperson4.do")   PublicModelandview ToPerson4 () throws exception{ person person=NewPerson (); Person.setname ("Jerome"); Person.setage ( A); Person.setaddress ("Nanan"); SimpleDateFormat format=NewSimpleDateFormat ("YYYY-MM-DD"); Date Date= Format.parse ("2012-12-21");        Person.setbirthday (date); Map<String,Object> map =NewHashmap<string, object>(); Map.put ("P", person); return NewModelandview ("Jsp/index", map); }

Page Received: index.jsp

<body>     "${p.birthday} " pattern=" YYYY-MM-DD "/>

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:
/** * To 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, it is not recommended to use the Modelandview interface * @param map * @ret Urn * @throws Exception*/@RequestMapping ("/toperson5.do")   PublicString ToPerson5 (map<string,object>map) throws exception{ person person=NewPerson (); Person.setname ("Jerome"); Person.setage ( A); Person.setaddress ("Nanan"); SimpleDateFormat format=NewSimpleDateFormat ("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;

Recommended Use:
/** * directly define Model,model.addattribute ("P", person) in the parameter list; * To put the parameter values in the request class, it is recommended to use * @param map * @return * @throws Exception*/@RequestMapping ("/toperson6.do")   PublicString ToPerson6 (model model) throws Exception { person person=NewPerson (); Person.setname ("Jerome"); Person.setage ( A); Person.setaddress ("Nanan"); SimpleDateFormat format=NewSimpleDateFormat ("YYYY-MM-DD"); Date Date= Format.parse ("2012-12-21");    Person.setbirthday (date); //Put the parameter values in the request class.Model.addattribute ("P", person); return "Jsp/index"; }

To restart Tomcat access:
Http://localhost:8080/springmvc-2/test/toPerson6.do
Output the correct data;

Do not need page jump: Ajax Backend Method:

In TestController Plus

/** * Ajax request return value type should be void, parameter list directly define HttpServletResponse, * Get Prontwriter class, finally can write results to page * Not recommended * @param name * Param response*/@RequestMapping ("/ajax.do")   Public voidAjax (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;

These methods are not recommended and are recommended to use:
 /*  * * define printwriter,out.wrote directly on the list of parameters (   result); * To write the results to the page, it is recommended to use * @param name * @param out  */  @RequestMapping ( "  /ajax1.do  Span style= "color: #800000;" > " "  public  void  ajax1 (String name, printwriter out   = " 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
Eject hello1 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 foreground must be in its established way to access, otherwise there will be 405 error * @param person * @return  */  @RequestMapping (value  = /toperson7.do  , Method= public   String ToPerson7 (person person) {System.  out     .println (person);  return   "   ; }

The Form.jap method is modified to get and post tests;

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
Http://localhost:8080/springmvc-2/test/toForm.do

Redirection between controllers:

Copy a TestController to TestController1
Leave this.

@Controller//class used to annotate the control layer where the current class is Springmvc@RequestMapping ("/test1")//the controller's unique identity or namespace Public classTestController1 {@RequestMapping ("/toform.do")   PublicString Toform () {return "Jsp/form"; }}

TestController add

Estcontroller 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

Http://localhost:8080/springmvc-2/test/redirectToForm1.do http://localhost:8080/springmvc-2/test/redirectToForm1.do

Redirect to

Http://localhost:8080/springmvc-2/test/toForm.do

Jquery-1.6.2.js Download:
Http://pan.baidu.com/s/1o6nwWP0

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.