Spring annotation @responsebody processing AJAX requests, JSON data types __js

Source: Internet
Author: User
Tags string format

Recently made a Spring+ajax two Cascade menu, always reported a variety of errors, and finally through the analysis and summary finally solve the problem, now the problem to show everyone, for sharing. If you have questions to comment on, definitely support.

JSON needs to refer to the JSON package: Jackson-core-asl-1.9.13.jar,jackson-mapper-asl-1.9.13.jar, the version is not fixed, just the two versions of the same line

Controller layer:

@RequestMapping (value = "Branchmap")
@ResponseBody
Public list<temp> Getbranchmap (HttpServletRequest request) {
String tmp = Request.getparameter ("Organizid");
Long id = long.parselong (TMP);
return Servicemanager.getbranchmap (ID);
}

Service layer:

Public list<temp> Getbranchmap (Long ID) {
return Institutionsdao.getbranchmap (ID);
}

DAO Layer: (Where temp is a temporary class, a class that consists of the fields the Institutions class wants)

Public list<temp> Getbranchmap (Long ID) {
List<temp> lt = new arraylist<temp> ();
String sql = "Select S.id,s.name from Institutions s where s.parent=?";
Session session = This.getsession ();
SQLQuery sqlquery = session.createsqlquery (sql);
Sqlquery.setlong (0, id);
Sqlquery.addscalar ("id", Hibernate.long);
Sqlquery.addscalar ("name", hibernate.string);
Sqlquery.setresulttransformer (Transformers
. Aliastobean (Institutions.class));
list<institutions> list = Sqlquery.list ();
if (list.size () <= 0) {
Temp T = new temp ();
T.setid (0L);
if (id = = 1) {
T.setname ("Please select the organization");
} else {
T.setname ("No branch of the institution");
}
Lt.add (t);
Return lt;
}
for (int i = 0; i < list.size (); i++) {
Institutions ins = List.get (i);
Temp T = new temp ();
T.setid (Ins.getid ());
T.setname (Ins.getname ());
Lt.add (t);
}
Return lt;
}

JSP layer:

var val=$ ("#organization option:selected"). Val ();
$.ajax ({
Type: "Post",
URL: "${ctx}/account/user/branchmap",
DataType: "JSON",
Async:false,
Data:{organizid:val},
Success:function (data) {
$ ("#branch"). HTML ("");
$.each (Data,function (I,item) {
$ ("#branch"). Append ("<option value=" +item.id+ ">" +item.name+ "</option>");
});
}
});

then add the configuration in the corresponding Spring-mvc.servlet.xml

<!--
After the @ResponseBody returns the Chinese characters in the string may appear garbled because sping
MVC defaults to text/plain;charset=iso-8859-1, to support the need to do the following configuration to specify the encoding format
-->

<bean
class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name= "Messageconverters" >
<list>
<bean
class= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name= "Supportedmediatypes" >
<list>
<!--return string format json-->
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>

As long as the result set of the background query query is a list collection of list<xxx> generics and then the controller layer annotation is @ResponseBody, the foreground calls this method and marks it as datatype in $.ajax: "JSON", Async: False, you can.

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.