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.

你可能感兴趣的:(spring,spring,exception,handler)