Yellow Flash
Yellow Flash

Reputation: 872

Spring MVC with XML

I'm learning SpringMVC via Mkyong.com tutorials. In his tutorials, Maven is used to build the programs.

I'm not using Maven to build it in this programs.

My Sample Code:

Coffee.java

package com.springMVC;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "coffee")
public class Coffee {

    String name;
    int quanlity;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getQuanlity() {
        return quanlity;
    }

    @XmlElement
    public void setQuanlity(int quanlity) {
        this.quanlity = quanlity;
    }

    public Coffee(String name, int quanlity) {
        this.name = name;
        this.quanlity = quanlity;
    }

    public Coffee() {
    }

}

XMLController.java

package com.springMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springMVC.Coffee;

@Controller
@RequestMapping("/coffee")
public class XMLController {

    @RequestMapping(value="{name}", method = RequestMethod.GET)
    public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {

        Coffee coffee = new Coffee(name, 100);

        return coffee;

    }

mvc-dispacter-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.springMVC" />

    <mvc:annotation-driven />

</beans>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

my code structure:

Please give me a URI(example:https:\localhost:8080/MVCXML/rest/coffee/arabic) to view an output like in MkYong.com tutorial(http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/)

Upvotes: 1

Views: 968

Answers (1)

Kevin
Kevin

Reputation: 25269

Here's how the URL mapping works:

  1. web.xml routes urls with a prefix to some servlet, and you have mapped everything with prefix /rest/ to the spring DispatcherServlet.
  2. The controller routing is handled by the parameter to the @Controller annotation, which you've annotated with /coffee
  3. Then there's a variable at the end that is the name of the coffee, which gets placed in the @PathVariable.

So nowhere in there do I see /MVCXML/. mkyong.com didn't provide a web.xml so it's a little unclear what the prefix is supposed to be, if you don't already know how web.xml and Spring's dispatcher servlet work together.

The correct URI should be http://localhost:8080/rest/coffee/arabic.

Upvotes: 1

Related Questions