mardi 21 juin 2016

pass multiple variables to the controller

I am trying to pass a country and name in the URL

http://localhost:8080/FirstSpringMVCProject/berlin/alex

but I am getting the following when typing the above URL:

Jun 21, 2016 1:18:37 AM org.springframework.web.servlet.PageNotFound noHandlerFound WARNUNG: No mapping found for HTTP request with URI [/FirstSpringMVCProject/berlin/alex] in DispatcherServlet with name 'spring-dispatcher'

and error 404.

How can I pass multiple variables in the URL? I have tried it just with one variable name in the URL and it worked.

spring-dispatcher-servlet.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:context="http://www.springframework.org/schema/context"
    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">

    <bean id="HandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <bean name="/welcome/{countryName}/{userName}" class="com.stack.HelloController" />

    <bean name="/hi" class="com.stack.HelloController" />

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

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

    </bean>

</beans>

HelloWorld class

package com.stack;

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.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(path="/welcome/{countryName}/{userName}", method=RequestMethod.GET)
    public ModelAndView helloWorld(@PathVariable(value="userName")  String name, @PathVariable(value="countryName")  String country) {
        ModelAndView model = new ModelAndView("HelloPage");
        model.addObject("msg", "Hello  " + name +" , you are from " +country );

        return model;
    }


    @RequestMapping("/hi")
    public ModelAndView hiWrld() {
        ModelAndView model = new ModelAndView("HelloPage");
        model.addObject("msg", "Hi world!");

        return model;
    }
}

HellpPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<!-- <title>Insert title here</title> -->
</head>

<h1> First Spring MVC Application Demo </h1>

<h2>${msg}</h2>


<body>

</body>
</html>

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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>FirstSpringMVCProject</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


</web-app>

enter image description here

Aucun commentaire:

Enregistrer un commentaire