Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。Spring 3.0更加完善,实现了对Struts 2的超越。现在越来越多的开发团队选择了Spring MVC。
Struts2也是非常优秀的MVC构架,优点非常多比如良好的结构,拦截器的思想,丰富的功能。但这里想说的是缺点,Struts2由于采用了值栈、OGNL表达式、struts2标签库等,会导致应用的性能下降,应避免使用这些功能。而Struts2的多层拦截器、多实例action性能都很好。
Spring的官方下载网址是:http://www.springsource.org/download
Eclipse 下载地址 http://yunpan.cn/Q7ws623WhxEZg 提取码 ca2c
小马哥说过,最好的学习方法就是活学活用,所以先配置一个小项目感受一下。
1)新建一个web project,这里我用的是Eclipse。
导入相关包
2)配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- web.xml 的加载顺序是:context-param - listener - filter - servlet ,
而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<!-- 使用ContextLoaderListener配置时,需要告诉它Spring配置文件的位置 -->
<!-- 如果没有指定,上下文载入器会在/WEB-INF/applicationContext.xml中找Spring配置文件 -->
<!-- 我们可以通过在Servlet上下文中设置contextConfigLocation参数,来为上下文载入器指定一个或多个Spring配置文件 -->
<!-- 注意:contextConfigLocation参数是一个用逗号分隔的路径列表,其路径是相对于Web系统的根路径的 -->
<!-- SpringMVC的前端控制器 -->
<!-- 当DispatcherServlet载入后,它将从一个XML文件中载入Spring的应用上下文,该XML文件的名字取决于<servlet-name> -->
<!-- 这里DispatcherServlet将试图从一个叫做springmvc-servlet.xml的文件中载入应用上下文,其默认位于WEB-INF目录下 -->
<!-- 所以ContextLoaderListener参数值也可写成<param-value>classpath:applicationContext-*.xml</param-value> -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 解决编码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<pre> <param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- encoding filter for jsp page -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 通过错误码来配置error-page ,配置了当系统发生404错误时,跳转到错误处理页面NotFound.-->
<error-page>
<error-code>404</error-code>
<location>/page404.html</location>
</error-page>
<!-- 通过异常的类型配置error-page ,配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp -->
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 启动扫描所有的controller -->
<context:component-scan base-package="com.test.action"/>
<!-- 主要作用于@Controller,激活该模式
下面是一种简写形式,完全可以手动配置替代这种简写形式;
它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,
是spring MVC为@Controllers分发请求所必须的
-->
<!-- <mvc:annotation-driven/> -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->
<mvc:resources location="/WEB-INF/resources/**" mapping="/resources"/>
<mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/>
<mvc:resources mapping="/styles/**" location="/static/css/"/>
<mvc:resources mapping="/images/**" location="/static/images/"/>
<!-- 配置页面访问地址www.xxx.com/about返回的静态html文件 -->
<mvc:resources mapping="/about/**" location="/WEB-INF/html/"/>
<!-- 走servlet的默认配置,先走默认的web.xml配置的servlet,没有的话才找对应controller -->
<mvc:default-servlet-handler />
<!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在jsp/目录下,查找XXX.jsp文件-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
</beans> 3)核心控制器 UserControllerpackage com.tgb.web.controller.annotation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@RequestMapping(value="/user/addUser",method=RequestMethod.POST)
public ModelAndView addUser(){
String result = "this is addUser-----";
return new ModelAndView("/jquery","result",result);
}
@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser(){
String result = "this is delUser";
return new ModelAndView("/jquery","result",result);
}
@RequestMapping(value="/user/toUser",method=RequestMethod.GET)
public ModelAndView toUser(){
return new ModelAndView("/jquery");
}
<ol class="dp-j" start="1"><li class="alt"><span><span class="comment">
//ModelAndView类在SpringMVC中是一个很重要的概念
//控制器执行方法都必须返回一个ModelAndView,ModelAndView对象保存了视图以及视图显示的模型数据
//第一个参数:视图组件的逻辑名称。这里视图的逻辑名称是userlist,视图解析器会使用该名称查找实际的View对象
//第二个参数:传递给视图的,模型对象的名称</span><span> </span></span></li><li class="alt">
//第三个参数:传递给视图的,模型对象的值
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- <script type="text/javascript">
$(document).ready(function(){
alert(11);
});
</script> -->
</head>
<body>
<h>jquery请求</h>
<form action="/springmvc01/user/addUser" method="post">
<input type="submit" value="post request" >
</form>
<br>
<div>
${result }
</div>
</body>
</html>四、配置tomcat 启动,并访问localhost
http://localhost:8080/springmvc01/user/addUser
原文地址:http://blog.csdn.net/cuiwjava/article/details/43924857