概述

   Spring框架为WEB项目提供了国际化以及异常处理机制。所谓的国际化也就是不同国籍,显示不同国籍的语言与符号。异常处理,也就是能够捕获WEB项目下的所有异常信息,并能处理记录这些异常信息机制。

国际化

  Spring对国际化的语言采用配置的方式存储到配置文件中,在springservletconfig.xml文件,添加下面语句:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的文件名 -->
<property name="basename" value="messages" />
<!-- 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>

  Spring MVC国际化的方式,可以基于Session,也可以基于Cookie,这里主要基于Session完成国际化,在springservletconfig.xml添加下面拦截器配置

  <mvc:interceptors>
<!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- 基于session国际化-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

    <!-- 基于Cookie国际化
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />-->

  在当前目录下添加如下文件,messages.properties、messages_zh_CN.properties、messages_en_US.properties三个文件,其中messages.properties、messages_zh_CN.properties;

messages_en_US.properties:

umoney=money
udate=date

messages_zh_CN.properties:

umoney=余额
udate=日期

Controller代码如下:

    @RequestMapping("/cuser")
public String ShowUser(HttpServletRequest request, HttpServletResponse response,Model model,
              @RequestParam(value="langType", defaultValue="zh") String langType)
{
UserInfo Usermodel=new UserInfo();
SessionLang(request, langType);
//CookieLang(request,response,langType);
RequestContext Rq=new RequestContext(request);
model.addAttribute("udate",Rq.getMessage("udate"));
model.addAttribute("umoney",Rq.getMessage("umoney"));
return "Fuser/cuser";
} /**
* @Title: SessionLang
* @Description: 基于session的国际化
* @param @param request
* @param @param langType 设定文件
* @return void 返回类型
* @throws
*/
public void SessionLang(HttpServletRequest request,String langType)
{
if(langType.equals("zh")){
Locale locale = new Locale("zh", "CN");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else if(langType.equals("en")){
Locale locale = new Locale("en", "US");
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
}
else
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
} /**
* @Title: CookieLang
* @Description: 基于Cookie的国际化
* @param @param request
* @param @param response
* @param @param langType 设定文件
* @return void 返回类型
* @throws
*/
public void CookieLang(HttpServletRequest request,HttpServletResponse response,String langType)
{
Locale locale =null;
if(langType.equals("zh")){
locale = new Locale("zh", "CN"); }
else if(langType.equals("en")){
locale = new Locale("en", "US");
}
else {
locale=LocaleContextHolder.getLocale();
}
(new CookieLocaleResolver()).setLocale (request, response, locale);
}

前台JSP页面

<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="cuser?langType=zh">中文</a> | <a href="cuser?langType=en">英文</a><br/> 下面展示的是后台获取的国际化信息:<br/>
${umoney}<br/>
${udate}<br/>
</body>
</html>

异常处理

  MVC Spring异常处理的方式有2种,一种是写个Class继承HandlerExceptionResolver,在配置文件中,添加Bean配置,另外一种方式,可以通过定义基类控制器,编写异常处理的公用方法,子类继承基类Controller即可。代码配置如下

编写基类Controller:

package justin.com.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ExceptionHandler;

public abstract  class BaseController {

    @ExceptionHandler
public String exception(HttpServletRequest request,Exception ex)
{
request.setAttribute("exceptionMessage", ex.getMessage()); return "error";
}
}

子类继承BaseController

package justin.com.controllers;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/helloworld")
public class HelloWorldController extends BaseController { @RequestMapping(value={"/*","/say"},method=RequestMethod.GET)
public ModelAndView China() throws SQLException
{ ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "Hello World!");
modelAndView.setViewName("HelloWorld/CIndex");
int Result=/;
return modelAndView;
}
}

当打开该页面,页面自动跳转到错误Controller中,完成数据的异常捕获与显示。

  

Spring MVC基础知识整理➣国际化和异常处理的更多相关文章

  1. Spring MVC基础知识整理➣拦截器和自定义注解

    概述 Spring MVC中通过注解来对方法或者类进行动态的说明或者标注,类似于配置标识文件的属性信息.当标注的类或者方式被使用时候,通过提取注解信息来达到对类的动态处理.在 MVC中,我们常用的注解 ...

  2. Spring MVC基础知识整理➣环境搭建和Hello World

    概述 Spring MVC属于SpringFrameWork的产品,采用Model-View-Controller进行数据交互,已经融合在Spring Web Flow里面.Spring 框架提供了构 ...

  3. Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库

    概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...

  4. Spring MVC基础知识整理➣数据校验与格式化

    概述 将view中Form的数据提交到后台之后,后台如何验证数据的有效性?在这里Spring MVC提供了相应的Hibernate类包(hibernate-validator-4.3.1.Final. ...

  5. Spring MVC基础知识整理➣View与Controller数据交互

    概述 Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点.MVC中,我们将View中的数据传递到Controlle ...

  6. Spring框架系列(一)--Spring MVC基础知识

    Web项目开发过程中一般都是使用MVC(Model-View-Controller)模式,早先的Struts2到Spring MVC,再到现在Spring Boot,都是相似的思 路.Spring B ...

  7. spring mvc 基础知识

    spring mvc 在web.xml中的配置: 例子: <?xml version="1.0" encoding="UTF-8"?> <we ...

  8. 【OGG】OGG基础知识整理

    [OGG]OGG基础知识整理 一.GoldenGate介绍 GoldenGate软件是一种基于日志的结构化数据复制软件.GoldenGate 能够实现大量交易数据的实时捕捉.变换和投递,实现源数据库与 ...

  9. Kali Linux渗透基础知识整理(四):维持访问

    Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...

随机推荐

  1. boost::function 介绍

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  2. C语言 16进制转float

    float hex_to_float(uint8_t *data) { float num = 0.0; uint8_t dd[4] = {data[0], data[1], data[2], dat ...

  3. OpenStack实践系列⑦深入理解neutron和虚拟机

    OpenStack实践系列⑦深入理解neutron和虚拟机 五.深入理解Neutron 5.1 虚拟机网卡和网桥 [root@node1 ~]# ifconfig brq65c11cc3-8e: fl ...

  4. Golang 优化之路-空结构[转]

    写在前面 开发 hashset 常用的套路: map[int]int8 map[int]bool 我们一般只用 map 的键来保存数据,值是没有用的.所以来缓存集合数据会造成内存浪费. 空对象 空对象 ...

  5. JetBrains GoLand 2018 激活码/ 注册码(最新破解方法)

    1 前言 本机测试环境如下: Goland版本:2018.1.5 电脑系统:win7 64位 JetbrainsCrack.jar:链接: https://pan.baidu.com/s/1blmN3 ...

  6. Python学习-字符编码浅析

    1.什么是字符编码 既然是简述那肯定是简单明了.字符编码,看名字就是一种字符的编码格式,由于计算机内部采用二进制,想要将人类的语言字符输入到计算机就需要一种编码格式,这就是字符编码.字符------- ...

  7. 计算机中K到底是1000还是1024?

    1000和1024的争论,其实是传输领域和存储领域概念不清引起的;在传输领域,1秒钟传输多少字位(即b,bit),肯定是用10进制表示,所以是1kb=1000b,即1秒钟传输1000个比特位;就好像: ...

  8. 鼠标hover图片时遮罩层匀速上升显示内容top、定位

    1.html <div class="div1">   <div class="div11">   <p >Dolor nu ...

  9. vscode开发c#

    转载自: http://www.cnblogs.com/lxhbky/p/6673230.html http://www.cnblogs.com/lxhbky/p/6692065.html 一.环境安 ...

  10. 存储过程数据insert into select

    create or replace procedure PRO_K3_CZZH (org_name in varchar2, --财政专户名称 opertype in varchar2, --操作类型 ...