您的位置:首页 > 编程语言 > Java开发

码农小汪-spring框架学习之11-Spring MVC简介

2016-04-16 22:46 323 查看

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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: