Ajax request and date conversion in SPRINGMVC (ix)

Source: Internet
Author: User
Tags findone

A code example (a jar package that needs to import jquery files and JSON)

  1. Front Page

<HTML>  <Head>    <Scripttype= "Text/javascript"src= "Js/jquery-1.8.3.js"></Script>  <Script>      functionCheck (x) {varuname=X.value; varURL="findonebyuname?t="+NewDate (). GetTime (); $.post (Url,{uname:uname},function(data) {alert (data); })      }  </Script>  </Head>    <Body>  <formAction= "Add"Method= "POST">Name:<inputname= "Uname"onblur= "Check (this)"/><BR>Age :<inputname= "Age" /><BR>Birthday:<inputname= "Birthday" /><BR>      <inputtype= "Submit"value= "Add"/>  </form>  </Body></HTML>

 2. Controller code

@Controller  Public class useraction {    @Autowired    private  userservice userservice;        @RequestMapping (Value= "Findonebyuname", produces= "Text/html;charset=utf-8")    @ResponseBody      Public string Findonebyuname (String uname) {        = userservice.findone (uname);        SYSTEM.OUT.PRINTLN (user);         return json.tojsonstring (user);}    }

3, Spring Core configuration file description (mainly to deal with garbled)

<mvc:resources location= "/js/" mapping= "/js/**"/>    <mvc:annotation-driven>    <mvc: Message-converters register-defaults= "true" >        class= " Org.springframework.http.converter.StringHttpMessageConverter ">          <property name=" Supportedmediatypes " Value= "Text/html;charset=utf-8"/>        </bean>      </mvc:message-converters>     </MVC: Annotation-driven>    

Second, the AJAX request garbled processing

  1, Method one

Controller settings
@Controllerpublicclass useraction { @RequestMapping (value= "Findonebyuname", Produces= "Text/html;charset=utf-8") @ResponseBody public String findonebyuname ( String uname) { = userservice.findone (uname); SYSTEM.OUT.PRINTLN (user); return json.tojsonstring (user);} }

  

Spring Core configuration File Settings
<location= "/js/" mapping= "/js/**"/> <Mvc:annotation-driven>

  2. Method Two 

@RequestMapping ("Findonebyuname")    @ResponseBody    public  String findonebyuname ( String uname) {        = userservice.findone (uname);        SYSTEM.OUT.PRINTLN (user);         return json.tojsonstring (user);    }

  

<mvc:resources Location= "/js/"Mapping= "/js/**"/>    <Mvc:annotation-driven>    <mvc:message-convertersRegister-defaults= "true">        <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter">          < Propertyname= "Supportedmediatypes"value= "Text/html;charset=utf-8"/>        </Bean>      </mvc:message-converters>     </Mvc:annotation-driven>

Third, the date conversion problem in spring MVC

  1. Use annotations to set up

 Public class User {    private  Integer uid;     Private String uname;     Private Integer age;     // Date Issue    when processing is incremented @DateTimeFormat (pattern= "Yyyy-mm-dd")    // date problem @JSONField When processing a query request    ( format= "Yyyy-mm-dd")    private  Date birthday;}

Ajax request and date conversion in SPRINGMVC (ix)

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.