SSM之springMVC(4/4)

上一节 SSM之springMVC(3/4)

目录

异常处理

全局异常处理器

在springmvc.xml配置全局异常处理器

异常测试

上传图片

RESTful支持

需求

Controller

REST的前端控制器

拦截器


异常处理

思路

系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。

       系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。

Springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。

全局异常处理器

思路:

       系统遇到异常,在程序中手动抛出,dao抛给service、service抛给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。

       全局异常处理器处理思路:

       解析出异常类型;

       如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示;

       如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)

 

Springmvc提供一个HandlerExceptionResolve接口。

public class CustomException extends Exception{
	//异常信息
	public String message;
	public CustomException(String message){
		super(message);
		this.message=message;
	}
	//get/set方法。。。
}
public class CustomExceptionResolver implements HandlerExceptionResolver {
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
			Object handler, Exception ex) {
		CustomException customException=null;
		if(ex instanceof CustomException){
			customException=(CustomException) ex;
		}else{
			customException=new CustomException("未知错误!");
		}
		
		//错误信息
		String message=customException.getMessage();
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("message", message);
		modelAndView.setViewName("error");
		return modelAndView;
	}
}

错误显示页面:

<body>

抛出异常信息......

${message}

</body>

在springmvc.xml配置全局异常处理器

<!-- 全局异常处理器

    只要实现CustomExceptionResolver接口就是全局异常处理器 -->

    <bean class="com.ssm.exception.CustomExceptionResolver"></bean>

异常测试

在controller、service、dao中任意一处需要手动抛出异常。

如果是程序手动抛出的异常,在错误页面中显示自定义的异常信息;如果不是手动抛出异常说明是一个运行时异常,在错误页面只显示“未知错误”。

 

在修改修改的controller方法中抛出异常:

public String editItems(Model model, @RequestParam(value = "id", required = true) Integer items_id)
			throws Exception {
		ItemsCustom itemsCustom = itemsService.selectByPrimaryKey(items_id);
		if(itemsCustom==null){
			throw new CustomException("修改的商品信息不存在!");
		}
		model.addAttribute("itemsList", itemsCustom); 
		return "itemsUpdate";
	}

如果与业务功能相关的异常,建议在service中抛出异常。

与业务关系没有关系的异常,建议在controller中抛出。

 

上边的功能,建议在service中抛出。

上传图片

https://blog.csdn.net/qq_40802448/article/details/88656836

RESTful支持

什么是RESTful:

RESTful架构,是一种互联网软件架构。结构清晰、符合标准、易于理解、扩展方便。

RESTful是一个开发理念,是对http的很好的诠释。

 

1、对url进行规范,写RESTful格式的url

非REST的url:http://……/queryItems.action?id=001&type=T01

REST的url风格:http://……/items/001

        特点:url简洁,将参数通过url传到服务器

2、http的方法规范

不管是删除、添加、更新…,使用url是一致的,如果进行删除,需要设置http的方法为delete,同理添加(post)…

后台controller方法:判断http方法,如果是delete执行删除,如果是post执行添加。

3、对http的contentType规范

请求时指定contentType,要json数据,设置成json格式的type。

需求

查询商品信息,返回json数据。

Controller

定义方法,进行url映射使用REST风格的url,将查询商品信息的id传入controller

输出json使用@ResponseBody将java对象输出json。

//查询商品信息,输出json
// /itemsView/{id}里边的{id}表示将这个位置的参数传到@PathVariable指定名称中
@RequestMapping("/itemsView/{id}")
public @ResponseBody ItemsCustom itemsView(@PathVariable("id") Integer id)throws Exception{
	//调用service查询商品信息
	ItemsCustom itemsCustom=itemsService.selectByPrimaryKey(id);
	return itemsCustom;
}

@RequestMapping(value="/itemsView/{id}"):{XXX}占位符,请求的URL可以是“/itemsView/1”或“/itemsView/2”,通过在方法中使用@PathVariable获取{XXX}中的XXX变量。

@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。

REST的前端控制器

web.xml
<!-- springmvc前端控制器:RESTful配置 -->
	<servlet>
		<servlet-name>springmvc_rest</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc_rest</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

测试:http://localhost:8080/springMVC_Pro/items/itemsView/1

拦截器

https://blog.csdn.net/qq_40802448/article/details/88657258

猜你喜欢

转载自blog.csdn.net/qq_40802448/article/details/88656629