Tuesday 27 February 2018

14.0 SPRING MVC FORMS

Dealing with FORM TAGS (DOWNLOAD)





















-------------------------------------------------------------------------------------
home.jsp
-------------------------------------------------------------------------------------
<html>
<head>
<title>HOME</title>
</head>
<body>
<H1>Welcome to Form DEMO - <a href="home">HOME</a></H1>
</body>
</html>


-------------------------------------------------------------------------------------
form.jsp
-------------------------------------------------------------------------------------
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Form</title>
</head>
<body bgcolor="grey">
<center>
<form:form action="processForm" modelAttribute="student">
<table border="0" width="40%" cellpadding="5px" cellspacing="5px" style="border-collapse: collapse;background-color: white">
<tr><td colspan="2" align="center"><h3>STUDENT FORM</h3></td></tr>
<tr><td>FirstName</td><td><form:input path="firstName" /></td></tr>
<tr><td>LastName</td><td><form:input path="lastName" /></td></tr>
<tr><td>Country</td>
<td>
<form:select path="country">
<form:options items="${student.countryOptions}" />
</form:select>
</td></tr>
<tr><td>Best Coding Time </td>
<td>
<form:radiobutton path="coding" value="Day" />Day
<form:radiobutton path="coding" value="Night" />Night
</td></tr>
<tr><td>Which Editor you use</td>
<td>
<form:checkbox path="editor" value="Notepad++" />Notepad++
<form:checkbox path="editor" value="Edit++" />Edit+
<form:checkbox path="editor" value="Sublime" />Sublime
<form:checkbox path="editor" value="Comodo" />Comodo
</td></tr>
<tr><td></td><td><input type="submit" value="SUBMIT"></td></tr>
</table>
</form:form>
</center>
</body>
</html>


-------------------------------------------------------------------------------------
output.jsp
-------------------------------------------------------------------------------------
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>OUTPUT</title>
</head>
<body bgcolor="grey">
<center>
<table border="0" width="40%" cellpadding="5px" cellspacing="5px" style="border-collapse: collapse;background-color: white">
<tr><td colspan="2" align="center"><h3>STUDENT FORM DATA</h3><hr></td></tr>
<tr><td>FirstName</td><td>${student.firstName}</td></tr>
<tr><td>LastName</td><td>${student.lastName}</td></tr>
<tr><td>Country</td><td>${student.country}</td></tr>
<tr><td>Best Coding Time</td><td>${student.coding}</td></tr>
<tr><td valign="top">Which Editor you use</td>
<td>
<ul><ud>My Favorites</ud>
<c:forEach var="temp" items="${student.editor}">
<li>${temp}</li>
</c:forEach>
</ul>
</td></tr>
</table>
</center>
</body>
</html>


-------------------------------------------------------------------------------------
spring-mvc-demo-servlet.xml
-------------------------------------------------------------------------------------
<?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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.spring.demo.mvc" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>



-------------------------------------------------------------------------------------
web.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>spring-mvc-demo</display-name>

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>



-------------------------------------------------------------------------------------
Student.java
-------------------------------------------------------------------------------------
package com.spring.demo.mvc;

import java.util.LinkedHashMap;

public class Student {
private String firstName;
private String lastName;
private String country;
private String coding;
private String[] editor;
private LinkedHashMap<String,String> countryOptions;
public Student(){
countryOptions=new LinkedHashMap<>();
countryOptions.put("IND", "India");
countryOptions.put("PAK", "Pakistan");
countryOptions.put("UK", "United Kingdom");
countryOptions.put("AUS", "Australia");
countryOptions.put("CHN", "China");
countryOptions.put("SHR", "Shrilanka");
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public LinkedHashMap<String, String> getCountryOptions() {
return countryOptions;
}

public String getCoding() {
return coding;
}

public void setCoding(String coding) {
this.coding = coding;
}

public String[] getEditor() {
return editor;
}

public void setEditor(String[] editor) {
this.editor = editor;
}


}



-------------------------------------------------------------------------------------
StudentController
-------------------------------------------------------------------------------------
package com.spring.demo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class StudentController {

@RequestMapping("/")
public String showHome(){
return "home";
}
@RequestMapping("/home")
public String showForm(Model model){
Student theStudent=new Student();
model.addAttribute("student",theStudent);
return "form";
}
@RequestMapping("/processForm")
public String formProcess(@ModelAttribute("student") Student theStudent)
{
return "output";
}
}




-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------







Read More »

13.0 MVC SPRING FIRST PROGRAM


Controller Creation demo (download)






















-----------------------------------------------------------------------------------
TestController.java
-----------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
@RequestMapping("/")
public String displayForm(){
return "home";
}
}


-----------------------------------------------------------------------------------
home.jsp
-----------------------------------------------------------------------------------
<html>
<body>
<h1>SPRING CONFIG TEST FIRST PROGRAM</h1>
</body>
</html>


-----------------------------------------------------------------------------------
web.xml
-----------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>spring-mvc-demo</display-name>

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


-----------------------------------------------------------------------------------
spring-mvc-demo-servlet.xml
-----------------------------------------------------------------------------------
<?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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.spring.demo" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>



-----------------------------------------------------------------------------------
spring-mvc-demo-servlet.xml
-----------------------------------------------------------------------------------














Example 2 : How to catch form data




















-----------------------------------------------------------------------------------
form.jsp
-----------------------------------------------------------------------------------
<html>
<body>
<H2>Welcome to Spring form testing</H2>
<form method="GET" action ="formProcess">
<input type="text" name="uname" placeholder="What is your name ?">
<input type="submit" value="Login">
</form>
</body>
</html>

-----------------------------------------------------------------------------------
home.jsp
-----------------------------------------------------------------------------------
<html>
<body>
<h1>SPRING CONFIG TEST FIRST PROGRAM</h1>
<hr>
<h3><a href="form">User Form</a></h3>
</body>
</html>

-----------------------------------------------------------------------------------
output.jsp
-----------------------------------------------------------------------------------
<html>
<body>
<h2>Welcome to the final result page</h2>
<h3>Hello ${param.uname}</h3>
</body>
</html>

-----------------------------------------------------------------------------------
TestController
-----------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
@RequestMapping("/")
public String displayForm(){
return "home";
}

@RequestMapping("/form")
public String showForm(){
return "form";
}

@RequestMapping("/formProcess")
public String Process(){
return "output";
}
}



-----------------------------------------------------------------------------------
OUTPUT
-----------------------------------------------------------------------------------




























Example 2 : How to catch form data using request dispatcher


-----------------------------------------------------------------------------------
TestController.java
-----------------------------------------------------------------------------------
package com.spring.demo;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TestController {
@RequestMapping("/")
public String displayForm(){
return "home";
}
@RequestMapping("/form")
public String showForm(){
return "form";
}
@RequestMapping("/formProcess")
public String Process(){
return "output";
}
@RequestMapping("/formProcessFirstMethod")
public String processFirstMethod(HttpServletRequest request,Model model)
{
String name=request.getParameter("uname");
name=name.toUpperCase();
String result="UPPER CASE : "+name;
model.addAttribute("message",result);
return "output";
}
@RequestMapping("/formProcessSecondMethod")
public String processSecondMethod(@RequestParam("uname") String name,Model model)
{
name=name.toUpperCase();
String result="UPPER CASE : "+name;
model.addAttribute("message",result);
return "output";
}
}


-----------------------------------------------------------------------------------
form.jsp
-----------------------------------------------------------------------------------

<html>
<body>
<H2>Welcome to Spring form testing</H2>
<form method="GET" action ="formProcess">
<input type="text" name="uname" placeholder="What is your name ?">
<input type="submit" value="Login">
</form>
<hr>
<H2>Welcome to Spring form testing Request parameter</H2>
<form method="GET" action ="formProcessFirstMethod">
<input type="text" name="uname" placeholder="What is your name ?">
<input type="submit" value="Login">
</form>
<hr>
<H2>Welcome to Spring form testing Request parameter Second method</H2>
<form method="GET" action ="formProcessSecondMethod">
<input type="text" name="uname" placeholder="What is your name ?">
<input type="submit" value="Login">
</form>
</body>
</html>

-----------------------------------------------------------------------------------
output.jsp
-----------------------------------------------------------------------------------
<html>
<body>
<h2>Welcome to the final result page</h2>
<h3>Hello ${param.uname}</h3>
<h3>${message}</h3>
<h3>${message}</h3>
</body>
</html>

-----------------------------------------------------------------------------------
OUTPUT
-----------------------------------------------------------------------------------

Read More »

12.0 SPRING MVC PROJECT SETUP

Create a Dynamic Web Project (DOWNLOAD)

To create a new dynamic Web project in Eclipse:
  1. On the main menu select File > New > Project....
  2. In the upcoming wizard choose Web > Dynamic Web Project.

































Put all the jars in lib folder in web-inf (You can download the project having all the supported jars in link above)

------------------------------------------------------------------------------------------------------------------------------------
web.xml
-------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>spring-mvc-demo</display-name>

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>



------------------------------------------------------------------------------------------------------------------------------------
spring-mvc-demo-servlet.xml
------------------------------------------------------------------------------------------------------------------------------------

<?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: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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>








Read More »