您的位置:首页 > 新闻 > 资讯 > SpringMVC后续4

SpringMVC后续4

2024/12/26 18:16:01 来源:https://blog.csdn.net/m0_68844946/article/details/142329099  浏览:    关键词:SpringMVC后续4

文件上传下载

导入maven

<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.2</version></dependency>

配置MultipartResolver

<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 设置上传文件的最大尺寸为5MB --><property name="maxUploadSize" ><value>5242880</value></property>
</bean>

创建表单

<form action="uploadFile" method="post" enctype="multipart/form-data">文件:<input type="file" name="file"><br><input type="submit" value="上传">
</form>

创建Controller

@RequestMapping("uploadFile")public String uploadFile(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request){//文件上传的位置   服务器路径String path = request.getSession().getServletContext().getRealPath("resources/upload");//System.out.println(path);if(!file.isEmpty()){try {FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path, file.getOriginalFilename()));} catch (IOException e) {e.printStackTrace();}}return "index";}

配置文件问题

静态资源问题

方式一:修改前端控制器 不推荐 web.xml

<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>*.do</url-pattern>
</servlet-mapping>

方式二:追加默认放行的资源 不推荐 (有多少写多少)

<servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.js</url-pattern>
</servlet-mapping>

方式三:修改SpringMVC-servlet.xml文件

追加mvc的命名空间

xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd//mvc驱动//mapping:映射路径//location:真实路径<mvc:annotation-driven/><mvc:resources mapping="/resources/**" location="/resources/"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:annotation-config/><context:component-scan base-package="com.hwq"></context:component-scan><mvc:annotation-driven/><mvc:resources mapping="/resources/**" location="/resources/"/><!--    视图解析器--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>

异常处理

局部异常

作用于当前控制器

@ExceptionHandler({Exception.class})
public String excpetion(Exception a){System.out.println("抛出异常");a.printStackTrace();return "redirect:error.jsp";//return "error";
}

全局异常

作用于整个项目

<!-- 异常解析器 --><bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><props><prop key="java.lang.Exception">redirect:error.jsp</prop></props></property></bean>

拦截器

第一步:创建类,继承拦截器适配器类或实现拦截器接口

第二步:重写方法

public class MyInterceptor implements HandlerInterceptor {/*登录拦截false表示拦截,不向下执行true表示放行*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("拦截器执行了");//判断是否合法    登录路径   session有数据//request.getRequestURI().equals("/login");//request.getSession().getAttribute("user");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("拦截器执行结束了");}
}

第三步:配置拦截器

<!--    拦截器配置--><mvc:interceptors><mvc:interceptor><mvc:mapping path="/*"/><bean id="myInterceptor" class="com.hwq.interceptor.MyInterceptor"></bean></mvc:interceptor></mvc:interceptors>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com