Spring MVC Tiles

Spring MVC Tiles:

Spring furnishes joining support with apache tiles system. So we can essentially deal with the design of the spring mvc application by the assistance of spring tiles bolster.

Favorable position of Tiles bolster in Spring MVC:

Reusability: We can reuse a solitary part in numerous pages like header and footer segments.

Centralized control:  We can control the format of the page by a solitary layout page as it were.

Spring MVC Tiles
Required Jar files
  • Spring Core jar files
  • Spring Web jar files
  • Tiles jar files

Index Page:

index.jsp

<a href=”hello.html”>Hello Spring</a> |

<a href=”contact.html”>Contact</a>

 Controller classes

HelloWorldController.java

package com.javatpoint.controller;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.servlet.ModelAndView;

@Controller

public class HelloWorldController {

@RequestMapping(“/hello”)

public ModelAndView helloWorld() {

String message = “Hello World, Spring MVC @ Javaspot”;

return new ModelAndView(“hello”, “message”, message);

}

}

ContactController.java

package com.javaspot.controller;

import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.ModelAttribute;

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

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.SessionAttributes;

import org.springframework.web.servlet.ModelAndView;

import com.javaspot.form.Contact;

@Controller

@SessionAttributes

public class ContactController {

@RequestMapping(value = “/addContact”, method = RequestMethod.POST)

public String addContact(@ModelAttribute(“contact”) Contact contact, BindingResult result) {

//write the code here to add contact

return “redirect:contact.html”;

}

@RequestMapping(“/contact”)

public ModelAndView showContacts() {

return new ModelAndView(“contact”, “command”, new Contact());

}

}

Form class

Contact.java

package com.javaspot.form;

public class Contact {

private String firstname;

private String lastname;

private String email;

private String telephone;

//getters and setters

}

web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app version=”2.5″

xmlns=”http://java.sun.com/xml/ns/javaee”

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

xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>

<servlet>

<servlet-name>spring</servlet-name>

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

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>spring</servlet-name>

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

</servlet-mapping>

</web-app>

spring-servlet.xml      

<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”

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:component-scan

base-package=”com.javaspot.controller” />

<bean id=”viewResolver”

class=”org.springframework.web.servlet.view.UrlBasedViewResolver”>

<property name=”viewClass”>

<value>

org.springframework.web.servlet.view.tiles2.TilesView

</value>

</property>

</bean>

<bean id=”tilesConfigurer”

class=”org.springframework.web.servlet.view.tiles2.TilesConfigurer”>

<property name=”definitions”>

<list>

<value>/WEB-INF/tiles.xml</value>

</list>

</property>

</bean>

</beans>

tiles.xml file

tiles.xml

<?xml version=”1.0″ encoding=”UTF-8″ ?>

<!DOCTYPE tiles-definitions PUBLIC

“-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN”

“http://tiles.apache.org/dtds/tiles-config_2_0.dtd”>

<tiles-definitions>

<definition name=”base.definition”

template=”/WEB-INF/jsp/layout.jsp”>

<put-attribute name=”title” value=”” />

<put-attribute name=”header” value=”/WEB-INF/jsp/header.jsp” />

<put-attribute name=”menu” value=”/WEB-INF/jsp/menu.jsp” />

<put-attribute name=”body” value=”” />

<put-attribute name=”footer” value=”/WEB-INF/jsp/footer.jsp” />

</definition>

<definition name=”contact” extends=”base.definition”>

<put-attribute name=”title” value=”Contact Manager” />

<put-attribute name=”body” value=”/WEB-INF/jsp/contact.jsp” />

</definition>

<definition name=”hello” extends=”base.definition”>

<put-attribute name=”title” value=”Hello Spring MVC” />

<put-attribute name=”body” value=”/WEB-INF/jsp/hello.jsp” />

</definition>

</tiles-definitions>

Remoting in Spring
Spring structure makes the development of remote-empowered administrations simple. It spares a considerable measure of code by giving its own particular API.

Preferred standpoint of Spring Remoting

The software engineer needs to focus on business rationale just not plumbing exercises, for          beginning and halting the server.

Spring system underpins following remoting advancements

  • Remote Method Invocation (RMI)
  • Spring’s HTTP invoker
  • Hessian
  • Burlap
  • JAX-RPC (J2EE 1.4 API)
  • JAX-WS (Java EE 5 and Java EE 6 API)
  • JMS
Spring and RMI Integration

Spring RMI gives you a chance to uncover your administrations through the RMI framework. Spring gives a simple approach to run RMI application by the assistance of

org.springframework.remoting.rmi.RmiProxyFactoryBean org.springframework.remoting.rmi.RmiServiceExporterclasses.

 RmiServiceExporter

It gives the exportation administration to the rmi protest. This administration can be gotten to by means of plain RMI or RmiProxyFactoryBean.

RmiProxyFactoryBean

It is the production line bean for Rmi Proxies. It uncovered the proxied benefit that can be utilized as a bean reference.And Combination of the resources in a spring is with a RMI proxyFactory Bean.

Spring Remoting by HTTP Invoker

Spring gives its own execution of remoting administration known as HttpInvoker. It can be utilized for http ask for than RMI and functions admirably over the firewall.

By the assistance of HttpInvokerServiceExporter and HttpInvokerProxyFactoryBean classes, we can execute the remoting administration gave by Spring’s Http Invokers.

Http Invoker and Other Remoting procedures

You can utilize numerous Remoting strategies, how about we see which one can be best for you.

Http Invoker Vs RMI

RMI utilizes JRMP convention though Http Invokes utilizes HTTP convention. Since big business applications generally utilize http convention, it is the better to utilize HTTP Invoker. RMI additionally has some security issues than HTTP Invoker. HTTP Invoker functions admirably crosswise over firewalls.

Http Invoker Vs Hessian and Burlap

HTTP Invoker is the piece of Spring system yet Hessian and burlap are exclusive. All functions admirably crosswise over firewall. Hessian and Burlap are convenient to coordinate with different dialects, for example, .Net and PHP yet HTTP Invoker can’t be.

Spring Remoting by Hessian Example

By the assistance of HessianServiceExporter and HessianProxyFactoryBean classes, we can execute the remoting administration gave by hessian.

Favorable position of Hessian

Hessian functions admirably crosswise over firewall. Hessian is convenient to coordinate with different dialects, for example, PHP and .Net.
 Example of Remoting by Hessian
  1. Calculation.java
  2. CalculationImpl.java
  3. web.xml
  4. hessian-servlet.xml
  5. client-beans.xml
  6. Client.java

1) Calculation.java

package com.javaspot;

public interface Calculation {

int cube(int number);

}

2) CalculationImpl.java

package com.javaspot;

public class CalculationImpl implements Calculation{

public int cube(int number) {

return number*number*number;

}

}

3) web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app version=”2.5″

xmlns=”http://java.sun.com/xml/ns/javaee”

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

xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>

<servlet>

<servlet-name>hessian</servlet-name>

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

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>hessian</servlet-name>

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

</servlet-mapping>

</web-app>

4) hessian-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”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd”>

<bean id=”calculationBean” class=”com.javaspot.CalculationImpl”></bean>

<bean name=”/Calculation.http”

class=”org.springframework.remoting.caucho.HessianServiceExporter”>

<property name=”service” ref=”calculationBean”></property>

<property name=”serviceInterface” value=”com.javaspot.Calculation”></property>

</bean>

</beans>

5) client-beans.xml

  1. . serviceUrl
  1. serviceInterface

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans”

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

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd”>

<bean id=”calculationBean”

class=”org.springframework.remoting.caucho.HessianProxyFactoryBean”>

<property name=”serviceUrl”

value=”http://localhost:8888/hessian/Calculation.http”></property>

<property name=”serviceInterface” value=”com.javaspot.Calculation”></property>

</bean>

</beans>

6) Client.java

package com.javaspot;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

public static void main(String[] args){

ApplicationContext context = new ClassPathXmlApplicationContext(“client-beans.xml”);

Calculation calculation = (Calculation)context.getBean(“calculationBean”);

System.out.println(calculation.cube(5));

}

}

Spring Remoting by Burlap

Both, Hessian and Burlap are given by Coucho. Burlap is the xml-based option of Hessian.

By the assistance of BurlapServiceExporter and BurlapProxyFactoryBean classes, we can actualize the remoting administration gave by burlap.

Spring and JMS Integration

To coordinate spring with JMS, you have to make two applications.

JMS Receiver Application

JMS Sender Application

To make JMS application utilizing spring, we are utilizing Active MQ Server of Apache to make the Queue.

Required Jar Files

1) You have to include spring center, spring misc,spring aop, spring j2ee and spring industriousness center jug documents.

2) Add activemqall5.9.jar record situated inside the activemq index.

Spring Java Mail

Spring structure gives numerous valuable interfaces and classes for sending and getting sends. The org.springframework.mail bundle is the root bundle that gives mail bolster in Spring structure.

 

Java Mail API1) MailMail.java

package com.javaspot;

import org.springframework.mail.MailSender;

import org.springframework.mail.SimpleMailMessage;

public class MailMail{

private MailSender mailSender;

public void setMailSender(MailSender mailSender) {

this.mailSender = mailSender;

}

public void sendMail(String from, String to, String subject, String msg) {

//creating message

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(from);

message.setTo(to);

message.setSubject(subject);

message.setText(msg);

//sending message

mailSender.send(message);

}

}

2) applicationContext.xml

host

username

password

javaMailProperties

<?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”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

 

<bean id=”mailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl”>

<property name=”host” value=”smtp.gmail.com” />

<property name=”username” value=”yourgmailid@gmail.com” />

<property name=”password” value=”yourgmailpassword” />

<property name=”javaMailProperties”>

<props>

<prop key=”mail.smtp.auth”>true</prop>

<prop key=”mail.smtp.socketFactory.port”>465</prop>

<prop key=”mail.smtp.socketFactory.class”>javax.net.ssl.SSLSocketFactory</prop>

<prop key=”mail.smtp.port”>465</prop>

</props>

</property>

</bean>

<bean id=”mailMail” class=”com.javaspot.MailMail”>

<property name=”mailSender” ref=”mailSender” />

</bean>

</beans>

 

3) Test.java

package com.javaspot;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.*;

public class Test {

public static void main(String[] args) {

Resource r=new ClassPathResource(“applicationContext.xml”);

BeanFactory b=new XmlBeanFactory(r);

MailMail m=(MailMail)b.getBean(“mailMail”);

String sender=”sendergmailid@gmail.com”;//write here sender gmail id

String receiver=”receiveremailid@gmail.com”;//write here receiver id

m.sendMail(sender,receiver,”hi”,”welcome”);

System.out.println(“success”);

}

}

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.