Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

时间:2023-03-08 16:31:12
Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

拦截器

Spring MVC可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口,重写以下3个方法。

    @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
// TODO Auto-generated method stub
String a = request.getParameter("a");
if(a.equals("10")){
//通过
return true;
}else{
//不通过
response.sendRedirect("../inter2.jsp");
}
return false;
} /**
* 在渲染View之后被调用,可以用来释放资源
*/
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub } /**
* 在调用目标方法之后,在渲染界面之前被启用
* 可以对模型及数据进行调整
*/
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub }

Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制


文件上传

导包

commons-fileupload-1.2.jar
import java.io.File;
import java.io.IOException; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile; @Controller
@RequestMapping("/upload")
public class UploadController { @RequestMapping("/l1")
public String upload(MultipartFile file,HttpSession session) throws IllegalStateException, IOException{
//验证文件是否为空
if(!file.isEmpty()) {
//文件命名:当前时间毫秒数+原图的名字
String fileName=System.currentTimeMillis()+file.getOriginalFilename();
//服务器的上传路径
String location=session.getServletContext().getRealPath("upload");
//上传文件
file.transferTo(new File(location+"/"+fileName));
}
return "success";
} }

-中文乱码解决

在web.xml中进行配置。

  <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>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

rest风格:

一种互联网软件架构。

HTTP协议的四种基本操作:

  • GET获取资源
  • POST新建资源
  • PUT更新资源
  • DELETE删除资源

Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

rest风格的URL配置:

Spring MVC—拦截器,文件上传,中文乱码处理,Rest风格,异常处理机制

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/rest")
public class RestController { @RequestMapping("/r1")
public void r1(HttpServletRequest request,PrintWriter pw){
System.out.println(request.getMethod());
switch (request.getMethod()) {
//获取数据
case "GET":
//...
System.out.println("获取数据");
break;
//添加数据
case "POST":
//...
System.out.println("添加数据");
break;
//更新数据
case "PUT":
//...
System.out.println("更新数据");
break;
//删除数据
case "DELETE":
//...
System.out.println("删除数据");
break;
} } }

在web.xml中进行rest风格组件相关配置

 <!-- rest风格支持组件(支持表单向提交PUT/DELETE方法) -->
<filter>
<filter-name>hidden</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hidden</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

异常处理机制

异常处理机制:将遇到的错误进行封装。

在servlet.xml中配置错误处理解析器。

    <!-- 错误处理解析器 -->
<!-- <bean
id="exceptionResolver"
class="com.jredu.exception.MyExceptionHandler"/> -->
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.jredu.entity.User; @Controller
@RequestMapping("/error")
public class ErrorController { @RequestMapping("/e1")
public String e1(){
User user=null;
System.out.println(user.getPwd());
return "hello";
} }
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView; /**
* 异常处理捕获监听器
* @author Administrator
*
*/
public class MyExceptionHandler implements HandlerExceptionResolver{ @Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
// TODO Auto-generated method stub
ModelAndView mav=new ModelAndView("exception");
return mav;
} }