标签:操作 work base size query type contex val print
1. SpringMVC自定义异常处理
2. SpringMVC的interceptor(过滤器)
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--<load-on-startup>1</load-on-startup>-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context:component-scan base-package="com.rqy"/>
<mvc:annotation-driven/>
<!--配置springmvc视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
@Controller
public class HelloController {
@RequestMapping("hello")
public String hello(int type) throws Exception {
if (type==1){
throw new MyException("你的用户名过长");//自定义异常
}else if (type==2){
throw new FileSizeException();
}else if (type>2){
throw new UnknownParamException();
}
return "hello" ;
}
}
public class MyException extends Exception {
public MyException(String message){
super(message);
}
}
* 避免直接在页面显示异常信息,我们自定义异常处理器,抛出对应的异常时将转发到不同的页面上。
* 案例:
* @Component
public class CustomExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
System.out.println(o);
if (e instanceof MyException) {
MyException myException = (MyException) e;
modelAndView.addObject("content", myException.getMessage());
modelAndView.setViewName("customException");
} else if (e instanceof FileSizeException) {
modelAndView.setViewName("fileException");
} else if (e instanceof UnknownParamException) {
modelAndView.setViewName("paramException");
}
return modelAndView;
}
}
* 其中customException,fileException,paramException对应的jsp页面
* 例如:页面打印出: 自定义的异常 ${content},即可。
* public class MySecondInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle2");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle2");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion2");
}
}
* <mvc:interceptors>
<!--全部请求都通过interceptor-->
<!--/**-->
<bean class="com.rqy.interceptor.MyFirstInterceptor"/>
<bean class="com.rqy.interceptor.MySecondInterceptor"/>
<!--<ref bean="interceptor3"/>-->
<!--interc拦截部分请求-->
<!--user/query
user/add
user/register-->
<mvc:interceptor>
<!--localhost/user/a yes-->
<mvc:mapping path="/user/**"/>
<bean class="com.rqy.interceptor.MyThirdInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
* public class MyFirstInterceptor implements HandlerInterceptor {
//当返回值为false时不能够进入RequestMapping对应的方法
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle1");
String username = request.getParameter("username");
if (username==null|| "".equals(username)){
return true;//如果没有参数提交,继续验证下面方法postHandle,看看用户是否登录了
}else {
request.getSession().setAttribute("username",username);
return true; //username不为空,意味用户登录操作,不进行拦截
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String username = (String) request.getSession().getAttribute("username");
if (username==null|| "".equals(username)){
modelAndView.setViewName("login");//用户未登录返回login界面
}else {
modelAndView.addObject("username",username);
modelAndView.setViewName("hello");
}
System.out.println("postHandle1");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion1");
}
}
标签:操作 work base size query type contex val print
原文地址:https://www.cnblogs.com/rqy0526/p/11278111.html