Lcoder_001
Lcoder_001

Reputation: 153

Except @requestMapping what we can write in spring?

Here I don't want to write @requestMapping so except that what will write in spring.xml file. I want to know both scenarios like if I am not using @requestmapping both class level and method level what I have to write ?

Upvotes: 0

Views: 501

Answers (1)

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

To configure your SpringMVC there are two ways XML Config and Annotation Config :

  1. Configuration using XML (This is the old way, it's not recommended anymore) :

In spring-mvc-config.xml : here we mapped /hello to helloWorldController

<beans ...>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
        <props>
           <prop key="/hello">helloWorldController</prop>
         </props>
       </property>
    </bean>

    <bean id="helloWorldController" class="xx.yy.zz.HelloWorldController" />

</beans>

HelloWorldController should extends from AbstractController and implement handleRequestInternal() :

public class HelloWorldController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("hello");

        model.addObject("message", "HelloWorld!");

        return model; //will go to hello.jsp
    }
}
  1. The equivalent of this with Annotation config :
@Controller
public class HelloWorldController
{

    @RequestMapping("/hello")
    protected ModelAndView hello() throws Exception {

        ModelAndView model = new ModelAndView("hello");

        model.addObject("message", "HelloWorld!");

        return model; //will go to hello.jsp
    }
}

Upvotes: 2

Related Questions