标签:char object exception location private except intercept serial cat
1.springmvc配置文件自动注解设置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- <bean id="mybeanamemapings" class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping">
定义映射处理器,寻找所有不需要的id <property name="order" value="1"></property> </bean> <bean
name="/aa.action" class="com.springmvc.test.MyConmvc"></bean> -->
<context:component-scan base-package="com.springmvc.test"></context:component-scan>
<!-- 加载格式转换组件 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 设置异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="selfErr"></property>
<property name="exceptionAttribute" value="ex"></property>
<property name="exceptionMappings">
<props>
<prop key="com.springmvc.test.NewException">selfErr</prop>
</props>
</property>
</bean>
<!-- 设置格式转换 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<bean class="com.springmvc.test.MyFormat"></bean>
</property>
<property name="formatters">
<set>
<bean class="com.springmvc.test.MyFormat01"></bean>
</set>
</property>
</bean>
<!-- 国际化转换 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="mm"></property>
<property name="useCodeAsDefaultMessage" value="true"></property>
</bean>
<!-- 设置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.springmvc.test.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 视图地址解析 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.java类的实现
2.1注解实现控制器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
public class Hellocontroller extends MultiActionController {
@RequestMapping("/mylogin.action")
public ModelAndView mylogin(HttpServletRequest req, HttpServletResponse res) throws Exception
{
// RequestContext rc =new RequestContext(req);
// rc.getMessage("hello");
System.out.println(111);
ModelAndView mav= new ModelAndView("mylogin");
return mav;
}
@RequestMapping("/mysuccess.action")
public String mysuccess(@ModelAttribute("user") UserInfo user,Model model) throws Exception {
if(!(user.getId().matches("[0-9]{1,4}"))) {
throw new Exception("输入的账号格式错误!!!");
}
if(user.getId().equals("9999")) {
throw new NewException("此账号非法!!");
}
// RequestContext rc =new RequestContext(req);
// rc.getMessage("hello");
System.out.println(222);
System.out.println(user.getId());
System.out.println(user.getPsd());
System.out.println();
model.addAttribute("userid", user.getId());
model.addAttribute("userpsd", user.getPsd());
System.out.println("getBrithday:"+user.getDate());
model.addAttribute("date", user.getDate());
return "mysuccess";
}
}
2.2自定义异常类的实现
public class NewException extends Exception {
/**
*
*/
private static final long serialVersionUID = 7856526306432864622L;
public NewException() {
}
public NewException(String mes) {
super(mes);
}
}
2.3拦截器类的实现
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("create afterCompletion=============");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("create postHandle=============");
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("create preHandle=============");
return true;
}
}
web.xml基本servlet配置
<servlet>
<servlet-name>mysmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mysmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
使用配置文件设置编码格式
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
工程jsp文件的
Springmvc异常、拦截器、格式转换、国际化处理配置文件以及java类
标签:char object exception location private except intercept serial cat
原文地址:http://www.cnblogs.com/w7589/p/7678261.html