Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Multiple Controller


We can have a lot of controller classes in Spring Framework. In this example, we are creating two Controller classes HelloWorldController and WelcomeWorldController.

1) Controller Classes

HelloWorldController.java

  1. package com.Kreationnext;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.servlet.ModelAndView;
  5. @Controller
  6. public class HelloWorldController {
  7.     @RequestMapping(“/hello”)
  8.     public ModelAndView helloWorld() {
  9.         String message = “HELLO SPRING MVC”;
  10.         return new ModelAndView(“hellopage”“message”, message);
  11.     }
  12. }

WelcomeWorldController.java

  1. package com.Kreationnext;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.servlet.ModelAndView;
  5. @Controller
  6. public class WelcomeWorldController {
  7.     @RequestMapping(“/welcome”)
  8.     public ModelAndView helloWorld() {
  9.         String message = “WELCOME SPRING MVC”;
  10.         return new ModelAndView(“welcomepage”“message”, message);
  11.     }
  12. }

2) View components

To run this example, It must be located inside the WEB-INF/jsp directory.

hellopage.jsp

  1. Message is: ${message}

welcomepage.jsp

  1. Message is: ${message}

3) Index page

It is the optional welcome page, that provide the links to invoke both controller.

index.jsp

  1. <a href=“hello.html”>click</a>|
  2. <a href=“welcome.html”>click</a>

Other pages are same e.g. spring-servlet.xml and web.xml.

download this example (developed using myeclipse IDE)

Spring MVC Login Example

We can simply create login application by following the Spring MVC. We need to pass HttpServletRequest and HttpServletResponse objects in the request processing method of the Controller class. Let’s see the example:

1) Controller Class

HelloWorldController.java

  1. package com.Kreationnext;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7. @Controller
  8. public class HelloWorldController {
  9.     @RequestMapping(“/hello”)
  10.     public ModelAndView helloWorld(HttpServletRequest request,HttpServletResponse res) {
  11.         String name=request.getParameter(“name”);
  12.         String password=request.getParameter(“password”);
  13.         if(password.equals(“admin”)){
  14.         String message = “HELLO “+name;
  15.         return new ModelAndView(“hellopage”“message”, message);
  16.         }
  17.         else{
  18.             return new ModelAndView(“errorpage”“message”,“Sorry, username or password error”);
  19.         }
  20.     }
  21. }

2) View components

To run this example, It must be located inside the WEB-INF/jsp directory.

hellopage.jsp

  1. Message is: ${message}

errorpage.jsp

  1. ${message}
  2. <jsp:include page=“/index.jsp”></jsp:include>

3) Index page

It is the login page, that recieve name and password from the user.

index.jsp

  1. <form action=“hello.html” method=“post”>
  2. Name:<input type=“text” name=“name”/><br/>
  3. Password:<input type=“password” name=“password”/><br/>
  4. <input type=“submit” value=“login”/>
  5. </form>

Other pages are same e.g. spring-servlet.xml and web.xml.


Spring MVC Form Example

By the help of form handling, we will be able to create CRUD application using Spring MVC.

Click here for more details about spring mvc form handling example.


Spring MVC CRUD Example

By the help of form handling and JdbcTemplate, we will be able to create CRUD application using Spring MVC.

Click here for more details about spring mvc crud example.


Spring MVC Pagination Example

Let’s see a simple example of pagination using Spring MVC.

Click here for more details about spring mvc pagination example.


Spring MVC File Upload Example

Let’s see a file upload application using Spring MVC.

Click here for more details about spring mvc file upload example.


Spring MVC Tiles Example

By the help of Tiles framework, we can manage the layout of the spring mvc web application.

Click here for more details about spring mvc example with tiles.

Comments

comments