User:Sanjeevkumarhero

Steps to create spring mvc using xml
 1)Design the input jsp with required JSP form and the form element like 

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form&#x22;%>



UserName





 2) write own command class for storing the client submitted data 

command class is a simple class java bean with private variable and public getter setter

 3) write the validation request for your command data in a seperate validator class with the followinf steps 

a) write your own validator class by implementaing org.sf.validator interface

b) override the following method

b.a) boolean supports(class<?> clazz)

b.b) void validate(Object command,Errors errors)

c) write ihe code inside the support to check whether correct command is used or not

d) write the code inside the validate method to validate the input data

e) when you get the error then add the error message using

e.a) rejectValue(String,String,Ovject[],String)

e.b) void rejectValue("propertyName","key from resource bun")

 4) write the controller class with the following steps 

A. write your own controller class by extending one of the built in controller

AbstractController   SimpleFormController

B. override the required method in your controller class

in SinpleFormController

-> ModelAndView  onSubmit(Object command) *

-> formBackingObject(req) optional

 5) configure the contriller with bean name in spring cfg file 

A. AbstractController

B. SimpleFormController















web.xml
web.xml









<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet-name>jlcindia</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<servlet-mapping>

<servlet-name>jlcindia</servlet-name>

<url-pattern>*.jlc</url-pattern>

</servlet-mapping>

</web-app>

jlc-servlet.xml
jlc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns=" http://www.springframework.org/schema/beans "

xmlns:context=" http://www.springframework.org/schema/context "

xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "

xmlns:p=" http://www.springframework.org/schema/p "

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

">

<context:annotation-config></context:annotation-config>

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

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

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

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

<bean name="/login.jlc" class="com.jlcindia.spring.mvc.LoginController">

<property name="sessionForm" value="true"/>

<property name="commandName" value="user"/>

<property name="commandClass" value="com.jlcindia.spring.mvc.User"/>

<bean class="com.jlcindia.spring.mvc.UserValidator"/>

<property name="formView" value="login">

<property name="successView" value="home">

index.jsp
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core "%>

jlc

<a href="<c:url value="login.jlc"></c:url>">User Login</a>

login.jsp
logon.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

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

jlc

<form:form method="post" commandName="user">

</form:form>

home.jsp
home.jsp

<%@ taglib prefix="jlc" uri=" http://java.sun.com/jsp/jstl/core " %>

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath;

String basePath = request.getScheme+"://"+request.getServerName+":"+request.getServerPort+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

${user.username}

CourseServices.java
CourseServices.java

package com.jlcindia.spring.mvc;

import java.util.ArrayList;

import java.util.List;

import org.springframework.web.servlet.DispatcherServlet;

public class CourseService {

public List<String> getCourse

{

List<String> cs = new ArrayList<String>;

cs.add("a");

cs.add("b");

return cs;

}

}

Action
package com.jlcindia.spring.mvc;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindException;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractController;

import org.springframework.web.servlet.mvc.SimpleFormController;

public class LoginController extends SimpleFormController{

@Override

protected ModelAndView onSubmit(Object command, BindException errors)

throws Exception {

System.out.println("onSubmit");

User user = (User)command;

String un=user.getUsername;

String pw=user.getPassword;

String page="";

if(un.equals(pw))

page=getSuccessView;

else

{

page=getFormView;

System.out.println(page);

}

return new ModelAndView(page,"user",user);

}

@Override

protected Object formBackingObject(HttpServletRequest request)

throws Exception {

System.out.println("formBackingObject");

User user = new User;

user.setUsername("ram");

return user;

}

}

command
package com.jlcindia.spring.mvc;

public class User {

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;

}

}

validator
package com.jlcindia.spring.mvc;

import org.springframework.validation.Errors;

import org.springframework.validation.Validator;

public class UserValidator implements Validator{

@Override

public boolean supports(Class<?> clazz) {

return User.class.equals(clazz);

}

@Override

public void validate(Object command, Errors errors) {

User user=(User)command;

if(user.getUsername==null || user.getUsername.length==0)

{

errors.rejectValue("username","error.username.required",null,"UserName Required");

}

if(user.getPassword==null || user.getPassword.length==0)

{

errors.rejectValue("password","error.password.required",null,"Password Required");

}

}

}

properties file
error.username.required=UserName *

error.password.required=password *

Login page

--

<!DOCTYPE html>

Bootstrap Example

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">

.rcontent{

padding-top:15px;

}

.rheader{

}

.rbody{

margin-top:50px;

}

.button {

background-color: #1ee5f9;

border: none;

color: white;

padding: 12px 100px;

text-align: center;

text-decoration: none;

display: inline-block;

font-size: 16px;

}

.input{

height: 40px;

width: 250px;

}

.rfooter{

color: #34bd09;

background-color: black;

height: 36px;

margin-top: 15px;

}

<img src="http://image3.mouthshut.com/images/imagesp/925759937s.png" width="300" height="200"/>

<img src="https://www.vidalhealth.ae/sites/default/files/styles/1200x570/public/slider-img/Scale.jpg?itok=HCypQFjr" width="100%" height="100%"/>

<img src="http://image3.mouthshut.com/images/imagesp/925759937s.png" width="200" height="100"/>

Login

User Name

<input class="input" type="text"/>

Password

<input class="input" type="text"/>

Submit

@All Right Res

@All Right Res

=
4/12/17 Home

.rcontent{

padding-top:15px;

}

.rheader{

}

.rbody{

margin-top:50px;

}

.button {

background-color: #4BAE49;

border: none;

color: white;

padding: 12px 63px;

text-align: center;

text-decoration: none;

display: inline-block;

font-size: 16px;

}

.input{

height: 40px;

width: 250px;

}

.rfooter{

color: #CEC635;

background-color: black;

height: 36px;

margin-top: 15px;

}

Hello world!

<P>The time on the server is ${serverTime}.</P>

<img src="C:\work\1.png" width="300" height="75"/>

<img src="C:\work\2.jpg" width="50%" height="50%"/>

<img src="C:\work\3.jpg" width="250" height="50"/> LOGIN

User Name

<input class="input" type="text"/>

Password

<input class="input" type="text"/>

Submit

@Updated By Arvind 4th December 2017  Vidal Health Insurance Addminister

=== 4/12

Home

.rcontent{

padding-top:15px;

}

.rheader{

}

.rbody{

margin-top:50px;

}

.button {

background-color: #4BAE49;

border: none;

color: white;

padding: 12px 63px;

text-align: center;

text-decoration: none;

display: inline-block;

font-size: 16px;

}

.input{

height: 40px;

width: 250px;

}

.rfooter{

color: #CEC635;

background-color: black;

height: 36px;

margin-top: 15px;

}

Hello world!

<P>The time on the server is ${serverTime}.</P>

<img src="C:\work\1.png" width="300" height="75"/>

<img src="C:\work\2.jpg" width="50%" height="50%"/>

<img src="C:\work\3.jpg" width="250" height="50"/> LOGIN

User Name

<input class="input" type="text"/>

Password

<input class="input" type="text"/>

Submit

@Updated By Arvind 4th December 2017  Vidal Health Insurance Addminister

== ===

Home

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">

.rcontent{

padding-top:15px;

}

.rheader{

}

.rbody{

margin-top:50px;

}

.button {

background-color: #4BAE49;

border: none;

color: white;

padding: 12px 63px;

text-align: center;

text-decoration: none;

display: inline-block;

font-size: 16px;

}

.input{

height: 40px;

width: 250px;

}

.rfooter{

color: #CEC635;

background-color: black;

height: 36px;

margin-top: 15px;

}

Hello world!

<P>The time on the server is ${serverTime}.</P>

<img src="http://image3.mouthshut.com/images/imagesp/925759937s.png" width="300" height="200"/>

<img src="https://www.vidalhealth.ae/sites/default/files/styles/1200x570/public/slider-img/Scale.jpg?itok=HCypQFjr" width="100%" height="100%"/>

<img src="http://image3.mouthshut.com/images/imagesp/925759937s.png" width="200" height="100"/>

LOGIN

User Name

<input  class="input" type="text"/>

Password

<input class="input" type="text"/>

Submit

@Updated By Arvind 4th December 2017   Vidal Health Insurance Addminister