spring web

Ads
 

spring web

Hi can you explain the flow of spring web programatically.........

View Answers

October 18, 2011 at 12:42 PM

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









Related Tutorials/Questions & Answers:
spring Web Services - Spring
spring Web Services  Hi, Can any one explains or give me some example which gives me that how to start up spring web services. Thanks, mahi
Version of spring>spring-web dependency
List of Version of spring>spring-web dependency
Advertisements
spring web
spring web  Hi can you explain the flow of spring web programatically... in WEB-INF/lib directory, add the following servlet configuration in your web.xml... http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http
Version of springframework>spring-web dependency
List of Version of springframework>spring-web dependency
Maven Dependency spring-web >> 1.0.2
You should include the dependency code given in this page to add Maven Dependency of spring >> spring-web version1.0.2 in your project
Spring Boot Web Starter 2.1.1 Maven dependency
Spring Boot Web Starter 2.1.1 Maven dependency  Hi, I want to use Spring Boot Web Starter 2.1.1 in my spring boot application. My application is based on the old version of Spring Boot. Now will use Spring Boot Web Starter 2.1.1
Spring web UrlBasedViewResolver
Spring web UrlBasedViewResolver...;org.springframework.web.servlet.view.JstlView" /> <property name="prefix"> <value>/WEB...://www.springframework.org/schema/beans/spring-beans-3.0.xsd http
Maven Dependency spring-web >> 1.0
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.0 in your project
Maven Dependency spring-web >> 1.0.1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.0.1 in your project
Maven Dependency spring-web >> 1.0-rc1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.0-rc1 in your project
Maven Dependency spring-web >> 1.1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1 in your project
Maven Dependency spring-web >> 1.1.1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1.1 in your project
Maven Dependency spring-web >> 1.1.2
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1.2 in your project
Maven Dependency spring-web >> 1.1.3
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1.3 in your project
Maven Dependency spring-web >> 1.1.4
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1.4 in your project
Maven Dependency spring-web >> 1.1.5
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1.5 in your project
Maven Dependency spring-web >> 1.1-rc1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1-rc1 in your project
Maven Dependency spring-web >> 1.1-rc2
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.1-rc2 in your project
Maven Dependency spring-web >> 1.2
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2 in your project
Maven Dependency spring-web >> 1.2.1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2.1 in your project
Maven Dependency spring-web >> 1.2.2
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2.2 in your project
Maven Dependency spring-web >> 1.2.3
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2.3 in your project
Maven Dependency spring-web >> 1.2.4
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2.4 in your project
Maven Dependency spring-web >> 1.2.5
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2.5 in your project
Maven Dependency spring-web >> 1.2.6
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2.6 in your project
Maven Dependency spring-web >> 1.2-rc1
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2-rc1 in your project
Maven Dependency spring-web >> 1.2-rc2
You should include the dependency code given in this page to add Maven Dependency of springframework >> spring-web version1.2-rc2 in your project
Maven dependency for Spring Web MVC 4.0.1.RELEASE
Maven dependency for Spring Web MVC 4.0.1.RELEASE  Hi, What is the Maven dependency for Spring Web MVC 4.0.1.RELEASE? Thanks   Hi, You...; <groupId>org.springframework</groupId> <artifactId>spring
spring boot starter web maven dependency 2.0.0
spring boot starter web maven dependency 2.0.0  Hi, I want to use.... What is spring boot starter web maven dependency 2.0.0? Thanks   Hi...; The above code is for spring boot starter web maven dependency 2.0.0. Thanks
Spring web mvc 3.0 mysql project
Spring web mvc 3.0 mysql project  Hello Sir... I want a complete database connectivity web project in spring mvc 3.0..Just give me a simple example...: http://www.roseindia.net/spring/jdbc-template-batch-update.shtml
develope a web site using JSF,Spring,Hibernate.
develope a web site using JSF,Spring,Hibernate.  I have to develope a web site,just like Facebook,which will have all the functionality as wall post...,Spring Framework,Hibernate,JMS. Please anyone provide me idea,how to proceed ahead

Ads