Modify SPRINGMVC to return the JSON data in AJAX mode

Source: Internet
Author: User

Now, the requirement is to modify the SPRINGMVC Ajax request return value (to encrypt the return value) of several functional modules.

Because the controller is a lot, and in the past with the opening and closing principle, here is the choice of the use of interceptors (filter is not spring Handlerfilter). Nonsense not much, on the code. (JSON data is returned in the original controller).


    1. Modify Web. XML Add Filter

<filter>

<filter-name>jsonFilter</filter-name>

<filter-class>com.spr.util.UpdateJsonFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>jsonFilter</filter-name>

<url-pattern>/android/*</url-pattern><!--only intercept action--> under this request path.

</filter-mapping>

2. Processing classes

Package com.spr.util;


Import Java.io.ByteArrayOutputStream;

Import java.io.IOException;

Import Java.io.PrintWriter;


Import Javax.servlet.Filter;

Import Javax.servlet.FilterChain;

Import Javax.servlet.FilterConfig;

Import javax.servlet.ServletException;

Import Javax.servlet.ServletOutputStream;

Import Javax.servlet.ServletRequest;

Import Javax.servlet.ServletResponse;

Import Javax.servlet.http.HttpServletResponse;

Import Javax.servlet.http.HttpServletResponseWrapper;


Import Net.sf.json.JSONObject;


Import Org.apache.commons.lang3.StringUtils;

Import Org.springframework.http.HttpStatus;


public class Updatejsonfilter implements Filter {


public void DoFilter (ServletRequest request, servletresponse response,

Filterchain chain) throws IOException, Servletexception {

Byteresponsewrapper BRW = new Byteresponsewrapper (

(httpservletresponse) response);

Chain.dofilter (request, BRW);

String out = new String (Brw.getbytes ());

if (! Stringutils.isblank (out)) {

Handling of encrypted business (skip)

out = Out.tostring ();

} else {

out = "";

}

Response.setcontenttype ("Application/json; Charset=utf-8 ");

Response.setcontentlength (Out.getbytes (). length);

Response.getoutputstream (). Write (Out.getbytes ());

}


public void Destroy () {

}


public void init (Filterconfig filterconfig) {

}


Static Class Byteresponsewrapper extends Httpservletresponsewrapper {

Private PrintWriter writer;

Private Byteoutputstream output;

private int code = 200;


Public byte[] GetBytes () {

Writer.flush ();

return Output.getbytes ();

}


Public Byteresponsewrapper (HttpServletResponse response) {

Super (response);

Output = new Byteoutputstream ();

writer = new PrintWriter (output);

}


@Override

Public PrintWriter getwriter () {

return writer;

}


@Override

Public Servletoutputstream Getoutputstream () throws IOException {

return output;

}


public int GetStatus () {

return code;

}


public void Sendredirect (String location) throws IOException {

Code = HttpStatus.MOVED_TEMPORARILY.value ();

Super.sendredirect (location);

}


@Override

public void setStatus (int sc) {

This.code = SC;

}


@Override

public void SetStatus (int sc, String message) {

This.code = SC;

}


@Override

public void Senderror (int sc) throws IOException {

This.code = SC;

}


@Override

public void Senderror (int sc, String msg) throws IOException {

This.code = SC;

}

}


Static Class Byteoutputstream extends Servletoutputstream {

Private Bytearrayoutputstream BOS = new Bytearrayoutputstream ();


@Override

public void Write (int b) throws IOException {

Bos.write (b);

}


Public byte[] GetBytes () {

return Bos.tobytearray ();

}

}

}


This article is from the "re-learn Java" blog, please be sure to keep this source http://3131854.blog.51cto.com/3121854/1637831

Modify SPRINGMVC to return the JSON data in AJAX mode

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.