SpringMVC的优势

1.角色划分:控制器(controller)、验证器(validator)、命令对象(command obect)、表单对象(form object)、模型对象(model object)、Servlet分发器(DispatcherServlet)、处理器映射(handler mapping)、试图解析器(view resoler)。每一个角色都可以由一个专门的对象来实现。

2.配置方式:可以使用XML配置,也可以使用代码来实现零配置(下面会说)

3.代码重用:可以使用现有的业务对象作为命令或表单对象,而不需要去扩展某个特定框架的基类。

4.restful风格:spring提供从最简单的URL映射,到复杂的、专用的定制策略。

5.灵活的model转换:使用基于Map的键/值对来达到轻易的与各种视图技术集成。

6.强大的标签库:使用在JSP编写更加容易。

7.性能比Struts2好

13
二、使用xml配置SpringMVC实例
1.创建web项目,拷贝所需要的包,也可以使用maven来构建

2.配置web.xml文件
1


Springmvc

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



org.springframework.web.context.ContextLoaderListener



contextConfigLocation
/WEB-INF/spring/applicationContext.xml



spring-mvc

org.springframework.web.servlet.DispatcherServlet


contextConfigLocation

/WEB-INF/spring/spring-mvc-serlvet.xml


1


spring-mvc
/

3、配置Spring参数
1

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">;




4.SpringMVC配置文件
1

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">;

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

/WEB-INF/jsp/


.jsp







1ntroller类
1
package com.xxxx.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/*
@description
@author create by xiaoxsen
@date 2017年8月14日---下午1:59:17br/>*/
@Controller
public class HelloController {

@RequestMapping("/")
public String index(Model model)
{
    System.out.println("hello");
     //转到helloworld.jsp
     return "hello";
}
@RequestMapping(value="/hello*")
public String hello(@RequestParam String hello ,Model model)
{
     System.out.println(hello);//获取url请求的参数
     model.addAttribute("greeting", "Hello Spring MVC");
     //转到helloworld.jsp
     return "helloworld";
}

}

5.hello.jsp
1
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
;



Insert title here







6.helloworld.jsp
1
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
;



Insert title here


${greeting}



12
7.输入localhost:8080/SpringMVCTest/查看结果
1
先显示一个输入框,输入后点击提交就会跳转页面显示下面的结果
Hello Spring MVC
1
2
8.注意的细节
1.配置文件中版本号最好与你的jar版本号对应,比如我的Spring的jar包都是4.3的
1
2
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">;
1
2
3
4
5
2.注意配置文件的开头要配置成这样:
1
1
不能这样配置:
1
1
会报异常:
1
Line 13 in XML document from ServletContext resource [/WEB-INF/spring/spring-mvc-serlvet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 70; cvc-elt.1: 找不到元素 'beans' 的声明。
1
二、SpringMVC零配置实现

HelloWorldInitializer实现WebApplicationInitializer替代web.xml。这个类在Servlet容器启动时就会加载

package com.xxxx.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/*
@description 代替web.xml
@author create by xiaoxsen
@date 2017年8月16日下午8:16:00
此类在servlet容器启动时加载
/
public class HelloWorldInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) throws ServletException {
    // TODO Auto-generated method stub
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(HelloWorldConfiguration.class);//把我们的配置到这里相当于我们把--servlet.xml配置到web.xml里
    ctx.setServletContext(container);
    //相当于 org.springframework.web.servlet.DispatcherServlet
    ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
    //1
    servlet.setLoadOnStartup(1);
    // spring-mvcurl-pattern>/
    servlet.addMapping("/");
}

}

也可以继承AbstractAnnotationConfigDispatcherServletInitializer然后实现
getRootConfigClasses() ;getServletConfigClasses() ;getServletMappings() 。其中getRootConfigClasses()相当于我们在web.xml中配置spring-mvc-servlet.xml。getServletMappings()方法就是相当于在web.xml文件中配置访问映射。

package com.xxxx.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/*
@description 代替web.xml
@author create by xiaoxsen
@date 2017年8月16日下午8:16:00
此类在servlet容器启动时加载
/
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{

@Override
protected Class[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class[]{HelloWorldConfiguration.class};
}

@Override
protected Class[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[]{"/"};
}

HelloWorldConfiguration替代spring-mvc-servlet.xml配置

package com.xxxx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/*
@description 配置类相当于spring-mvc-servlet.xml
@author create by xiaoxsen
@date 2017年8月16日下午8:05:57br/>*/
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.xxxx.controller")
public class HelloWorldConfiguration {

@Bean
public ViewResolver getViewResolver()
{
    //视图解析器
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setSuffix(".jsp");
    resolver.setPrefix("/WEB-INF/views/");
    return resolver;
}

}