Spring 3 MVC Controller With Request Mapping Annotation


web.xml
<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
         id="WebApp_ID" version="2.5">  
    <display-name>Spring3MVC</display-name>  
  
    <welcome-file-list>  
        <welcome-file>index.jsp</welcome-file>  
    </welcome-file-list>  
  
    <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>/</url-pattern>  
    </servlet-mapping>  
  

</web-app>
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring WebMVC</title>
        <style type="text/css">
            *{
                font-family: arial;
                font-size: 13px;
            }
        </style>
    </head>
    <body>
    <center>
        <div > <a href="client/home/rampukar">Say Hello</a></div>
    </cen
</body>
</html>

Controller:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cloud.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/client")
public class HelloWorldController {

    @RequestMapping("/home/{username}")
    public ModelAndView homePage(@PathVariable("username") String getUser) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("firstname", getUser);
        mv.addObject("middlename", "Pukar ");
        mv.addObject("lastname", "Chaudhary");
        return mv;
    }

    @RequestMapping("/about")
    public ModelAndView aboutPage() {
        ModelAndView mv = new ModelAndView("about");
        mv.addObject("firstname", "About ");
        mv.addObject("middlename", "Pukar ");
        mv.addObject("lastname", "Chaudhary");
        return mv;
    }
}

hello.jsp;
<html>  
    <head>  
        <title>Java Guys | Home</title>  
    </head>  
    <body style="font-family:Segoe Print ">  
    <center>  
        <div >  ${firstname}</a></div>  
    </center>  
</body>  
</html>  

about.jsp:
<%-- 
    Document   : about
    Created on : Apr 19, 2016, 9:08:36 AM
    Author     : Pukar
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>about page</h1>
    </body>
</html>

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment