Faisal
Faisal

Reputation: 179

how to call request mapping method level through class level RequestMapping

I did one simple program using spring. I got answer for method level RequestMapping when i did not use class level RequestMapping. But i want to use both class level and method level RequestMapping.

This is my controller code

package com.birthid;

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

@Controller
@RequestMapping("/classLevel")
public class Controaller1 
{
     @RequestMapping("/spring")
     public ModelAndView display(@RequestParam("name") String name)
     {
         ModelAndView model=new ModelAndView("view");
         model.addObject("msg", name);
         return model;
     }      
}

html code

<html>
<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   <title>Hello App Engine</title>
</head>

<body>
   <h1>valith web application!</h1>
   <form action="/classLevel" method="get">
      name:<input type="text" name="name"/><br>
      <input type="submit" value="clik me"/>
   </form>
</body>
</html>

when i give this url in address bar. i got exact output. http:localhost:8888/classLevel/spring?name=john

but when i press button where i design in html page, that is giving error.

Upvotes: 4

Views: 4107

Answers (2)

Ghasem Sadeghi
Ghasem Sadeghi

Reputation: 1854

As you know:
In the Spring MVC you can return view as a String or ModelAndView object.

IMPORTANT NOTE:
In both cases you have to pay attention to relative/absolute path:

  1. If you declare / in the beginning of the view name, you are using absolute path.
    Namely it does not matter class level @RequestMapping and directly introduce itself as the final view name.
  2. If you do not declare / in the beginning of the view name, you are using relative path (relative to the class path) and therefore it appends to the class level @RequestMapping to construct final view name.

So, you have to consider the above notes when use the Spring MVC.

Example:
1. create two HTML file test1.html and test2.html in the static folder of spring (boot) structure:
Please note that the class level @RequestMapping behaves as a folder path in the case of relative path.

--- resources
    --- static
        --- classLevelPath     //behaves as a folder when we use relative path scenario in view names   
            --- test2.html      //this will be used for relative path [case (2)]
        --- test1.html          //this will be used for absolute path [case (1)]

  1. create a controller class like as the below. This example shows different cases with return String and ModelAndView in both relative and absolute path.

@Controller
@RequestMapping("/classLevelPath")
public class TestController {

    //case(1)
    @RequestMapping("/methodLevelAbsolutePath1")
    public String absolutePath1(Model model){
        //model.addAttribute();
        //...
        return "/test1.html";  
    }

    //case(1)
    @RequestMapping("/methodLevelAbsolutePath2")
    public ModelAndView absolutePath2(Model model){
        ModelAndView modelAndView = new ModelAndView("/test1.html");
        //modelAndView.addObject()
        //....
        return modelAndView; 
    }

    //case(2)
    @RequestMapping("/methodLevelRelativePath1")
    public String relativePath1(Model model){
        //model.addAttribute();
        //...
        return "test2.html";
    }

    //case(2)
    @RequestMapping("/methodLevelRelativePath2")
    public ModelAndView relativePath2(Model model){
        ModelAndView modelAndView = new ModelAndView("test2.html");
        //modelAndView.addObject()
        //....
        return modelAndView;  
    }


}

Note:
You can specify the suffix of your view files by a ViewResolver (for example InternalResourceViewResolver or spring.mvc.view.suffix=.html in the appliction.properties file of Spring Boot and do not declare .html suffix in the above code.

Best Regard

Upvotes: 1

cнŝdk
cнŝdk

Reputation: 32145

Well simply the problem is with your form action, you have action="/classLevel" wich should be action="/classLevel/spring" because your method have /spring as RequestMapping so change :

<form action="/classLevel" method="get">

To :

<form action="/classLevel/spring" method="get">

Because as you did in your url test the method call should be : /classLevel/spring.

Take a look at Mapping Requests With @RequestMapping section of Spring Docs for further information.

Upvotes: 1

Related Questions