Hi can you explain the flow of spring web programatically.........
Hi at first please insert the all the relevant jar files in WEB-INF/lib directory, add the following servlet configuration in your web.xml file.
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping>
Now write the dispatcher-servlet.xml file and put it into the same directory as web.xml file.
In dispatcher servlet the main thing you have to do is to specify the path of the Controller class, as
<context:component-scan base-package="net.roseindia.controller" />
the following are the typical configurations in dispatcher-servlet.xml file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Enable annotation driven controllers, validation etc... --> <mvc:annotation-driven /> <context:component-scan base-package="net.roseindia.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> </bean> </beans>
Then write the form as
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <CENTER> <h2>Please Enter Login Detail</h2> <form:form action="loginForm.htm" commandName="loginForm"> <table cellpadding="5" cellspacing="10" bgcolor="#00B8E6"> <tr> <td><form:label path="userId">User Id</form:label></td> <td><form:input path="userId" /></td> </tr> <tr> <td><form:label path="password">Password</form:label></td> <td><form:password path="password" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Submit" /></td> </tr> </table> </form:form></CENTER>
and corresponding bean class would be
package net.roseindia.model; public class LoginForm { private String userId; private String password; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
And Now you need the write the controller class. The package wold be the same as you specified in the dispatcher-servlet.xml file i.e net.roseindia.controller
The controller class is given below please consider it..
package net.roseindia.controller; import javax.validation.Valid; import net.roseindia.model.LoginForm; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("loginForm.htm") public class MyController { @RequestMapping(method = RequestMethod.GET) public String getRequest(ModelMap model) { LoginForm login = new LoginForm(); model.put("loginForm", login); return "loginForm"; } // @RequestMapping("loginForm.htm") @RequestMapping(method = RequestMethod.POST) public String postReQuest(ModelMap model, @Valid LoginForm login) { // LoginForm login = new LoginForm(); model.addAttribute(login); System.out.println("Id- " + login.getUserId()); System.out.println("Id- " + login.getPassword()); model.put("loginForm", login); return "loginForm"; } }
Thanks
Ads