This is very basic running example for spring MVC. I developed this project in IMB RAD (
Rational Software Development Platform 6.0) and WebSphere Application Server v6.0.
Step1: Create one Dynamic web project (File -> New -> Dynamic Web Project) and give some name (Say SpringTrans).
Step2: Add spring.jar to project and copy spring.jar to WEB-INF\lib folder
Step3: Edit web.xml
<?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%22>
<display-name>SpringTrans</display-name>
<servlet>
<servlet-name>SpringTrans</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringTrans</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 4: Create one xml file SpringTrans-servlet.xml
Note: Name must be SpringTrans-servlet.xml, since we have given the servlet name SpringTrans, so it is SpringTrans-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd%22>
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/loginCheck.htm" value-ref="userController"/>
</map>
</property>
</bean>
<bean id="userController" class="com.controller.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/loginCheck.htm">loginHandler</prop>
</props>
</property>
</bean>
</property>
</bean>
</beans>
Step 5: In Java Resource, create one package com.controller
Step 6: Create one Java file in this package UserController.java
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController {
public ModelAndView loginHandler( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*********** FROM CONTROLLER *************");
String userInfoName = request.getParameter("userName");
String pass = request.getParameter("password");
System.out.println("User Name :: "+userInfoName);
System.out.println("Password :: "+pass);
return new ModelAndView("/SucessPage.jsp","user",userInfoName);
}
}
Step 7: Create jsp file inside WebContent folder
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Login Page</TITLE>
</HEAD>
<BODY>
<P>LOGIN PAGE<BR></P>
<form action="loginCheck.htm">
<TABLE>
<TBODY>
<TR>
<TD width="326" align="right">User Name</TD>
<TD width="311"><input type="text" name="userName"/></TD>
</TR>
<TR>
<TD width="326" align="right">Password</TD>
<TD width="311"><input type="text" name="password"/></TD>
</TR>
<TR>
<TD width="326" align="right"><input type="submit" value="OK"/></TD>
<TD width="311"><input type="button" value="Cancel"/></TD>
</TR>
</TBODY>
</TABLE>
</form>
</BODY>
</HTML>
Step 8: SucessPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>SucessPage Page</TITLE>
</HEAD>
<BODY>
<P>Your Login is Sucessfull</P>
<%
String userName = request.getParameter("userName");
try{ out.print(userName); }catch(Exception e){}
%>
</BODY>
</HTML>
Step9:
Run http://localhost:9080/SpringTrans/
Tuesday, February 24, 2009
Subscribe to:
Post Comments (Atom)
Great tutorial and thanks for the sharing your experience with us. Its really nice to read your post and learn so many thing. Keep posting like this.
ReplyDeleteReally very good tutorial for start up....
ReplyDeletethanks
Thanks for sharing very clear tutorial for beginner.
ReplyDeleteIn login page , after giving username and password when I am clicking OK button "webpage not found" page is displayed. Can any one help me in solving this issue.
ReplyDeleteThanks in advance
Thanks for sharing such a great blog..Keep posting..
ReplyDeleteJAVA Institutes in Chennai
Thank you, I really appreciate for the helpful tutorial
ReplyDelete