<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="test?i=0">test</a>
<br><br>
<form action="upFile" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="*.ppt application/vnd.ms-powerpointMS PowerPoint Presentation"/>
<input type="submit"/>
</form>
<a href="login">To login</a>
</body>
</html>
一、处理异常 一个@RequestMapping(“/test”)错误方法
@RequestMapping("/test")
public void TestEx(@RequestParam("i") int i){
System.out.println(10/i);
}
一个用@ContrllerAdvice注解的类
@ControllerAdvice
public class ExceptionHandlers {
//SpringMVC 对异常的处理 执行的时候 找对应的异常 没有对应的异常 找最近的异常
@ExceptionHandler(value={RuntimeException.class})
public String testError(){
System.out.println("出错哦!!!");
return "error";
}
@ExceptionHandler(value={ArithmeticException.class})
public ModelAndView testError(Exception exception){
System.out.println("ModelAndView...出错了哦");
return new ModelAndView("error","exception",exception);
}
二、文件的上传 两个jar包
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
(1、类)
@Controller
public class FieUpLoad
//文件的上传
{ @RequestMapping("/upFile")
public void testUpFile(MultipartFile file,HttpSession session) throws Exception, IOException{
System.out.println(file.getOriginalFilename());//web目录下的
或 //“d:/test/”
String path=session.getServletContext().getRealPath("/test/")+new Date().getTime()+file.getOriginalFilename()+Math.random();
File newFile=new File(path);
file.transferTo(newFile);
}
(2.)jsp页面
(3)Springmvc.xml配置文件
<!-- 文件的上传 配置 -->
application.xml
<context:component-scan base-package="com.Greatest.Springmvc.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<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"></property>
<property name="maxUploadSize" value="50000000"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 可配置多个拦截器 -->
<mvc:interceptors>
<!--mvc:interceptor:注册一个拦截器,拦截发送到一个或多个URI路径的请求 -->
<mvc:interceptor>
<mvc:mapping path=""/>
<bean class="com.Greatest.Springmvc.controller.Test" > </bean>
</mvc:interceptor>
</mvc:interceptors>
三、用两个控制器一个管理Spring一个管理Springmvc
(1)web.xml文件配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Springmvc2.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置两个控制器 一个管理Spring 一个管理Springmvc -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
(2)两个 Spring.xml
classpath:Springmvc2.xml:
<context:component-scan base-package="com.Greatest.Springmvc.Service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
四、Springmvc的拦截器
(1.)类
/* 拦截器主要作用是拦截用户的请求并进行相应的处理,其他的作用
* 比如通过它来进行权限验证,或者是来判断用户是否登陆,日志记录,
* 或者限制时间点访问。Interceptor
* 拦截请求是通过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("afterCompletion");
}
/*在目标方法执行前执行
*验证 日志 事物
* */
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("afterCompletion");
return true;//默认 false
}
(2)在xml文件中配置