0

I am learning spring MVC nowadays for new project.There I came across @RequestMapping annotations for how requests are mapped with URL.

I want to understand that , How request and URL is mapped previously without use of this annotation? How it is configured in XML files?

Controller code:

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello(Model model){
        model.addAttribute("greeting","Helloworld");
        return "hello";
    }
}

servlet-config.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"
xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <bean name="/greeting.html" class="com.pj.controller.HelloController"/>

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
</beans>

web.xml

<servlet>
  <servlet-name>fitTrackerServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/servlet-config.xml</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>fitTrackerServlet</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>
S M
  • 100
  • 12
  • Why are you looking into this kind of stuff? Declaring mappings via XML is not used and also seems to have been removed from Spring's reference manual but anyways if we want to have a look you could check here: https://stackoverflow.com/questions/4481373/requestmapping-in-xml – akortex Jan 02 '20 at 14:31
  • If you're interested there a number of resources online that tell you how to map spring controllers using xml: https://www.baeldung.com/spring-xml-vs-java-config. But yeah, annotation defined controllers are the way to go. – mohammedkhan Jan 02 '20 at 14:33
  • @Aris_Kortex In the project I am working with,they are not using annotations.The project is very old and they are using old techniques ,so I have to understand it. I know that using annotations is nowadays good practice. – S M Jan 02 '20 at 14:33
  • @SM, what kind of project is it? How ancient is it? I suggest you guys invest on getting everything up to date. – akortex Jan 02 '20 at 14:34

1 Answers1

0

Check this out: Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)

Nowadays,

  1. RequestMapping is used to map the endpoint of your controller.
  2. For the http verbs, there are specific mapping annotations like
    • @Getmapping,
    • @PostMapping,
    • @PutMapping...
Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33