Detailed configuration SpringMVC-Controller (II)

1. implementation of the controller

SpringMVC, the Controller for arrangement there are many;

Controller is an interface, at org.springframework.web.servlet.mvc package, only one interface method;

We need a view resolver ModelAndView model parameters to resolve our view and handle.

Need to add springMVC-servlet.xml configuration file:

Handle mappers

  <! - Processing mapper -> 
    < the bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

Processing adapter

   <! - Processing Adapter -> 
    < the bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

View resolvers

  <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

Write controller

public class helloController {
    public ModelAndView hello(){

        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","hello,springmvc");
        mv.setViewName("hello");

           return mv;
    }

}

2. Through the implementation of annotations

We need to add springmvc-servlet.xml configuration file:

Notes Scanning

Spring can use the scanning mechanism to find all controller class annotation-based applications in order to ensure the Spring to find your controller, you declare the components in the configuration file scanning.

    <! - scanning the specified annotation packet, so that the designated class can be managed IOC container -> 
    < context: Scan-Component Base-Package = "org.west.controller" />

Static resource filter

    <-! Static resource filter -> 
    < MVC: default-the servlet-Handler />

Support for annotation-driven

    <-! Annotation-Driven: annotation-driven MVC support -> 
    < MVC: Annotation-Driven />

View resolvers

<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

We need to realize controller through three layers of processing (Model, View, Controller) configuration are indispensable.

The write controller

@Controller
public class HelloWorldController {

    //请求映射("路径")
    @RequestMapping("/hello")
    public String hi(Model model){
        model.addAttribute("msg","Hello,SpringMVC");
        System.out.println("进入HELLO");
        return "hello"; //WEB-INF/jsp/hello.jsp
    }

}

Notes Controller similar instance of the class declaration Spring, is a controller.

Different requests can point a view, but the result is not the same page of the result, the view is multiplexed, and the relationship between the weak coupling between the controller and the view.

3.Restful style

Restful is a style of software architecture, design, rather than the standard, but provides a set of design principles and constraints. It is mainly used for client and server interaction class software. Based on this style of software can be more concise, more structured and easier to implement caching mechanisms.

Popular in terms of my understanding, it does not require mass participation in Restful style as traditional as requested by "?" To mass participation, and we only need "/" on it.

The traditional way: https: //........add p1 = 1 & p2 = 2?

Restful style: https: //........add/p1/p2

Then write a controller to see it!

Write controller

@Controller
public class HelloWorldController {

    //请求映射("路径")
    @RequestMapping("/hello/{p1}/{p2}")
    public String hi(@PathVariable int p1,@PathVariable int p2, Model model){
        int sum=p1+p2;
        model.addAttribute("msg",sum);
        return "hello"; //WEB-INF/jsp/hello.jsp
    }

}

Note: We need a comment PathVariable our value map up.

We want to pass the transmission type of correspondence, or it may not visit the corresponding request method.

The advantage of using a variable path: the path becomes more concise; more convenient to obtain parameters, the framework will automatically type conversion. Can restrict access path variable parameters of the type, if not the same type, it is less than the corresponding access request method.

 

Guess you like

Origin www.cnblogs.com/xiaoqiqistudy/p/11320288.html