Spring3.1 web exception handling example(Updated)

I didn't figure it out how to user spring2.5 based on the links:

http://www.tutorialspoint.com/spring/spring_exception_handling_example.htm

http://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/


Then I user @ExceptionHandler annotation to do that, it also very easy and convenient.

1. Write your own exception class

public class MyTwitterException extends Exception {

	private static final long serialVersionUID = -6436439497152731976L;
	private String exceptionMsg;


	public MyTwitterException(String exceptionMsg) {
		this.exceptionMsg = exceptionMsg;
	}


	public String getExceptionMsg() {
		return this.exceptionMsg;
	}


	public void setExceptionMsg(String exceptionMsg) {
		this.exceptionMsg = exceptionMsg;
	}

}


2. Config handler method( I do it in my controller class)

@RequestMapping("VIEW")
@Controller
public class TwitterPortlet {

	private static Logger logger = Logger.getLogger(TwitterPortlet.class);

	@Autowired
	@Qualifier("twitterTweetManager")
	private ITweetManager tweetManager;

	@RenderMapping
	public String doView(RenderRequest renderRequest, RenderResponse renderResponse)
			throws Exception {
		
		throw new MyTwitterException("PortalException");
		
	}

	/**
	 * Here is to handle All Exception.
	 * @param exception
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public String handleTwitterException(Exception exception) {
		return "error";
	}

}


3. Write your error.jsp page(Because I use Spring to handle view Resolving
<h2>No Tweets available now!</h2>
you also can pass message into the page to display.

Even though we don't create MyTwitterException, it still works, because we handle all exception.

-----------------------------------------------------------------------------------------------------------------------------------------------

Last week, I learned another way to handle exception

First, we need to create a class implements HandlerExceptionResolver, Ordered

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.springframework.core.Ordered;
import org.springframework.web.portlet.HandlerExceptionResolver;
import org.springframework.web.portlet.ModelAndView;

public class MyExceptionResolver implements HandlerExceptionResolver, Ordered {

	private int order;
	
	
	public void setOrder(int order) {
		this.order = order;
	}

	@Override
	public int getOrder() {
		// TODO Auto-generated method stub
		return order;
	}

	@Override
	public ModelAndView resolveException(RenderRequest arg0, RenderResponse arg1, Object arg2,
			Exception arg3) {
		// TODO Auto-generated method stub
		return new ModelAndView("error");
	}

	@Override
	public ModelAndView resolveException(ResourceRequest arg0, ResourceResponse arg1, Object arg2,
			Exception arg3) {
		// TODO Auto-generated method stub
		return new ModelAndView("error");
	}

}

Then config in your applicationContext.xml

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

<bean class="com.rujuan.exception.MyExceptionResolver">
	<property name="order" value="0" />
</bean>

You error.jsp is put inside/web-inf/jsp folder. Because I use spring viewresolver.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值