Wednesday, September 14, 2011

Spring MVC MultiActionController - simple login application

Hello All,

Here i am posting simple Spring MVC example with IOC(setter , Constructor Injection).

Follow the steps to execute the program in your eclipse.

Step1 : Open Eclipse - Create new Dynamic Web Project

Step2 : After creating Project, open web.xml which is there inside created project.

edit Web.xml and add the below code.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>spring-features</display-name>

<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>


Step 3: As per above web.xml code- the servlet name as springweb so As per spring mvc we have to create xml file called springweb-servlet.xml under WEB-INF






Step 4: Inside springweb-servlet.xml , we have to put configuration details for the controller's, jsp pages., IOC information. as mentioned below.







<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd;





<!-- no 'id' required, HandlerMapping beans are automatically detected by the DispatcherServlet -->





<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">



<property name="mappings">



<props>



<prop key="login.htm">loginController</prop>



<prop key="success.htm">loginController</prop>



</props>



</property>



</bean>




<bean id="loginController" class="com.siva.controller.LoginController">



<property name="name" value="siva"/>



<property name="age" value="27"/>



<constructor-arg type="int" value="27"/>



<constructor-arg type="java.lang.String" value="raju"/>



</bean>



<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">



<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>



<property name="prefix" value="/WEB-INF/jsp/"/>



<property name="suffix" value=".jsp"/>



</bean>



</beans>




Step 5: Create LoginForm.java inside src folder- under any package





package com.siva.form;
import java.io.Serializable;
public class LoginForm implements Serializable {




private static final long serialVersionUID = 1L;
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}







Step 6: Create LoginController which extends MultiActionController,





package com.siva.controller;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.siva.form.LoginForm;




public class LoginController extends MultiActionController {




public LoginForm loginForm;
private String name;
private String age;




public LoginController(){
}




//This is constructor type setting in IOC - Check on springweb-servlet.xml for further reference.
//If you are still confusing the Study IOC basics.




public LoginController (String name1 , int age1)
{
System.out.println("Name------" + name1);
System.out.println("Age------" + age1);
}




/**
* This method has been called when ever the user enter login.htm under project.
* In this GetName, getAge methods are one way of IOC through set methods. We set the values
* in springweb-servlet.xml as a property and value. So when ever we called respective get methods
* we will get the results from the springweb-servlet.xml.
*/




public ModelAndView login(HttpServletRequest request, HttpServletResponse response, LoginForm form) {




ModelAndView mav = new ModelAndView();
System.out.println("Name for the set IOC " + getName());
System.out.println("Age for the set IOC " + getAge());
mav.addObject("loginForm",form);
mav.addObject("login");
return mav;
}




/**
* This method also will after enter the username and password in login page.
* It will check whether user name is 'siva' and password is 'raju'. if it's true then
* it will redirect to the success page. otherwise it will remain in the same page
*/




public ModelAndView success(HttpServletRequest request, HttpServletResponse response, LoginForm form){
ModelAndView mav = new ModelAndView();
//User Enter name and password
System.out.println("UserName----" + form.getUserName());
System.out.println("Password-----" + form.getPassword());




// Injected name and age through IOC
System.out.println("Name for the set IOC " + getName());
System.out.println("Age for the set IOC " + getAge());
mav.addObject("loginForm",form);
if(form.getUserName() != null && form.getUserName().equalsIgnoreCase("siva") &&
form.getPassword() != null && form.getPassword().equalsIgnoreCase("raju")) {
mav.setViewName("success");
}
else{
mav.setViewName("login");
}
return mav;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}
}







Step 7 : Create login.jsp, success.jsp . Place these 2 jsp pages under WEB-INF/jsp/




login.jsp




<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<body>



<form:form commandName="loginForm" id="loginForm"



action="success.htm" method="POST" >



Username <form:input path="userName"/> </br>



Password <form:input path="password"/></br> <



input type="submit" value="Login"/>



</form:form>



</body>



</html>







success.jsp




success







Step 8: Add required jar's

commons-beanutils-1.7.0.jar
commons-digester-1.8.jar
commons-lang-2.4.jar

commons-logging-1.1.1.jar

commons-pool-1.3.jar

commons-validator-1.3.1.jar

j2ee.jar

jstl-1.1.0.jar

log4j-1.2.15.jar

spring-2.5.6.jar

spring-beans.jar

spring-context.jar

spring-modules-validation.jar

spring-security-core-2.0.4.jar

spring-web.jar

spring-webmvc.jar

standard.jar


all the above jar's can be used to run the application.

Step 9: Now right click on the project- Run As - Run on Server-

select the appropriate server, and run the application.

Step 10: These are the steps to run simple MVC MultiActionController example.



Get the source code from this link
DOWNLOAD SOURCE CODE

Wednesday, September 7, 2011

Getting Started With Spring batch 2.0

Hello world example using spring batch 2.0

Hello world example with spring batch 2.0 along with eclipse.
Follow below steps to start the hello world example with spring batch.

Step 1 : open eclipse - create new java project.
Step 2 : create new xml file under project (applicationContext.xml)
Step 3: create one more xml. under project location.(simpleJob.xml)
Step 4: create one java class which implements
org.springframework.batch.core.step.tasklet.Tasklet
Step 5: override the required methods in your respective class.
Step 6: we can run the job through
org.springframework.batch.core.launch.support.CommandLineJobRunner
This class will take two parameters (Job path and job parameters).
We need to pass those details.
Step 7: Here i am posting, how to run through eclipse.
Right click on the java class( Which we written)- Select RunConfigurations -
Select Main tab- Select Main class as
org.springframework.batch.core.launch.support.CommandLineJobRunner
Click on Arguments - Enter Jobpath and JobParameters under Program arguments
Eg: simpleJob.xml helloWorldJob
Click on the Run Button then hello world will display on the console.

Step 8: Required jar files
commons-logging-1.1.1.jar
javaee-api-5.0-1.jar
log4j-1.2.14.jar
spring-2.5.6.jar
spring-batch-core-2.1.8.RELEASE.jar
spring-batch-infrastructure-2.1.8.RELEASE.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
xstream-1.3.1.jar

In this way you can start the Spring batch.

Source Code of applicationContext.xml




 

 class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
   
  
 
    
  
  
Source Code of simpleJob.xml









Source Code of HelloWorld.java
package com.siva;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class HelloWorld implements Tasklet {
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
System.out.println("Hello World");
return RepeatStatus.FINISHED;
}
} 
Source Code of the log4j.properties

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c{1} %x - %m%n
log4j.logger.org.apache.commons=ERROR
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.security.ldap=DEBUG

Tuesday, May 10, 2011

Split the big String into words

//This method used to split the big text and add it to the next line
public String StringSplit( String text , int splitSize)
{

String finalString = "";
String dummyString =text;
for(int i=0;i<=text.length()/splitSize;i++)
{
if(dummyString.length()>=splitSize)
{
finalString+= dummyString.substring(0,splitSize) +"\n";
dummyString = dummyString.substring(splitSize);
}
else
finalString+= dummyString;

}
return finalString;
}

Tuesday, April 27, 2010

webservices

Web services definition.
The term webservices is simply defined as serve a request through web is known as web
service. The service can be of any type.
the term Web Services refers to the technologies that allow for making connections.
Services are what you connect together using Web Services. A service is the endpoint of a
connection.

AddToAny

Contact Form

Name

Email *

Message *