Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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.jspHello Spring |Contact

 Controller classes

HelloWorldController.javapackage com.javatpoint.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class HelloWorldController {@RequestMapping("/hello")public ModelAndView helloWorld() {String message = "Hello World, Spring MVC @ Javaspot";return new ModelAndView("hello", "message", message);}}ContactController.javapackage 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@SessionAttributespublic class ContactController {@RequestMapping(value = "/addContact", method = RequestMethod.POST)public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) {//write the code here to add contactreturn "redirect:contact.html";}@RequestMapping("/contact")public ModelAndView showContacts() {return new ModelAndView("contact", "command", new Contact());}}

Form class

Contact.javapackage com.javaspot.form;public class Contact {private String firstname;private String lastname;private String email;private String telephone;//getters and setters}web.xmlspringclass>org.springframework.web.servlet.DispatcherServletclass>1spring*.htmlspring-servlet.xml      package="com.javaspot.controller" />class="org.springframework.web.servlet.view.UrlBasedViewResolver">org.springframework.web.servlet.view.tiles2.TilesViewclass="org.springframework.web.servlet.view.tiles2.TilesConfigurer">/WEB-INF/tiles.xmltiles.xml filetiles.xmlextends="base.definition">extends="base.definition">
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.javapackage com.javaspot;public interface Calculation {int cube(int number);}2) CalculationImpl.javapackage com.javaspot;public class CalculationImpl implements Calculation{public int cube(int number) {return number*number*number;}}3) web.xmlhessianclass>org.springframework.web.servlet.DispatcherServletclass>1hessian*.http4) hessian-servlet.xmlclass="com.javaspot.CalculationImpl">class="org.springframework.remoting.caucho.HessianServiceExporter">5) client-beans.xml
  1. . serviceUrl
  1. serviceInterface
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">6) Client.javapackage 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 BurlapBoth, 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 IntegrationTo coordinate spring with JMS, you have to make two applications.JMS Receiver ApplicationJMS Sender ApplicationTo make JMS application utilizing spring, we are utilizing Active MQ Server of Apache to make the Queue.Required Jar Files1) 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 MailSpring 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.javapackage 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 messageSimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(msg);//sending messagemailSender.send(message);}}2) applicationContext.xmlhostusernamepasswordjavaMailProperties class="org.springframework.mail.javamail.JavaMailSenderImpl">true465javax.net.ssl.SSLSocketFactory465class="com.javaspot.MailMail"> 3) Test.javapackage 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 idString receiver="receiveremailid@gmail.com";//write here receiver idm.sendMail(sender,receiver,"hi","welcome");System.out.println("success");}}