Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Thursday, June 30, 2011

Response Header with MVC interceptors in Spring

Response Header's in Spring can be easily set using org.springframework.web.servlet.support.WebContentGenerator as this is a abstract class so we use a direct known sub class for the same which is org.springframework.web.servlet.mvc.WebContentInterceptor , this Interceptor checks and prepares request and response. Checks for supported methods and a required session, and applies the specified number of cache seconds.

Here is the Example, this should be under beans tag in your server-config.xml file

<mvc:annotation-driven />
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="120"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="requireSession" value="false"/>
<property name="useCacheControlNoStore" value="true" />
<property name="cacheMappings">
<props>
<prop key="/**/*.html">2000</prop>
<prop key="/**/*.css">500000</prop>
<prop key="/**/*.js">2592000</prop>
</props>
</property>
</bean>
</mvc:interceptors>

In CacheMapping attribute we can specify the cache time for different file types, as this increases the preformance of application.

Tuesday, June 21, 2011

PJson in Spring using JacksonJson (Cross Domain Issues)

We can create pjson (json with padding) to achieve cross domain java-script call which is very important if data is coming from other domain.

Here is what i did to get pjson from object.
I created a class MappingJacksonJsonpView extended it by AbstractView, now here is the trick
I overrided a method renderMergedOutputModel as follows:

@Override
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception
{
Object value = filterModel(model);
JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), encoding);
String callback = request.getParameter("jsoncallback");
prefixJson = false;
if (callback!=null)
{
prefixJson = true;
}
if (prefixJson)
{
generator.writeRaw(callback + "(");
}
objectMapper.writeValue(generator, value);
generator.flush();

if (prefixJson)
{
generator.writeRaw(");");
generator.flush();
}
}

and make sure you put entry in your servlet.xml file
<property name="defaultViews">
<list>
<bean class="com.views.utility.MappingJacksonJsonpView" />

Monday, July 13, 2009

Spring ActionScript Configuration in Flash/Flex/AIR application


Just as we use the applicationContext.xml in java application where we map the classes to the objecys in the context file and mapping the variables in the class files to the properties in the configuration file, we can use a simialr methodolgy in our flex application and get the changes to the variable by just changing in the configuration file , without building or refreshing the application.

Here is the code sniplet which is used to achive the following :-
// spring actionscript config
var applicationContext : FlexXMLApplicationContext =new FlexXMLApplicationContext("config.xml");
applicationContext .addEventListener(Event.COMPLETE, onApplicationContextComplete);
applicationContext.load();

protected function onApplicationContextComplete(event:Event):void
{
var appConfig : AppConfig = applicationContext.getObject("appConfig");
model.appConfig = appConfig;
resourceManager.localeChain = [appConfig.locale];

}

// HERE model.appConfig is the actionscript class which i have mapped to the object in my configuration file.
AppConfig.as file is as follows :-
package com.infoswell.model.global
{
[Bindable]
public class AppConfig
{
// src path used as root to locate images, etc.
public var srcPath:String = "";


// locale for menu config path
// note: curently resource bundle properties locale from project compile options
public var locale:String = "en_US";

public function AppConfig()
{
}

}
}


The config.xml is as follows:-

xsi:schemaLocation="http://www.pranaframework.org/objects
http://www.pranaframework.org/schema/objects/prana-objects-0.6.xsd">







The swc file which is used in the application can be created as follows :-
First download the source using a subversion client from the following url:

https://src.springframework.org/svn/se-springactionscript-as/

Step into the ant folder and execute “ant” to see the options. You can create a debug enabled version, a release, documentation. I created the debug enabled version with the following command. Of course you do have to have ant on your path. Do not forget to change the build.properties. You need to change the FLEX_HOME parameter as described in the readme file that comes with the sources.

ant compile-main-debug

The swc file is created in the antbuild/compile/main/swc folder.

Use the swc created in the project libs folder and use this extra ordinary feture to load configuration from outside.

Cheers
Varun

About Me