Creating a controller class

Go to src | main | java in the project and create the package name that we mentioned in spring-mvc-kotlin-servlet.xml. Assume that our package name is mvckotlin:

Create a controller .kt file. We call this MVCKotlinAppController.kt:

@Controller
class MVCKotlinAppController {
@RequestMapping("/greeting")
fun greetingMessage(): ModelAndView {
val message =
"<div style='text-align:center;'>" +
"<h3>Welcome to Learn Spring for Android Application Development</h3>" +
"</div>"
return ModelAndView("greeting", "message", message)
}
}

We have a class named MVCKotlinAppController.kt and annotated this with @Controller, which means that this class is a controller class. After initializing the project, Spring starts to search the bundle from here.

The @RequestMapping("/greeting") annotation will map a web request and /greeting will create a base URI. 

We have created a function named greetingMessage() that will return a ModelAndView object. Here we just create a sample HTML code for greeting. If we go to http://localhost:8080/greeting, this will return a view based on greetingMessage().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.113.229