Results 1 to 6 of 6

Thread: How to return JSON/XML using MultiActionController?

  1. #1
    Join Date
    Oct 2008
    Posts
    286

    Default How to return JSON/XML using MultiActionController?

    here's the MultiActionController
    Code:
    public class SimulationController extends MultiActionController {
    
    	private SimulationService simulationService;
    
    	/**
    	 * @param simulationService the simulationService to set
    	 */
    	@Autowired
    	public void setSimulationService(SimulationService simulationService) {
    		this.simulationService = simulationService;
    	}
    	
    	public ModelAndView changeSimulationMode(HttpServletRequest request, HttpServletResponse response) 
    		throws Exception {
    		
    		Object objMode = request.getParameter("mode"); 
    		String strMode = (String) objMode;
    		
    		SimulationMode simulationMode = new SimulationMode();
    		simulationMode.setSimulationMode(strMode);
    		
    		simulationService.execChangeSimulationMode(simulationMode);
    		
            String json = "";
            try {
            	
            	json = "{" +
    					   "\"success\":true" +
    					"}";
    
            } catch (Exception e) {
                response.setHeader("Errored", "true");
                response.setHeader("ErrorType", "system");
                response.setHeader("ErrorMessage", e.getMessage());
                json = "{success:false,errors:{reason:'error'}}";
            }
            
            response.setContentType("text/json; charset=utf-8");
            response.setHeader("Cache-Control", "no-cache");
            PrintWriter pw = response.getWriter();
            pw.write(json);
            pw.close();
            return null;
    	}
    	
    }
    Successfully called the changeSimulationMode method but when it returns to browser... it will popup a download dialog to download the JSON content... like when downloading a file..

    I've heard that Spring MVC 2.5.6 and SWF2.0.8 provides JSON and XML view..

    Could you guide me on how to implement the above..
    thanks
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

  2. #2
    Join Date
    May 2006
    Posts
    3

    Default

    set content type to make the response "browser aware" and it should be fine!

  3. #3
    Join Date
    Oct 2008
    Posts
    286

    Default

    I am setting the content type already,... that's why it looks strange...
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

  4. #4
    Join Date
    May 2006
    Posts
    3

    Default

    check the response using livehttpdheader firefox plugin. my guess is the response type is still not d right one!

    note that ur are setting content type in response obj like,
    response.setContentType("text/json; charset=utf-8");

    rather try setting it in json obj since json's obj new constructor sets ct to default "app/json"

    also try somethin like JsonView jv = new JsonView();
    Last edited by shivarajan; Mar 10th, 2010 at 04:07 AM.

  5. #5

    Default

    another way -

    1. In MultiActionController you will have getJSON() which will return JSON string

    Code:
    public ModelAndView getJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		// following service return JSON string
    		String readContentString = contentService.readContent();
    		return new ModelAndView("ContentJsonPage","contentJson", readContentString);
    		
    	}
    2. In contentJSONPage.jsp

    Code:
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
    <c:out value="${contentJson}" escapeXml="false"></c:out>

    This way it will return JSON

  6. #6

    Thumbs up

    A simple response.setContentType("text/html"); does the trick.

    Best Regards,

    Ashish.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •