我是靠谱客的博主 慈祥戒指,最近开发中收集的这篇文章主要介绍码农小汪-spring框架学习之11-Spring MVC简介MVCThe request processing workflow in Spring Web MVC (high level) 处理的流程默认DispatcherServlet配置实现控制器Mapping Requests With @RequestMapping 映射请求URI Template Patterns,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

MVC

是一种框架模式,模块化我们的不同的模块,更加的利于开发!、

Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central Servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’s DispatcherServlet however, does more than just that. It is completely integrated with the Spring IoC container and as such allows you to use every other feature that Spring has.(Spring的web MVC框架,就像许多其他web MVC框架,request-driven, 围绕一个中心分派请求的Servlet控制器设计和报价 其他功能,促进了web应用程序的开发)

我们看看基本的模型图案
这里写图片描述

The request processing workflow in Spring Web MVC (high level) 处理的流程

The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application. You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping in the same web.xml file. This is standard Java EE Servlet configuration; the following example shows such a DispatcherServlet declaration and mapping:
所有的请求都是从这里进入我们的容器中的,这个使我们的分发控制器哦!

<web-app>
    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>/example/*</url-pattern>
    </servlet-mapping>

</web-app>

默认DispatcherServlet配置

每个特殊beanDispatcherServlet维护一个列表,默认实现使用。 这些信息保存在 文件DispatcherServlet.properties在包org.springframework.web.servlet。

实现控制器

控制器提供你通常定义的应用程序行为 一个服务接口。 控制器解释用户的输入并将其转换成一个模型 表示给用户的视图。 弹簧实现了控制器在一个非常 抽象的方式,它使您能够创建一个广泛的控制器。

Spring 2.5引入了一个基于注解的MVC控制器的编程模型 使用注释等@RequestMapping,@RequestParam,@ModelAttribute,所以 上。 这个注释支持Servlet MVC和Portlet(窗口;门户组件;门户件;管理;信息组件;) MVC。
下面就是个简单的实例

@Controller
public class HelloWorldController {

    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
}

Mapping Requests With @RequestMapping 映射请求

You use the @RequestMapping annotation to map URLs such as /appointments onto an entire class or a particular handler method. (放在我们类上面或者我们的请求的方法上面) 放在我们的类上面就类似Structs namespac 命名空间
a specific HTTP method request method (“GET”, “POST”, etc.) or an HTTP request parameter condition.
看不懂的慢慢的来看就好了啊!

@Controller
@RequestMapping("/appointments")
public class AppointmentsController {

    private final AppointmentBook appointmentBook;

    @Autowired
    public AppointmentsController(AppointmentBook appointmentBook) {
        this.appointmentBook = appointmentBook;
    }

    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Appointment> get() {
        return appointmentBook.getAppointmentsForToday();
    }

    @RequestMapping(value="/{day}", method = RequestMethod.GET)
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
        return appointmentBook.getAppointmentsForDay(day);
    }

    @RequestMapping(value="/new", method = RequestMethod.GET)
    public AppointmentForm getNewForm() {
        return new AppointmentForm();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String add(@Valid AppointmentForm appointment, BindingResult result) {
        if (result.hasErrors()) {
            return "appointments/new";
        }
        appointmentBook.addAppointment(appointment);
        return "redirect:/appointments";
    }

URI Template Patterns

URI 模板大大的方便了@RequestMapping 方法中 URL 配置。 URI 模板是类URI 字串,包含一个或多个变量名,为变量设置值时,它就成了 URI。
比如, URI 模板 http://www.example.com/users/{userId}包含一个变量 userId,设置 userId 变量的 值为 fred, http://www.example.com/users/fred。
In Spring MVC you can use the @PathVariable annotation on a method argument to bind it to the value of a URI template variable: @PathVariable 这个变量我们可以获取我们的url匹配中的值

例子如下

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
    Owner owner = ownerService.findOwner(ownerId);
    model.addAttribute("owner", owner);
    return "displayOwner";
}

在处理@PathVariable 过程中, Spring MVC 以 by name 方式从 URI 模板中匹配变量, @PathVariable 可以指定 name

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
    // implementation omitted
}

最后

以上就是慈祥戒指为你收集整理的码农小汪-spring框架学习之11-Spring MVC简介MVCThe request processing workflow in Spring Web MVC (high level) 处理的流程默认DispatcherServlet配置实现控制器Mapping Requests With @RequestMapping 映射请求URI Template Patterns的全部内容,希望文章能够帮你解决码农小汪-spring框架学习之11-Spring MVC简介MVCThe request processing workflow in Spring Web MVC (high level) 处理的流程默认DispatcherServlet配置实现控制器Mapping Requests With @RequestMapping 映射请求URI Template Patterns所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部