SpringMVC静态资源和异常

Spring核心应用(1)



Spring MVC重定义和转发

重定向 —“redirect:”

return "redirect:/user/login.html";

转发 —“forward:”

  return "forward:/user/login.html";

逻辑视图

  return "user/login.html";

静态资源文件的引用

  1. <mvc:resources/>标签
    mapping:将静态资源映射到指定的路径下
    location:本地静态资源文件所在的目录
<mvc:resources mapping="/**" location="/"/>

DispatcherServlet不会拦截mapping配置的请求地址,并会到location配置中查找访问的文件
2. 采用<mvc:default-servlet-handler />

异常处理

1. 局部异常处理

仅能处理指定Controller中的异常
@ExceptionHandler

    @RequestMapping(value = "exlogin.html")
    public String exLogin(@RequestParam String userCode, @RequestParam String userPassword) {
        logger.info("exlogin++++++++++");
        User login = userService.login(userCode, userPassword);
        if (null == login) {
            throw new RuntimeException("用户名或密码不正确");
        }
        return "redirect:/user/main.html";

    }
   @ExceptionHandler(value = {RuntimeException.class})
    public String userError(RuntimeException e, HttpServletRequest request) {
        request.setAttribute("error", e);
        return "login";
    }

2. 全局异常处理

对所有异常进行统一处理(须取消局部异常处理)
配置SimpleMappingExceptionResolver

<bean class="org.springframework.web.servlet.handler.
                          SimpleMappingExceptionResolver">
<property name="exceptionMappings">
	<props>
		<prop key="java.lang.RuntimeException">error</prop>
	</props>
</property>
</bean>

3. 自定义异常和通用异常

第一步:定义异常

public class SException extends Exception {
    private String message;

    public SException(String message) {
        super(message);
        this.message = message;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

第二步:实现HandlerExceptionResolver接口,并定义处理方式

@Component
public class SmbmsExceptionResolver implements HandlerExceptionResolver {
    Logger logger = Logger.getLogger(SmbmsExceptionResolver.class);
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        //可以处理我们的全局错误
        logger.error("进入到全局异常");
        if (e instanceof SmbmsException){
            //用我们自己的方式处理它
            logger.error("\t进入自定义异常处理模块");
            ModelAndView modelAndView = new ModelAndView("error");
            modelAndView.addObject("error",e.getMessage());
            return modelAndView;
        }else {
            //用我们自己的方式处理它
            logger.error("\t通用异常");
            ModelAndView modelAndView = new ModelAndView("login");
            modelAndView.addObject("error",e.getMessage());
            return modelAndView;
        }
    }
}

第三步:控制层调用

@Controller
public class ExceptionController {
    @RequestMapping(value = "exception")
    public void exception() throws SmbmsException {
        throw new SmbmsException("抛出自定义异常");
    }
    @RequestMapping(value = "c/exception")
    public void cException() throws Exception {
        throw new Exception("抛出通用异常");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值