Laxmi Kadariya
Laxmi Kadariya

Reputation: 1103

creating controllers in spring MVC

I have two views named hello.jsp and hello_new.jsp. Does it mean that I need to create two seperate controllers as

hello.java

@Controller
public class hello {
    @RequestMapping("/hello_new")
    public ModelAndView helloWorld() {

        String message = "Hello World_new, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }
}

and hello_new.java

@Controller
public class Hello_new {
    @RequestMapping("/hello_new")
    public ModelAndView helloWorld() {

        String message = "Hello World_new, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello_new", "message", message);
    }
}

or is there any way creating a single controller can map this two views?

Upvotes: 0

Views: 719

Answers (1)

Stanley
Stanley

Reputation: 5137

No, you don't have to create different controller. Just create one controller and have multiple methods to handle different URL.

@Controller
public class hello {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }

    @RequestMapping("/hello_new")
    public ModelAndView helloWorldNew() {
        String message = "Hello World_new, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello_new", "message", message);
    }
}

Upvotes: 1

Related Questions