How can the JSON data be returned correctly and conveniently when an AJAX request is required in the crystal portlet and the backend is returned with JSON data?
Here are two ways to:
Method One: Ajax requests, using Renderurl, corresponding to the Portlet class using the Ajax (data) method to return Java objects;
Method Two: Ajax requests, using ResourceUrl, corresponding to the Portlet class using the Ajax (Data,response) method to output the Java object directly into the response stream; (Recommended this method)
Step into the Guide
Method One:
- When Ajax requests, URLs are generated in Renderurl, with no differences from ordinary portlet requests;
The corresponding method is created in the Portlet class that corresponds to the access, and is returned in Ajax (data), as shown in the following code example:
@RequestMapping (params"action=getuserbyid") Public String Ajaxrenderurltest (@RequestParam string id) { = Userservice.getuserbyid (ID); return Ajax (user);}
- At this point, the "common/_ajax.jsp file could not be found" error may appear, as follows:
- Make sure the project uses the latest version Crystal-portlet-core 2.4 snapshot and above version;
In the Portlet's/web-inf/spring/common/jstl-portlet.xml file, replace the configuration of the Viewresolver bean with the following configuration:
<bean id="Viewresolver" class="Com.gsoft.crystal.portlet.CrystalInternalResourceViewResolver" Abstract="true"> <property name="Viewclass"value="Org.springframework.web.servlet.view.JstlView"/> <property name="prefix"Value="/web-inf/jsp/"/> <property name="Baseprefix"Value="/web-inf/jsp"/> <property name="suffix"Value=". JSP"/> <property name="ContentType"Value="Text/html;charset=utf-8"/> <property name="Order"Value="1"/></bean>
Class is replaced with Com.gsoft.crystal.portlet.CrystalInternalResourceViewResolver;
- Add the Baseprefix variable, the value of which is the root directory of the JSP file, such as:/web-inf/jsp.
Method Two: (Recommended use)
- When Ajax requests, URLs are generated using ResourceUrl;
The corresponding method is created in the Portlet class that corresponds to the access, and is returned in Ajax (Data,response), as shown in the following code example:
@ResourceMapping ("testajax")publicvoid ajaxtest ( Resourceresponse response, @RequestParam String ID) throws IOException { = Userservice.getuserbyid (ID); C11/>ajax (user, response);}
The foreground AJAX call sample code is as follows:
$.ajax ({ type:'post', URL:'<portlet: ResourceUrl id= "Testajax"/>', data:{ <portlet:namespace/ >id: success:function (data) { }});
How do I correctly return JSON data to an AJAX request in the Crystal Portlet?