SPRINGMVC Controller Configuration Method Summary

Source: Internet
Author: User

The first type: A bean that does not configure a controller. (How to annotate)

In Dispatch-servlet.xml, configure the Context:component-scan node under the Beans node

<!--enable Spring annotations--><context:component-scan base-package= "Com.loong.controller"/>

In this way, you use @requestmapping ("Hello") on the Controller object to specify the path that the method corresponds to, such as two ways:

Example 1:url:http://localhost:8080/demo1/welcome/hello

@Controller @RequestMapping ("/welcome") public class Helloworldcontroller {     @RequestMapping (value= "/hello", method=requestmethod.get) Public     String Printwelcome (Modelmap model) {         Model.addattribute ("message", " Spring 3 MVC Hello World ");         return "Hello";     } }

Example 2:url:http://localhost:8080/demo1/home

@Controller public class HomeController {    @RequestMapping ("Home") public     String Home () {         return "home";     }}

  

The second way to configure the controller's bean (URL matching bean)
In the Xxx-servlet.xml

<context:component-scan base-package= "Com.controller"/> <!--handlermapping--><bean class= " Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "/> <!--handleradapter--><bean class= "Org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/><!--processor--><bean name= "/ Hello "class=" Com.controller.HelloWorldController "/>

Beannameurlhandlermapping: Represents the URL of the request and the Bean name Mapping, such as the URL is "context/hello", the spring configuration file must have a bean named "/hello", the context is ignored by default.

Simplecontrollerhandleradapter: Indicates that all beans that implement the Org.springframework.web.servlet.mvc.Controller interface can act as spring web The processor in MVC. If other types of processors are required, they can be resolved by implementing hadleradapter.

Tip: For questions on writing handlermapping and Handleradapter, refer to:
http://www.imooc.com/article/4293

In this case the Controller class will implement the Controller interface:

public class Helloworldcontroller implements Controller {@Overridepublic Modelandview handlerequest ( HttpServletRequest req, HttpServletResponse resp) throws Exception {//TODO auto-generated method stub    Modelandview m v = new Modelandview ();    Mv.addobject ("message", "HelloWorld");    Mv.setviewname ("Hello");    return MV;}}

  

The third way to configure the bean in the controller, using a unified configuration collection, the corresponding controller for each URL to do a relational mapping

In the Xxx-servlet

<bean id= "Logincontroller" class= "Com.msm2.controller.LoginController"/><bean id= "Maincontroller" class= " Com.msm2.controller.MainController "/><bean id=" handlermapping "class=" Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping "><property name=" mappings "><props ><prop key= "Login" >logincontroller</prop><prop key= "Userlogin" >mainController</prop> </props></property></bean>

In the Controller class
Url:http://localhost:8080/msm2/login

@Controllerpublic class Logincontroller extends abstractcontroller{@Overrideprotected Modelandview Handlerequestinternal (HttpServletRequest request, httpservletresponse response) throws Exception {     Modelandview Mav = new Modelandview ();    Mav.addobject ("login");    return MAV;}}

Original address: http://www.cnblogs.com/ysloong/p/6388962.html

SPRINGMVC Controller Configuration Method Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.