<!--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. pt-query-digest怎么分析慢查询日志分析数据

    在进行使用linux系统作为服务器的情况,那几需要进行对linux的服务器进行性能上数据进行抓取之后,就需要对数据中内容进行分析,看数据库中内容是否存在瓶颈上的问题,在进行获取到的数据的慢查日志,将使 ...

  2. SELECT INTO FROM mysql Undeclared variable

    使用SELECT INTO FROM进行数据表和数据的复制时,会提示Error Code: 1327. Undeclared variable: 这是因为mysql 数据库不支持 SELECT INT ...

  3. js计时器的问题

    不说话直接上代码了 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...

  4. SpringMVC 模拟登陆

    新建BackgroundController类: package cn.bdqn.mvc.controller; import org.springframework.stereotype.Contr ...

  5. [Java解惑]类

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. Codeforces Round #250 (Div. 2)A(英语学习)

    链接:http://codeforces.com/contest/437/problem/A A. The Child and Homework time limit per test 1 secon ...

  7. GBASE结构理解

    GBASE数据库 8a 8a Cluster 8t 8m BI 8d 8 分析型数据库 分布式并行数据库集群 高端事务性数据库 高速内存数据库 可视商业智能 大型目录服务体系 硬加密安全数据库 数据分 ...

  8. 转 图片资源加密,Lua文件加密

    游戏开发中常遇到资源保护的问题. 目前游戏开发中常加密的文件类型有:图片,Lua文件,音频等文件,而其实加密也是一把双刃剑. 需要安全那就得耗费一定的资源去实现它.目前网上也有用TexturePack ...

  9. mysql概要(七)表字段管理,字段的增删改

    1.添加列 放在某列之后 放第一列: 2.修改(声明和名字),删除列 2.1修改声明 2.2删除列

  10. Jquery中css()方法获取边框长度

    1. JQuery中可以使用css()方法获取块元素的边框宽度,如下: $("divMode").css("border-left-width");//左边框长 ...