1.2 Writing an Annotation Based Controller Class -@Controller @RequestMapping
1.2 Writing an Annotation Based Controller Class -@Controller @RequestMapping
in
Creating Servlets in Eclipse 2
05/05/25
Creating Servlets in Eclipse 3
05/05/25
Simple Spring MVC Web App
05/05/25
This is the annotation based HelloController class
@Controller
@RequestMapping("/welcome")
model.addObject("msg","hello world");
return model;
Creating Servlets in Eclipse 5
} }
05/05/25
annotation based HelloController class
According to Spring MVC --------------
If the developer has mentioned @Controller
annotation on top of a java class --------
Then that class will be considered as a Spring
Controller.
Here there is no need to extend controller class from
any specific base class -----------
Like ---------- HelloController extends
AbstractController{
Creating Servlets in Eclipse 6
05/05/25
annotation based HelloController class
<bean name="/welcome.html"
class="com.techMentor.hellocontroller.HelloController
" />
Creating Servlets in Eclipse 7
05/05/25
annotation based HelloController class
Instead of the earlier <bean> entry ---------- we need
to put following entry in that configuration file.
<context : component-scan base-package =
"com.techMentor.hellocontroller" />
05/05/25
annotation based HelloController class
And also we are asking framework to consider all
of them are part of this .xml file.
If we do not mention this statement --------
We need to do the entry of every single controller
class in the .xml file
As we did in previous program for one controller class.
05/05/25
annotation based HelloController class
05/05/25
annotation based HelloController class
05/05/25
annotation based HelloController class
05/05/25
annotation based HelloController class
05/05/25
annotation based HelloController class
@RequestMapping("/welcome")
05/05/25
annotation based HelloController class
05/05/25
annotation based HelloController class
05/05/25
annotation based HelloController class
public class HelloController {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg","hello world"); return model;
}
05/05/25
annotation based HelloController class
The Front Controller now would find out the exact path
of the view which is present in the project
05/05/25
annotation based HelloController class
05/05/25
Multi - Action Controller class
05/05/25
Multi - Action Controller class
05/05/25
Multi - Action Controller class
05/05/25
So in this case ----- if a client is going
to type above URL ---------
05/05/25
Multi - Action Controller class
05/05/25
Multi - Action Controller class
05/05/25
Class Level Annotation
Now ---------- None of our earlier URL patterns will match because the
class level annotation will override the method level annotation
Creating Servlets in Eclipse 30
05/05/25
Class Level Annotation in Spring MVC
05/05/25
Multi - Action Controller class
05/05/25
Creating Servlets in Eclipse 33
05/05/25
Creating Servlets in Eclipse 34
05/05/25
Creating Servlets in Eclipse 35
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
So the URL
http://localhost:8181/FifthSpringMVCProject/welcome/countryName/userName
Is now used as
http://localhost:8181/FifthSpringMVCProject/welcome/countryName/sachin
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
This annotation can be placed inside the code as
follows….
public ModelAndView
helloWorld(@PathVariable(“userName”) String name {
name variable
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
http://localhost:8181/FifthSpringMVCProject/welcome/countryName/sachin
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
Creating Servlets in Eclipse 51
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25
Creating Servlets in Eclipse 54
05/05/25
Use of one more Annotation
05/05/25
Use of one more Annotation
05/05/25