异常处理
@RequestMapping("error")
public String testerror() {
int i = 1/0;
return "welcome";
}
//处理该类中出现的ArithmeticException,AclNotFoundException
//方法的参数中只能由Exception,如需将数据转发,可以使用ModelAndView
@ExceptionHandler({ArithmeticException.class,AclNotFoundException.class})
public String exceptionHandler(Exception e) {
e.printStackTrace();
return "welcome";
}
使用@ExceptionHandler注解表示的方法的参数必须是异常类型,不能包含其他类型的参数
当类中有多个异常捕获方法时,根据最短优先执行方法
自定义异常类@ControllerAdvice,可以处理任何类的异常
在类前加@ControllerAdvice,在方法前加@ExceptionHandler({xxxException.class})
@ControllerAdvice
public class MyException {
@ExceptionHandler({Exception.class})
public String exceptionHandler(Exception e) {
e.printStackTrace();
return “welcome”;
}
}
ResponseStatusExceptionResolver: 自定义异常显示页面 @ResponseStatus
1.创建自定义异常类加@ResponseStatus注解
@ResponseStatus(value=HttpStatus.NOT_FOUND,reason="页面不存在")
public class MyResponseStatus extends Exception{//自定义异常
}
2.需要是抛出自定义异常类
@RequestMapping("page")
public void testPage() throws MyResponseStatus {
throw new MyResponseStatus();//抛出自定义异常
}
在方法中实现自定义异常页面,将@ResponseStatus()写在方法前面
@RequestMapping("methodpage")
public String testmethodPage(){
return "redirect:topage";
}
@ResponseStatus(value = HttpStatus.NOT_FOUND,reason="测试")
@RequestMapping("topage")
public void topage() throws MyResponseStatus {
}
整合SSM
1.Spring-整合MyBatis,将MyBatis的SqlSessionFactory 交给Spring
2.Spring-整合SpringMVC,就是将Spring-SpringMVC各自配置一边
SM整合步骤:
1.jar
mybatis-spring.jar spring-tx.jar spring-jdbc.jar spring-expression.jar
spring-context-support.jar spring-core.jar spring-context.jar
spring-beans.jar spring-aop.jar spring-web.jar commons-logging.jar
commons-dbcp.jar ojdbc.jar mybatis.jar log4j.jar commons-pools.jar
2.表-类
person类–person表
3.(spring整合时,可省)MyBatis配置文件conf.xml(数据源-mapper.xml)–可省,将该文件中的配置 全部交由spring管理
spring配置文件 applicationContext.xml(springBeanConfiguration file)
4.通过mapper.xml将 类,表建立映射关系
5.配置spring配置文件(applicationContext.xml)
<!-- web项目中引入spring -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
6.使用Spring整合MyBatis
<!-- 数据源 -->
<bean id = "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/person"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- MyBatis核心类 sqlSessionFactory-->
<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value ="classpath:mapper/*.xml"></property>
</bean>
<!-- Spring整合MyBatis:将MyBatis的SqlSessionFactory 交给Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 使mapper包中的所有对象,产生与之对应的动态代理对象(对象名就是首字母小写的接口名) -->
<property name="basePackage" value="mapper"></property>
</bean>
<!-- 依赖注入:给service注入Dao -->
<bean id="personService" class="service.PersonServiceImpl" >
<property name="mapper" ref="personMapper"></property>
</bean>
扫描包
继续整合SpringMVC:将springMVC加入项目即可
1.jar
Spring-webmvc.jar
2.配置springMVC
<!-- 项目整合SpringMVC -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.编写spring配置文件
<!-- 扫描有注解的包 -->
<context:component-scan base-package="handler"></context:component-scan>
<!-- 配置视图解析器 为 controller 中返回中的字段 添加前缀 和 后缀-->
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value = "/view/"></property>
<property name="suffix" value= ".jsp"></property>
</bean>
<!-- 此配置时SpringMVC的基础配置,很多功能都需要通过该注解来协调 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 使用默认Servlet去访问静态资源 -->
<mvc:default-servlet-handler/>