How to add a controller in Spring MVC 3?

I created a new Spring MVC 3 project using NetBean. But there is no way to add a new controller to the IDE.

+4
source share
2 answers

Well, adding a controller is as easy as adding a class annotated with

@Controller 

And specifying the package to be scanned from applicationContext.xml, which, in turn, is specified in web.xml. Something like that:

  <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/appServlet/applicationContext.xml </param-value> </context-param> 

in web.xml

Then in / WEB -INF / spring / appServlet / applicationContext.xml:

 <context:component-scan base-package="your.package" /> 

Of course you need the actual schema in your application Context.xml

 xmlns:context="http://www.springframework.org/schema/context" 

And at the circuit address:

 http://www.springframework.org/schema/context/spring-context-3.0.xsd 

And then the class:

 package your.package ..... @Controller MyController{ ..... 
+3
source

If you are using the annotation implementation of Spring, you don’t need to do anything special. Create a standard Java class inside the package that Spring is configured to scan. Then annotate the class using @Controller , then create your method and mappings using @RequestMapping .

In its simplest form, the controller will be something like this:

 @Controller public class MyClass { @RequestMapping("/myUrlMapping.do") public ModelAndView myMethod() { return new ModelAndView("myView"); } } 

It is assumed that you already have Spring configured.

+2
source

Source: https://habr.com/ru/post/1393012/


All Articles