<!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- scan the package and the sub package -->
<context:component-scan base-package="test.SpringMVC"/> <!-- don't handle the static resource -->
<mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven /> <!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>

文件上传

  1.需要导入两个jar包

2.在SpringMVC配置文件中加入

<!-- upload settings -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000"></property>
</bean>

3.方法代码

@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest req) throws Exception{
MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
MultipartFile file = mreq.getFile("file");
String fileName = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
"upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
fos.write(file.getBytes());
fos.flush();
fos.close(); return "hello";
}

4.前台form表单

<form action="mvc/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>

设置一个自定义拦截器

1.创建一个MyInterceptor类,并实现HandlerInterceptor接口

public class MyInterceptor implements HandlerInterceptor {

    @Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("afterCompletion");
} @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("postHandle");
} @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("preHandle");
return true;
} }

2.在SpringMVC的配置文件中配置

<!-- interceptor setting -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/mvc/**"/>
<bean class="test.SpringMVC.Interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

3.拦截器执行顺序

整合SpringIOC和SpringMVC

1.创建一个test.SpringMVC.integrate的包用来演示整合,并创建各类

2.User实体类

public class User {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
}
private int id;
@NotEmpty
private String name; @Past
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;
}

3.UserService类

@Component
public class UserService {
public UserService(){
System.out.println("UserService Constructor...\n\n\n\n\n\n");
} public void save(){
System.out.println("save");
}
}

4.UserController

@Controller
@RequestMapping("/integrate")
public class UserController {
@Autowired
private UserService userService; @RequestMapping("/user")
public String saveUser(@RequestBody @ModelAttribute User u){
System.out.println(u);
userService.save();
return "hello";
}
}

5.Spring配置文件

  在src目录下创建SpringIOC的配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
>
<context:component-scan base-package="test.SpringMVC.integrate">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
</beans>

在Web.xml中添加配置

<!-- configure the springIOC -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

6.在SpringMVC中进行一些配置,防止SpringMVC和SpringIOC对同一个对象的管理重合

<!-- scan the package and the sub package -->
<context:component-scan base-package="test.SpringMVC.integrate">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

SpringMVC详细运行流程图

SpringMVC与struts2的区别

1、springmvc基于方法开发的,struts2基于类开发的。springmvc将url和controller里的方法映射。映射成功后springmvc生成一个Handler对象,对象中只包括了一个method。方法执行结束,形参数据销毁。springmvc的controller开发类似web service开发。

  2、springmvc可以进行单例开发,并且建议使用单例开发,struts2通过类的成员变量接收参数,无法使用单例,只能使用多例。

  3、经过实际测试,struts2速度慢,在于使用struts标签,如果使用struts建议使用jstl。

SpringMVC--xxx.xml配置的更多相关文章

  1. springmvc web.xml配置之 -- SpringMVC IOC容器初始化

    SpringMVC IOC容器初始化 首先强调一下SpringMVC IOC容器初始化有些特别,在SpringMVC中除了生成一个全局的spring Ioc容器外,还会为DispatcherServl ...

  2. SpringMvc的xml配置与annotation配置的例子的区别

    1.导入jar包时,要在xml配置基础上加 spring-aop-4.2.2.RELEASE.jar (注解的时候需要) 2.编写controller的时候要annotation需要做相关配置即红色部 ...

  3. 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置

    首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...

  4. spring 5.x 系列第1篇 —— springmvc基础 (xml配置方式)

    文章目录 一.搭建hello spring工程 1.1 项目搭建 1.2 相关配置讲解 二.配置自定义拦截器 三.全局异常处理 四.参数绑定 4.1 参数绑定 4.2 关于日期格式转换的三种方法 五. ...

  5. SpringMVC 中xml 配置多数据源

    1,配置jdbc.properties jdbc.driver_one=... jdbc.url_one=..... jdbc.username_one=... jdbc.password_one=. ...

  6. SpringMvc web.xml配置详情

    出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...

  7. springmvc web.xml配置之 -- DispatcherServlet

    springMVC servlet配置与启动 看一下springmvc的web.xml常见配置: <servlet> <!-- 配置DispatcherServlet --> ...

  8. springmvc web.xml配置之 -- ContextLoaderListener

    首先回归一下web.xml的常用配置,看一个示例: <context-param> <param-name>contextConfigLocation</param-na ...

  9. SpringMVC spring-servlet.xml配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  10. Java web.xml 配置技巧—动态欢迎页地址

    我们的 Java   Web  项目在配置web.xml 欢迎页地址默认是index.html .index.jsp ,不知道有人注意过没有,如果我要配置成/index/user.action  或者 ...

随机推荐

  1. Json 入门例子(form表单)【0】

    <script> $(function () { var aa = $("#fm").serialize().replace(/\+/g, ""); ...

  2. 【Java】ServerSocket的学习笔记

    公司有本<Java网络编程>一直闲置在书架上,反正我对Socket方面不太懂,今天跟着书学习一番. > 参考的优秀书籍 <Java网络编程> --中国电力出版社 > ...

  3. QT笔记之模态对话框及非模态对话框

    模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...

  4. activiti学习总结

    Activiti界面元素的使用总结 一.图形设计中元素的使用 1.SequenceFlow:连接线,可以连接两个任务,来管理流程实例的流向 -----General -----id:流程的id,用与程 ...

  5. NTT

    1 问题描述FFT问题解决的是复数域上的卷积.如果现在的问题是这样:给出两个整数数列$Ai,Bj,0\leq i\leq n-1,0\leq j\leq m-1$,以及素数$P$,计算新数列$Ci=( ...

  6. Ubuntu 14.04中文输入法的安装

    Ubuntu默认自带的中文输入法是IBUS框架的ibus-pinyin,IBUS-Bopomofo等.对于习惯于搜狗,紫光华宇,谷歌拼音的我们可能有点使用不习惯.下面就是安装常用的IBUS中文输入法. ...

  7. 使用Spring容器

    Spring的两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口. Spring容器就是一个大的Bea ...

  8. freebsd镜像作用和vmware服务开启

    第一个是可以引导的光盘,只能引导系统,通常用于网络安装.基本没用.第二个是系统光盘的第一张.用这张就可以安装一个基本的系统.其他的软件,在系统安装完之后安装.第三个是系统盘的DVD版本.包括的软件比上 ...

  9. SQL生成包含年月日的流水号

    --************************************************************************************************** ...

  10. HTML笔记(五)表单<form>及其相关元素

    表单标签<form> 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中输入信息的元素. 输入标签<input> 输入标签的输入类型由其类型属性type决定.常见的输入 ...