The Time
The Time

Reputation: 737

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
WARNING: 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

Upvotes: 0

Views: 203

Answers (2)

Tahir Hussain Mir
Tahir Hussain Mir

Reputation: 2626

Check your Url :

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

but, you have mentioned welcome in the request mapping which is missing in the respected URL.
Hence there is no requestmapping which can resolve this url of your's.
Either add welcome to your browser url path
then your url will be

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

or remove welcome from the requestmapping.

then the requestmapping will be like this

@RequestMapping(path="/{countryName}/{userName}", method=RequestMethod.GET

Upvotes: 1

Vijendra Kumar Kulhade
Vijendra Kumar Kulhade

Reputation: 2265

You don't need to write bean like this in you dispatcher-servlet.xml as you are already putting annotations for it.

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

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

Your project should just work fine without it. In you web.xml file you need to add

<load-on-startup>1</load-on-startup>

This is allow your servlet container to initialize spring.

Upvotes: 1

Related Questions