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)