Showing posts with label logic. Show all posts
Showing posts with label logic. Show all posts

October 4, 2006

Struts authentication on every request

Today, somebody asked me if we always need to implement an interceptor design to authenticate the user request every time before it is processed. Fortunately for him, he is working on a Struts based solution, hence my answer was that you have an option to create a subclass of RequestProcessor.

Override processPreprocess() (which returns true in normal conditions) to call your method which will authenticate the user request. Return true to proceed and false to abort request processing.

I have learned this trick the hard way (doing it wrong the first time), but you have my blog to simplify things for you. Write to me (Sachin) if you need any particular solution.

July 21, 2006

Divide and rule - use multiple struts config files for one application

Too tired of searching through the long struts config file for the module you are working on? Get a new config file for your modules. All you need is to modify the web.xml to use multiple config files.

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>

org.apache.struts.action.ActionServlet

</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>

/WEB-INF/struts-config.xml,

/WEB-INF/module1.xml,

/WEB-INF/module2.xml,

/WEB-INF/module3.xml

</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

When the ActionServlet is loaded, Struts will merge the results of the specified configuration files into a single in-memory configuration. Just make sure that syntax of each file is correct.

December 8, 2005

Divide and rule - use multiple resource bundles

Too tired of searching through the long struts resource bundles file for the text you added? Get a new resource file for your module. All you need is to modify the struts-config.xml in the following manner:

<message-resources

parameter=" com.text.sachin.msgText "

key="msgText">

</message-resources>

<message-resources

parameter=" com.text.sachin.pageText "

key="pageText">

</message-resources>

On application startup, Struts creates a runtime representation of the message resources and stores it in the servlet context.

Note: If the properties file was located in /WEB-INF/classes/com/text/sachin/msgText.properties, you would set up the message resources element as follows:

<message-resources

parameter="com.text.sachin.msgText"/>

Use “bundle” parameter in “bean:message” tag to identify the file having the properties.

<bean:message bundle="msgText" key="msgText.welcome"/>

I’ll try to upload files with working example to illustrate this technique of struts. Post me in case you need help.

February 18, 2005

Presentation of table with alternate row background colors (works with Java, .NET, Ruby, everything else too)

Yesterday my team member asked me (after spending 2hrs) for logic for presentation of table with alternate row background colors. I felt that it may be simple to people who know it and difficult to others, hence I am blogging about the solution.

This solution works with Java, .NET, Ruby, every other language. I’ll use HTML example for simplicity and focus on solution rather than technology syntax.

First, declare two style classes (or something you need to set the font/background/etc that needs to be altered):

<style>

.rowstyle1 {background-color:white;}

.rowstyle0 {background-color:lightyellow;}

</style>

Second, declare a numeric (int) variable to count (running number of) the table rows:

int rowNumber = 0;

Third, increment the running number before rendering the table row:

rowNumber++;

Fourth, use the running number to assign the style class to each row:

class='rowstyle<%=(rowNumber % 2)%>'

Post me if you need help with logic building in any situation. I’ll try to help.