Wednesday, August 29, 2007

Spring portlet mvc and spring servlet mvc validation

Spring mvc is easy to get along with, unless we get the basic flow right. We end up wasting lot of time figuring out the flow of control/binding of errors for validation/etc. Spring portlet mvc validation
  1. extend AbstractCommandController or SimpleFormController
  2. In case of AbstractCommandController implement the method handleRenderView(req,res,command,errors)
  3. Important thing to note is that you should return the ModelAndView as new ModelAndView(jspPage, errors..getModel());
  • Note the method handleRenderView() is called even if validation fails. Hence we have to return back the command object having errors using errors.getModel().
Spring servlet mvc validation
  • Implemented a simple form controller.
class ManageProfileController extend SimpleFormController { public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { Map modelMap = new HashMap(); return new ModelAndView(forwardJSP, modelMap); } }
  • The configuration in *-servlet.xml is as below (assume dispatcher servlet is configured already)
<bean id="manageProfileController" class="com.TEST.web2.portlet.controller.common.ManageProfileController">        
        <property name="registrationService" ref="registrationService"/>
        <property name="formView"><value>common/manageprofile</value></property>
        <property name="commandClass"><value>com.TEST.web2.model.DpsUser</value></property>
<property name="validator" ><ref bean="userValidator"/></property>
      <property name="successView"><value>common/manageprofile</value></property>
    </bean>    

  • On validation error, in servlet mvc, the onSubmit() is not called and the command object is available with errors.
  • To extract the errors in JSP use:
<spring:bind path="command.confirmPassword"> <c:out value="${status.expression}"/> <input name=confirmPassword type="password" style="width:200px; " onKeyUp="document.getElementById('cpw').value=this.value" value="<c:out value="${status.value}"/>"> <font > <c:out value="${status.errorMessage}"/> </font> </spring:bind>

Wednesday, August 22, 2007

Castor --The XML Marshaller

Forget writing XML parser...Castor will take care of parsing. It generates the java
classes to work with XML.
To use castor, -- put the below lib's in classpath -- ./lib/castor-0.9.9.jar ./lib/castor-0.9.9-srcgen-ant-task.jar ./lib/castor-0.9.9-xml.jar xercesImpl.jar; commons-logging.jar -- Run java org.exolab.castor.builder.SourceGenerator -i test.xsd -dest dtnFolder --optionally specify the package name if required. -- To convert from DTD to XSD use java org.exolab.castor.xml.dtd.Converter dtdFileName newXSDFileName To use the generated code:
  • To run the unmarshal the castor lib should be in class path and follow the sample code herewith.
// Create a Reader to the file to unmarshal from
reader = new FileReader("test.xml");

// Marshal the person object
Person person = (Person)Unmarshaller.unmarshal(Person.class, reader);

  • To marshal an instance of the person class you simply call the Marshaller as follows:
// Create a new Person
Person person = new Person("Ryan 'Mad Dog' Madden");
person.setDateOfBirth(new Date(1955, 8, 15));

// Create a File to marshal to
writer = new FileWriter("test.xml");

// Marshal the person object
Marshaller.marshal(person, writer);

Follow the document of castor for details.

Wednesday, August 1, 2007

LINUX usefuls

html'&gt;linux basic commands: ls ls -a -- lists all files (hidden as well .) ls -l -- list with permission and details ln -s A B -- create soft link btw two file/folders ps -- see current process ps -e -- see background process as well ps -H -- list process hierarchy ps -f -- ? pstree top - see all proc's at one shot pmap [PID] : list all the resource held by this proc. cat /proc/[PID]/maps : kill -3 [PID] -to get the status of threads $ killall httpd -kill all the procs with name httpd grep -- useful for search. Very powerful if we combine with other commands eg: ps -ef | grep liferay == will return all the liferay process fg -- bring the process to foreground bg -- send the process to background CTRL Z -- send the process to background..Can bring it to foreground using fg. CTRL C -- close a program. tail myfile.txt -n 100 -- list the last 100 lines in the file mail -s "picture of me surfing" sylvia@home.com &lt; surfing.jpeg --- To mail a file tail -f catalina.out --display the contents in real time head -15 myfile.txt - Would display the first fifteen lines of myfile.txt. diff -r -N folder1/ folder2/ ---find difference b/n folders.. -N do display diff content. cat test.txt --display content of file test.txt cat t.txt t2.txt &gt; t3.txt -- merge t.txt+t2.txt= t3.txt difference b/n folder du -hs liferay1/webapps/* liferay2/webapps/* touch file.txt -- quickly create a empty file VI editor commands i -- insert mode r -- replace mode / -- search :%s/fred/joe/igc : general substitute command(replace) w -- write wq --write and quit q-- quit :#20 - moving to a line 20 Ctrl u --page up Ctrl d -- page down :set number -- to set the number ~ Toggle case of the character under the cursor, or all visually-selected characters. q: You can bring up the command line history while in Normal mode. :42G -- ways to go to a particular line (line 42 for example) Multiple Files Management vim test xyz :bn -- next file :bp -- next file :wn -- write file and move to next (SUPER) Screen Ref: http://www.kuro5hin.org/story/2004/3/9/16838/14935 Screen is best described as a terminal multiplexer. Using it, you can run any number of console-based applications--interactive command shells,logging, curses-based applications, text editors, etc.--within a single terminal.Also can reattach again to the screen even after logout.Excellent isn't it. Should have had a separate blog :0). Any way the quick reference below: $screen --Start screen just by typing screen at your favorite command shell prompt Ctrl a c --create another screen ctrl a ctrl A --switch screen ctrl a n -- switch next screen ctrl a p -- switch prev screen ctrl a A -- Give a name to the current screen. ctrl a " -- to get a full-screen list of windows ctrl a K --You can also kill misbehaving programs $screen -r --will reattach the screen to the current session which. Networking commands: ping netstat -- active connections netstat -r --shows routing table ifconfig (like ipconfig in windows) ifconfig eht0 down -- shutdown lan card (to renew ip u can do this) ifconfig eht0 up --startup lan card iptraf nmap -- port sniff dig NATing /etc/hosts file It is used to map simple human readable names with ip address. Eg: 127.0.0.1 localhost.localdomain localhost 127.0.0.1 access.idp1.com Installation rpm -i foo-v123.rpm ---install a package rpm -i http://oss.oracle.com/kernel-2.4.20-18.10.1.i686.rpm rpm -e foo rpm -qi foo -- To query a RPM package and display info more details refer http://www.idevelopment.info/data/Unix/Linux/LINUX_RPMCommands.shtml Autoexec.bat in linux vi /root/.bashrc # the user is root here. ---Do whatever needs to be done on login in the .bashrc To load the properties without relogging in use : source /root/.bashrc Secured remote copy: scp &lt;target1&gt; &lt;target2&gt; eg: scp test.zip sandeepm@xHost:/export/home/sandeep -- copies the test.zip to the target2 from target 1. scp sandeepm@xHost:/export/home/sandeep/test22.zip . -- copies the test22.zip from target2 to target1 .

Use grep recursively

You can search recursively i.e. read all files under each directory for a string “192.168.1.5″ cd /etc $ grep -R "192.168.1.5" *

Use grep to search words only

When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word: $ grep -w "boo" /path/to/file

Grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: $ grep -v bar /path/to/file

Count line when words has been matched

grep can report the number of times that the pattern has been matched for each file using -c (count) option: $ grep -c 'word' /path/to/file To get the DB conn list $ netstat|grep -c app610 Shell script The below will run a command for configurable no of times. chmod +x test usage : ./test [NO_TIMES] [COMMAND] for (( i = 0 ; i &lt; $1; i++ )) do echo "Executing $2"; $2; sleep 1; done More scripts here http://www.usd.edu/~sweidner/lsst/

One of the best methods to capture a Unix terminal session is to use the `script` command.

In this example we start a script session, run a couple of commands, and then use the `exit` command to stop capturing the terminal session:

$ script
Script started, output file is typescript
$ pwd
/home/will
$ ps
PID  TT  STAT      TIME COMMAND
11909  p0  Ss     0:00.05 -bash (bash)
25622  p0  S+     0:00.01 script
25623  p1  Ss     0:00.01 /usr/local/bin/bash -i
25624  p1  R+     0:00.00 ps
$ exit
Ref: http://vim.wikia.com/wiki/Best_Vim_Tips