0

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?

1 Answer 1

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.