Home>

spring mvc is a follow-up product of springframework,Already integrated into spring web flow. The spring framework provides a full-featured mvc module for building web applications. Use spring pluggable mvc architecture, so when using spring for web development, you can choose to use spring's springmvc framework or integrate other mvc development frameworks,Such as struts1, struts2 and so on.

1. The first method is a product of the spring2 era,That is, each json view controller is configured with a jsoniew.

Such as:<bean />

Or

Also use the jackson jar package.

2. The second method is to use json to serialize objects into json. Common tools are jackson, fastjson, and gson.

Use httpservletresponse, then get response.getoutputstream () or response.getwriter ()

Direct output.

Example:

public class jsonutil
{
  private static gson gson=new gson ();
  /**
   * @methodname:tojson
   * @description:convert the object into a json string, this method can meet most needs
   * @param src
   *:The object to be transformed
   * @return:json string after conversion
   * /
  public static string tojson (object src) {
    if (src == null) {
      return gson.tojson (jsonnull.instance);
    }
    return gson.tojson (src);
  }
}

3.The third annotation using spring mvc3 @responsebody

E.g:

@responsebody
 @requestmapping ("/list")
 public list<string>list (modelmap modelmap) {
  string hql="select c from clothing c";
  page<clothing>page=new page<clothing>();
  page.setpagesize (6);
  page=clothingserviceimpl.queryforpagebyhql (page, hql);
  return page.getresult ();
 }

Then use the default configuration of spring mvc to return json, but you need a jackson jar package.

Note:When usingin springmvc-servlet.xml, if the annotationmethodhandleradapter is injected by default before 3.1, and the requestmappinghandleradapter is injected by default after 3.1, just add the jar package mentioned above!

If it is manually injected requestmappinghandleradapter can be set like this

The configuration is as follows:

<bean
    p:ignoredefaultmodelonredirect="true">
      <property name="messageconverters">
        <list>
          <bean />
        </list>
      </property>
    </bean>

Add package

jackson-mapper-asl-*. jar

jackson-core-asl-*. jar

As can be seen,It ’s getting easier and easier to use,Programmers are getting stupid,I do n’t know if it ’s a good thing,Still bad ...

The above are the three ways that springmvc returns json data that we share with you,Hope you like it.

  • Previous Analysis of the difference between js form submission and submit submission
  • Next C ++ implementation singleton pattern examples