SpringMVC入门

1、Spring  MVC 背景介绍

Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer PagesJSP)技术、VelocityTilesiText 和 POISpring MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

 

2、常见MVC框架比较 

    运行性能上:

        Jsp+servlet>struts1>spring mvc>struts2+freemarker>>struts2,ognl,值栈。

    开发效率上,基本正好相反。值得强调的是,spring mvc开发效率和struts2不相上下。

 

Struts2的性能低的原因是因为OGNL和值栈造成的。所以,如果你的系统并发量高,可以使用freemaker进行显示,而不是采用OGNL和值栈。这样,在性能上会有相当大得提高。

3、SpringMVC 核心原理

1. 用户发送请求给服务器。urluser.do

2. 服务器收到请求。发现DispatchServlet可以处理。于是调用DispatchServlet

3. DispatchServlet内部,通过HandleMapping检查这个url有没有对应的Controller。如果有,则调用Controller

4. Controller开始执行。

5. Controller执行完毕后,如果返回字符串,则ViewResolver将字符串转化成相应的视图对象;如果返回ModelAndView对象,该对象本身就包含了视图对象信息。

6. DispatchServlet将执视图对象中的数据,输出给服务器。

7. 服务器将数据输出给客户端。

4、基于spring 3.0项目开发实例

    1、导入相关jar包

    2、配置web.xml  

01<?xml version="1.0" encoding="UTF-8"?>
02<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
03  <display-name>SpringMVCDemo</display-name>
04 
05 
06    <servlet>
07        <servlet-name>web servlet</servlet-name>
08        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class
09        <init-param>
10            <param-name>contextConfigLocation</param-name>
11            <param-value>WEB-INF/spring-mvc.xml</param-value>
12        </init-param>
13        <load-on-startup>1</load-on-startup>
14    </servlet>
15     
16    <servlet-mapping>
17        <servlet-name>web servlet</servlet-name>
18        <url-pattern>/</url-pattern>   
19    </servlet-mapping>
20 
21</web-app>

  3、配置spring-mvc.xml

01<?xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
04    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
05    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
06    xsi:schemaLocation="http://www.springframework.org/schema/beans  
07            http://www.springframework.org/schema/beans/spring-beans.xsd  
08            http://www.springframework.org/schema/context   
09            http://www.springframework.org/schema/context/spring-context.xsd  
10            http://www.springframework.org/schema/aop   
11           http://www.springframework.org/schema/aop/spring-aop.xsd  
12            http://www.springframework.org/schema/tx   
13            http://www.springframework.org/schema/tx/spring-tx.xsd  
14            http://www.springframework.org/schema/mvc   
15            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
16            http://www.springframework.org/schema/context   
17            http://www.springframework.org/schema/context/spring-context.xsd">
18 
19    <!--告知Spring,我们启用注解驱动-->
20    <mvc:annotation-driven />
21 
22    <!--配置要扫描注解的类的路径-->
23    <context:component-scan base-package="com.excel.controller"></context:component-scan>
24 
25    <!--配置获取VIEW的方式 -->
26    <bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
27        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
28</beans>

4、编写控制器

01package com.excel.controller;
02 
03import org.springframework.stereotype.Controller;
04import org.springframework.ui.ModelMap;
05import org.springframework.web.bind.annotation.ModelAttribute;
06import org.springframework.web.bind.annotation.RequestMapping;
07import org.springframework.web.bind.annotation.RequestParam;
08import org.springframework.web.bind.annotation.SessionAttributes;
09 
10@Controller
11@RequestMapping("user")
12public class UserController {
13 
14     
15    //RequestParam  当传递参数名与接收名称不一致时指定
16    @RequestMapping("/login.do")
17    public String login(String userName ){
18        System.out.println("User Login...."+userName);
19        return "index";
20    }
21}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值