Springmvc異常、攔截器、格式轉換、國際化處理設定檔以及java類

來源:互聯網
上載者:User

標籤: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類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.